Skip to content

Commit b2751d0

Browse files
SferaDevfabien0102
andauthored
feat: Add fetchers dictionary by tag (#45)
* Add fetchers dictionary by tag Signed-off-by: Alexis Rico <[email protected]> * Add snapshot test Signed-off-by: Alexis Rico <[email protected]> * Conditionally build operations by tag Signed-off-by: Alexis Rico <[email protected]> * Refactor * Add @SferaDev as contributor Co-authored-by: Fabien BERNARD <[email protected]>
1 parent 80ee3e0 commit b2751d0

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
"contributions": [
3030
"doc"
3131
]
32+
},
33+
{
34+
"login": "SferaDev",
35+
"name": "Alexis Rico",
36+
"avatar_url": "https://avatars.githubusercontent.com/u/2181866?v=4",
37+
"profile": "https://github.com/SferaDev",
38+
"contributions": [
39+
"code",
40+
"ideas"
41+
]
3242
}
3343
],
3444
"contributorsPerLine": 7

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
166166
<tr>
167167
<td align="center"><a href="https://github.com/mpotomin"><img src="https://avatars.githubusercontent.com/u/639406?v=4?s=100" width="100px;" alt=""/><br /><sub><b>mpotomin</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=mpotomin" title="Code">💻</a> <a href="#ideas-mpotomin" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/fabien0102/openapi-codegen/pulls?q=is%3Apr+reviewed-by%3Ampotomin" title="Reviewed Pull Requests">👀</a></td>
168168
<td align="center"><a href="https://github.com/micha-f"><img src="https://avatars.githubusercontent.com/u/200647?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michael Franzkowiak</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=micha-f" title="Documentation">📖</a></td>
169+
<td align="center"><a href="https://github.com/SferaDev"><img src="https://avatars.githubusercontent.com/u/2181866?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Alexis Rico</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=SferaDev" title="Code">💻</a> <a href="#ideas-SferaDev" title="Ideas, Planning, & Feedback">🤔</a></td>
169170
</tr>
170171
</table>
171172

plugins/typescript/src/generators/generateFetchers.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { set } from "lodash";
12
import { OpenAPIObject } from "openapi3-ts";
23
import { Config, generateFetchers } from "./generateFetchers";
34

@@ -117,4 +118,45 @@ describe("generateFetchers", () => {
117118
"
118119
`);
119120
});
121+
122+
it("should generate fetcher with operations by tag", async () => {
123+
const writeFile = jest.fn();
124+
125+
const openAPIDocumentWithTags = set(
126+
openAPIDocument,
127+
"paths./pets.get.tags",
128+
["pets"]
129+
);
130+
131+
await generateFetchers(
132+
{
133+
openAPIDocument: openAPIDocumentWithTags,
134+
writeFile,
135+
readFile: async () => "",
136+
existsFile: () => true,
137+
},
138+
config
139+
);
140+
141+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
142+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
143+
"/**
144+
* Generated by @openapi-codegen
145+
*
146+
* @version 1.0.0
147+
*/
148+
import { petstoreFetch } from \\"./petstoreFetcher\\";
149+
import type * as Schemas from \\"./petstoreSchemas\\";
150+
151+
export type ListPetsResponse = Schemas.Pet[];
152+
153+
/**
154+
* Get all the pets
155+
*/
156+
export const listPets = () => petstoreFetch<ListPetsResponse, undefined, {}, {}, {}>({ url: \\"/pets\\", method: \\"get\\" });
157+
158+
export const operationsByTag = { \\"pets\\": { listPets } };
159+
"
160+
`);
161+
});
120162
});

plugins/typescript/src/generators/generateFetchers.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export const generateFetchers = async (context: Context, config: Config) => {
115115
}
116116

117117
const operationIds: string[] = [];
118+
const operationByTags: Record<string, string[]> = {};
118119

119120
Object.entries(context.openAPIDocument.paths).forEach(
120121
([route, verbs]: [string, PathItemObject]) => {
@@ -126,7 +127,12 @@ export const generateFetchers = async (context: Context, config: Config) => {
126127
`The operationId "${operation.operationId}" is duplicated in your schema definition!`
127128
);
128129
}
130+
129131
operationIds.push(operationId);
132+
operation.tags?.forEach((tag) => {
133+
if (!operationByTags[tag]) operationByTags[tag] = [];
134+
operationByTags[tag].push(operationId);
135+
});
130136

131137
const {
132138
dataType,
@@ -166,6 +172,36 @@ export const generateFetchers = async (context: Context, config: Config) => {
166172
}
167173
);
168174

175+
if (Object.keys(operationByTags).length > 0) {
176+
nodes.push(
177+
f.createVariableStatement(
178+
[f.createModifier(ts.SyntaxKind.ExportKeyword)],
179+
f.createVariableDeclarationList(
180+
[
181+
f.createVariableDeclaration(
182+
f.createIdentifier("operationsByTag"),
183+
undefined,
184+
undefined,
185+
f.createObjectLiteralExpression(
186+
Object.entries(operationByTags).map(([tag, operationIds]) => {
187+
return f.createPropertyAssignment(
188+
f.createStringLiteral(c.camel(tag)),
189+
f.createObjectLiteralExpression(
190+
operationIds.map((operationId) =>
191+
f.createShorthandPropertyAssignment(operationId)
192+
)
193+
)
194+
);
195+
})
196+
)
197+
),
198+
],
199+
ts.NodeFlags.Const
200+
)
201+
)
202+
);
203+
}
204+
169205
await context.writeFile(
170206
filename + ".ts",
171207
printNodes([

0 commit comments

Comments
 (0)