Skip to content

Commit c5ce863

Browse files
committed
feat: remove unused filters
1 parent 9bb2f81 commit c5ce863

File tree

3 files changed

+4
-137
lines changed

3 files changed

+4
-137
lines changed

openapi.config.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
{
22
"input": "./test/spec/saddlebackApi.json",
33
"output": "./test/test/saddleback/",
4-
"httpClient": "axios",
5-
"clientName": "TestAxiosClassName",
4+
"httpClient": "saddleback",
5+
"clientName": "",
66
"useOptions": true,
77
"useUnionTypes": false,
8-
"exportCore": true,
8+
"exportCore": false,
99
"exportServices": true,
1010
"exportModels": true,
1111
"exportSchemas": false,
1212
"indent": "4",
13-
"postfix": "",
14-
"filterMethod": "greedy",
15-
"filterArray": ["/api/agreement", "/api/agreement/{id}", "/api/share-page/compare-results"]
13+
"postfix": ""
1614
}

src/generateCustomSpec.ts

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,12 @@
11
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';
102

113
type Config = Options & {
12-
filterMethod: 'greedy' | 'ascetic';
13-
filterArray: string[];
14-
input: string;
154
useSaddlebackServices?: boolean;
165
additionalModelFileExtension?: boolean;
176
additionalServiceFileExtension?: boolean;
187
};
198

209
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-
13810
await generate({ ...config, input: listWithRequiredPaths });
13911
};
14012

types/index.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ export type Options = {
3131
};
3232

3333
export type CustomConfig = Options & {
34-
filterMethod: 'greedy' | 'ascetic';
35-
filterArray: string[];
36-
input: string;
3734
additionalModelFileExtension?: boolean;
3835
additionalServiceFileExtension?: boolean;
3936
};

0 commit comments

Comments
 (0)