Skip to content

Commit a842cf9

Browse files
committed
feat: add custom generator to generate only required url paths
1 parent 6b32b65 commit a842cf9

File tree

4 files changed

+80575
-0
lines changed

4 files changed

+80575
-0
lines changed

src/generateCustomSpec.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { OpenApi } from './openApi/v3/interfaces/OpenApi';
2+
import { OpenApiMediaType } from './openApi/v3/interfaces/OpenApiMediaType';
3+
import { OpenApiOperation } from './openApi/v3/interfaces/OpenApiOperation';
4+
import { OpenApiParameter } from './openApi/v3/interfaces/OpenApiParameter';
5+
import { OpenApiSchema } from './openApi/v3/interfaces/OpenApiSchema';
6+
import { OpenApiServer } from './openApi/v3/interfaces/OpenApiServer';
7+
import { getOpenApiSpec } from './utils/getOpenApiSpec';
8+
import { Dictionary } from './utils/types';
9+
10+
export const generateCustomSpec = async (gen: any, input: string, output: string, config: Record<string, unknown>) => {
11+
const getSchemaRefFromContent = (content: OpenApiMediaType): string => {
12+
let ref: string = '';
13+
14+
ref = content.$ref || content.schema?.$ref || content.schema?.items?.$ref || '';
15+
16+
return ref.split('/').slice(-1)[0];
17+
};
18+
19+
const list: OpenApi = await getOpenApiSpec(input);
20+
21+
const requiredPathsList: string[] = ['/api/agreement', '/api/agreement/{id}'];
22+
23+
const requiredPaths: OpenApi['paths'] = {};
24+
25+
for (const path in list.paths) {
26+
if (requiredPathsList.some(it => it === path)) {
27+
requiredPaths[path] = list.paths[path];
28+
}
29+
}
30+
31+
const requiredSchemasSet: Set<string> = new Set();
32+
33+
for (const pathName in requiredPaths) {
34+
const pathElement = requiredPaths[pathName];
35+
36+
const openApiPathValues = Object.values(pathElement) as (
37+
| OpenApiOperation
38+
| OpenApiServer
39+
| OpenApiParameter
40+
| string
41+
)[];
42+
43+
openApiPathValues.forEach(requestMethodData => {
44+
if (typeof requestMethodData !== 'string') {
45+
if (!('url' in requestMethodData)) {
46+
if ('parameters' in requestMethodData) {
47+
// add schemas from {apiPath}/{method}/parameters
48+
requestMethodData.parameters?.forEach(parameter =>
49+
requiredSchemasSet.add(getSchemaRefFromContent(parameter))
50+
);
51+
}
52+
if ('responses' in requestMethodData) {
53+
const responsesCodeData = Object.values(requestMethodData.responses);
54+
55+
responsesCodeData.forEach(response => {
56+
const contentTypeData = Object.values(response.content ?? {});
57+
58+
// add schemas from {apiPath}/{method}/responses/{responseType}/content
59+
contentTypeData.forEach(content => {
60+
requiredSchemasSet.add(getSchemaRefFromContent(content));
61+
});
62+
});
63+
}
64+
}
65+
}
66+
});
67+
}
68+
69+
const requiredSchemas: Dictionary<OpenApiSchema> = {};
70+
71+
if (list && list.components && list.components.schemas) {
72+
for (const schema in list.components.schemas) {
73+
if (requiredSchemasSet.has(schema)) {
74+
requiredSchemas[schema] = list.components.schemas[schema];
75+
}
76+
}
77+
}
78+
79+
const listWithRequiredPaths: OpenApi = {
80+
...list,
81+
paths: requiredPaths,
82+
components: {
83+
schemas: requiredSchemas,
84+
},
85+
};
86+
87+
await gen(listWithRequiredPaths, output);
88+
};

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { postProcessClient } from './utils/postProcessClient';
99
import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates';
1010
import { writeClient } from './utils/writeClient';
1111

12+
export { generateCustomSpec } from './generateCustomSpec';
1213
export { HttpClient } from './HttpClient';
1314
export { Indent } from './Indent';
1415

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const generateRealWorldSpecs = async () => {
5959
const main = async () => {
6060
await generate('./test/spec/v2.json', './test/generated/v2/');
6161
await generate('./test/spec/v3.json', './test/generated/v3/');
62+
await OpenAPI.generateCustomSpec(generate, './test/spec/saddlebackApi.json', './test/generated/saddleback/');
6263
// await generateRealWorldSpecs();
6364
};
6465

0 commit comments

Comments
 (0)