Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@

<img style="max-width: 400px" src="https://user-images.githubusercontent.com/271912/194000679-5a4501b8-5fc0-430c-9217-028bf91a5dcd.gif">

If you wish to change any of the selections made, you can do so in the generated `openapi-codegen.config.ts` file later..
If you wish to change any of the selections made, you can do so in the generated `openapi-codegen.config.ts` file later.

2. **Start Generation**

```bash
npx openapi-codegen gen {namespace}
npx @openapi-codegen/cli gen {namespace}
```

After the code generation is done, you will notice the following files:
Expand All @@ -61,7 +61,7 @@

3. **Configure the Fetcher** (optional)

After the first step you should see a file called `{namespace}Fetcher.ts` in your ouput directory.
After the first step you should see a file called `{namespace}Fetcher.ts` in your output directory.

By default it uses the built-in Fetch API, you are free to change this to your fetching library of choice (Axios, Got etc.)

Expand Down
87 changes: 87 additions & 0 deletions plugins/typescript/src/generators/generateFetchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,93 @@ describe("generateFetchers", () => {
`);
});

it("should generate variables for the Xquik search query", async () => {
const writeFile = createWriteFileMock();
const xquikDocument: OpenAPIObject = {
openapi: "3.0.0",
info: {
title: "Xquik API",
version: "1.0.0",
},
components: {
securitySchemes: {
apiKey: {
type: "apiKey",
in: "header",
name: "x-api-key",
},
},
},
paths: {
"/api/v1/x/tweets/search": {
get: {
operationId: "searchTweets",
description: "Search tweets",
security: [{ apiKey: [] }],
parameters: [
{
name: "q",
in: "query",
required: true,
schema: { type: "string" },
},
{
name: "limit",
in: "query",
required: false,
schema: { type: "integer", minimum: 1, maximum: 100 },
},
],
responses: {
"200": {
description: "Search results",
content: {
"application/json": {
schema: {
type: "object",
required: ["data"],
properties: {
data: {
type: "array",
items: {
type: "object",
required: ["id", "text"],
properties: {
id: { type: "string" },
text: { type: "string" },
},
},
},
},
},
},
},
},
},
},
},
},
};

await generateFetchers(
{
openAPIDocument: xquikDocument,
writeFile,
readFile: async () => "",
existsFile: () => true,
},
config
);

const output = writeFile.mock.calls[0][1];
expect(output).toContain("export type SearchTweetsQueryParams = {");
expect(output).toContain("export type SearchTweetsVariables = {");
expect(output).toContain("queryParams: SearchTweetsQueryParams;");
expect(output).toContain("q: string;");
expect(output).toContain("limit?: number;");
expect(output).toContain('url: "/api/v1/x/tweets/search"');
});

it("should generate fetchers without prefix", async () => {
const writeFile = createWriteFileMock();

Expand Down