|
1 | 1 | import { generate, Options } from './generate';
|
2 |
| -import { OpenApi } from './openApi/v3/interfaces/OpenApi'; |
3 |
| -import { OpenApiMediaType } from './openApi/v3/interfaces/OpenApiMediaType'; |
4 |
| -import { OpenApiOperation } from './openApi/v3/interfaces/OpenApiOperation'; |
5 |
| -import { OpenApiParameter } from './openApi/v3/interfaces/OpenApiParameter'; |
6 |
| -import { OpenApiSchema } from './openApi/v3/interfaces/OpenApiSchema'; |
7 |
| -import { OpenApiServer } from './openApi/v3/interfaces/OpenApiServer'; |
8 |
| -import { getOpenApiSpec } from './utils/getOpenApiSpec'; |
9 |
| -import { Dictionary } from './utils/types'; |
10 | 2 |
|
11 | 3 | type Config = Options & {
|
12 |
| - filterMethod: 'greedy' | 'ascetic'; |
13 |
| - filterArray: string[]; |
14 |
| - input: string; |
15 | 4 | useSaddlebackServices?: boolean;
|
16 | 5 | additionalModelFileExtension?: boolean;
|
17 | 6 | additionalServiceFileExtension?: boolean;
|
18 | 7 | };
|
19 | 8 |
|
20 | 9 | export const generateCustomSpec = async (config: Config) => {
|
21 |
| - const getNameFromRef = (ref: string): string => { |
22 |
| - return ref.split('/').slice(-1)[0]; |
23 |
| - }; |
24 |
| - |
25 |
| - const getSchemaRefFromContent = (content: OpenApiMediaType): string => { |
26 |
| - let ref: string = ''; |
27 |
| - |
28 |
| - ref = content.$ref || content.schema?.$ref || content.schema?.items?.$ref || ''; |
29 |
| - |
30 |
| - return getNameFromRef(ref); |
31 |
| - }; |
32 |
| - |
33 |
| - const recursiveAddAllUnknownModels = (modelName: string): void => { |
34 |
| - const model = list.components?.schemas ? list.components.schemas[modelName] : undefined; |
35 |
| - if (model === undefined) return; |
36 |
| - |
37 |
| - for (const property in model.properties) { |
38 |
| - const ref = model.properties[property].$ref || model.properties[property].items?.$ref || ''; |
39 |
| - const modelName = getNameFromRef(ref); |
40 |
| - |
41 |
| - if (!requiredSchemasSet.has(modelName)) { |
42 |
| - requiredSchemasSet.add(modelName); |
43 |
| - recursiveAddAllUnknownModels(modelName); |
44 |
| - } |
45 |
| - } |
46 |
| - }; |
47 |
| - |
48 |
| - const list: OpenApi = await getOpenApiSpec(config.input); |
49 |
| - |
50 |
| - // const filterArray: string[] = ['/api/agreement', '/api/agreement/{id}']; |
51 |
| - |
52 |
| - const requiredPaths: OpenApi['paths'] = {}; |
53 |
| - |
54 |
| - for (const path in list.paths) { |
55 |
| - if (!list.paths.hasOwnProperty(path)) return; |
56 |
| - |
57 |
| - if (config.filterMethod === 'ascetic') { |
58 |
| - if (config.filterArray.some(it => it === path)) requiredPaths[path] = list.paths[path]; |
59 |
| - } |
60 |
| - if (config.filterMethod === 'greedy') { |
61 |
| - if (!config.filterArray.some(it => it === path)) requiredPaths[path] = list.paths[path]; |
62 |
| - } |
63 |
| - } |
64 |
| - |
65 |
| - const requiredSchemasSet: Set<string> = new Set(); |
66 |
| - |
67 |
| - for (const pathName in requiredPaths) { |
68 |
| - const pathElement = requiredPaths[pathName]; |
69 |
| - |
70 |
| - const openApiPathValues = Object.values(pathElement) as ( |
71 |
| - | OpenApiOperation |
72 |
| - | OpenApiServer |
73 |
| - | OpenApiParameter |
74 |
| - | string |
75 |
| - )[]; |
76 |
| - |
77 |
| - openApiPathValues.forEach(requestMethodData => { |
78 |
| - if (typeof requestMethodData !== 'string') { |
79 |
| - if (!('url' in requestMethodData)) { |
80 |
| - if ('parameters' in requestMethodData) { |
81 |
| - // add schemas from {apiPath}/{method}/parameters |
82 |
| - requestMethodData.parameters?.forEach(parameter => { |
83 |
| - const modelName = getSchemaRefFromContent(parameter); |
84 |
| - |
85 |
| - requiredSchemasSet.add(modelName); |
86 |
| - recursiveAddAllUnknownModels(modelName); |
87 |
| - }); |
88 |
| - } |
89 |
| - if ('responses' in requestMethodData) { |
90 |
| - const responsesCodeData = Object.values(requestMethodData.responses); |
91 |
| - |
92 |
| - responsesCodeData.forEach(response => { |
93 |
| - const contentTypeData = Object.values(response.content ?? {}); |
94 |
| - |
95 |
| - // add schemas from {apiPath}/{method}/responses/{responseType}/content |
96 |
| - contentTypeData.forEach(content => { |
97 |
| - const modelName = getSchemaRefFromContent(content); |
98 |
| - |
99 |
| - requiredSchemasSet.add(getSchemaRefFromContent(content)); |
100 |
| - recursiveAddAllUnknownModels(modelName); |
101 |
| - }); |
102 |
| - }); |
103 |
| - } |
104 |
| - if ('requestBody' in requestMethodData) { |
105 |
| - const requestBodyContent = Object.values(requestMethodData.requestBody?.content ?? {}); |
106 |
| - |
107 |
| - // add schemas from {apiPath}/{method}/responses/{responseType}/requestBody/content |
108 |
| - requestBodyContent.forEach(content => { |
109 |
| - const modelName = getSchemaRefFromContent(content); |
110 |
| - |
111 |
| - requiredSchemasSet.add(getSchemaRefFromContent(content)); |
112 |
| - recursiveAddAllUnknownModels(modelName); |
113 |
| - }); |
114 |
| - } |
115 |
| - } |
116 |
| - } |
117 |
| - }); |
118 |
| - } |
119 |
| - |
120 |
| - const requiredSchemas: Dictionary<OpenApiSchema> = {}; |
121 |
| - |
122 |
| - if (list && list.components && list.components.schemas) { |
123 |
| - for (const schema in list.components.schemas) { |
124 |
| - if (requiredSchemasSet.has(schema)) { |
125 |
| - requiredSchemas[schema] = list.components.schemas[schema]; |
126 |
| - } |
127 |
| - } |
128 |
| - } |
129 |
| - |
130 |
| - const listWithRequiredPaths: OpenApi = { |
131 |
| - ...list, |
132 |
| - paths: requiredPaths, |
133 |
| - components: { |
134 |
| - schemas: requiredSchemas, |
135 |
| - }, |
136 |
| - }; |
137 |
| - |
138 | 10 | await generate({ ...config, input: listWithRequiredPaths });
|
139 | 11 | };
|
140 | 12 |
|
|
0 commit comments