@devlearning/swagger-generator 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generator.js +249 -1
- package/dist/index.js +89 -33
- package/dist/models/swagger-component.js +1 -0
- package/dist/swagger-downloader.js +9 -14
- package/package.json +3 -2
- package/src/generator.ts +309 -0
- package/src/index.ts +4 -4
- package/src/models/swagger-component.ts +8 -0
- package/src/models/swagger.ts +2 -7
- package/src/swagger-downloader.ts +7 -6
- package/tsconfig.json +2 -21
- package/dist/output/api.autogenerated.js +0 -392
- package/dist/output/model.autogenerated.js +0 -354
package/src/generator.ts
CHANGED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
// import { Swagger } from "./models/swagger";
|
|
2
|
+
|
|
3
|
+
// class Generator {
|
|
4
|
+
// private _swagger: Swagger;
|
|
5
|
+
|
|
6
|
+
// constructor(swagger: Swagger) {
|
|
7
|
+
// this._swagger = swagger;
|
|
8
|
+
// }
|
|
9
|
+
|
|
10
|
+
// generateApi() {
|
|
11
|
+
// console.debug(`Start autogeneration Apis`);
|
|
12
|
+
|
|
13
|
+
// let apiMethods = ``;
|
|
14
|
+
// for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
15
|
+
// const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
16
|
+
// const swaggerMethod = this._swagger.paths[apiName];
|
|
17
|
+
// const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
18
|
+
// const swaggerMethodInfo = swaggerMethod[method];
|
|
19
|
+
// console.debug(`\tAPI - ${apiName} - ${method}`);
|
|
20
|
+
|
|
21
|
+
// let parametersString = this.retrieveParameters(swaggerMethodInfo);
|
|
22
|
+
// let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
|
|
23
|
+
// let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
|
|
24
|
+
// if (returnTypeString == null) returnTypeString = 'void';
|
|
25
|
+
// let prepareRequestString = ``; //request = this._handleRequest(request);
|
|
26
|
+
// let haveRequest = swaggerMethodInfo.requestBody != null;
|
|
27
|
+
|
|
28
|
+
// if (haveRequest) {
|
|
29
|
+
// prepareRequestString = `request = this._handleRequest(request);
|
|
30
|
+
// `;
|
|
31
|
+
// }
|
|
32
|
+
|
|
33
|
+
// apiMethods +=
|
|
34
|
+
// `
|
|
35
|
+
// public ${apiName.replace('/api/v{version}/', '').replaceAll('/', '_')}(${parametersString}): Observable<${returnTypeString}>{
|
|
36
|
+
// ${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${environment.BASE_URL}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', request' : ''}, httpOptions)
|
|
37
|
+
// .pipe(
|
|
38
|
+
// map(x => this._handleResponse(x)),
|
|
39
|
+
// catchError((err, obs) => {
|
|
40
|
+
// return this._handleError(err, <Observable<any>>obs);
|
|
41
|
+
// })
|
|
42
|
+
// );
|
|
43
|
+
// }
|
|
44
|
+
// `;
|
|
45
|
+
// }
|
|
46
|
+
|
|
47
|
+
// fs.writeFileSync(outputDirectory + "/api.autogenerated.ts",
|
|
48
|
+
// `${apiPre}
|
|
49
|
+
// ${apiMethods}
|
|
50
|
+
// ${apiPost}`,
|
|
51
|
+
// { flag: 'w' });
|
|
52
|
+
// }
|
|
53
|
+
|
|
54
|
+
// generateModel() {
|
|
55
|
+
// let usedTypes: string[] = [];
|
|
56
|
+
|
|
57
|
+
// for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
58
|
+
// const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
59
|
+
// const swaggerMethod = this._swagger.paths[apiName];
|
|
60
|
+
// const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
61
|
+
// const swaggerMethodInfo = swaggerMethod[method];
|
|
62
|
+
|
|
63
|
+
// // if(apiName == "/api/v{version}/Ticket/Create"){
|
|
64
|
+
// // debugger
|
|
65
|
+
// // }
|
|
66
|
+
// let parametersRefType = swaggerMethodInfo.parameters.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
|
|
67
|
+
// usedTypes = usedTypes.concat(parametersRefType);
|
|
68
|
+
|
|
69
|
+
// if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null) {
|
|
70
|
+
// usedTypes.push(swaggerMethodInfo.responses[200].content[contentType].schema.$ref.replace('#/components/schemas/', ''));
|
|
71
|
+
// }
|
|
72
|
+
|
|
73
|
+
// if (swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref) {
|
|
74
|
+
// usedTypes.push(swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref.replace('#/components/schemas/', ''));
|
|
75
|
+
// }
|
|
76
|
+
// }
|
|
77
|
+
|
|
78
|
+
// this.retrieveNestedObjects(usedTypes);
|
|
79
|
+
|
|
80
|
+
// usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
|
|
81
|
+
|
|
82
|
+
// // usedTypes = usedTypes.filter((value, index, array) => {
|
|
83
|
+
// // array.indexOf(value) === index;
|
|
84
|
+
// // });
|
|
85
|
+
// // usedTypes.forEach(element => {
|
|
86
|
+
// // console.debug(element);
|
|
87
|
+
// // });
|
|
88
|
+
|
|
89
|
+
// console.debug(`Start autogeneration Models`);
|
|
90
|
+
// let models = ``;
|
|
91
|
+
// for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
|
|
92
|
+
// const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
|
|
93
|
+
|
|
94
|
+
// if (modelName == 'ActivatedVoucherSnackListResponse') {
|
|
95
|
+
// console.debug("ActivatedVoucherSnackListResponse");
|
|
96
|
+
// }
|
|
97
|
+
|
|
98
|
+
// if (usedTypes.indexOf(modelName) < 0) {
|
|
99
|
+
// console.debug(`\tModel SKIP - ${modelName}`);
|
|
100
|
+
// continue
|
|
101
|
+
// };
|
|
102
|
+
|
|
103
|
+
// const swaggerCopmponent = this._swagger.components.schemas[modelName];
|
|
104
|
+
|
|
105
|
+
// console.debug(`\tModel - ${modelName}`);
|
|
106
|
+
|
|
107
|
+
// let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
|
|
108
|
+
// let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
|
|
109
|
+
|
|
110
|
+
// models +=
|
|
111
|
+
// `
|
|
112
|
+
// export ${type} ${modelName} {${content}
|
|
113
|
+
// }
|
|
114
|
+
// `;
|
|
115
|
+
// }
|
|
116
|
+
|
|
117
|
+
// fs.writeFileSync(outputDirectory + "/model.autogenerated.ts",
|
|
118
|
+
// `${modelPre}
|
|
119
|
+
// ${models}
|
|
120
|
+
// ${modelPost}`,
|
|
121
|
+
// { flag: 'w' });
|
|
122
|
+
// }
|
|
123
|
+
|
|
124
|
+
// retrieveParameters(swaggerMethodInfo: SwaggerMethod) {
|
|
125
|
+
// if (swaggerMethodInfo.requestBody != null) {
|
|
126
|
+
// return `request: ${swaggerMethodInfo.requestBody.content[contentType].schema.$ref.replace('#/components/schemas/', '')}`
|
|
127
|
+
// }
|
|
128
|
+
|
|
129
|
+
// let parameters = ``;
|
|
130
|
+
// swaggerMethodInfo.parameters.filter(x => x.in == 'query').forEach(parameter => {
|
|
131
|
+
// if (parameter.schema.$ref != null) {
|
|
132
|
+
// parameters += `${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
133
|
+
// } else {
|
|
134
|
+
// parameters += `${parameter.name}: ${this.getNativeType(parameter.schema)}, `;
|
|
135
|
+
// }
|
|
136
|
+
// });
|
|
137
|
+
|
|
138
|
+
// if (parameters.length > 2)
|
|
139
|
+
// parameters = parameters.substring(0, parameters.length - 2);
|
|
140
|
+
|
|
141
|
+
// return parameters;
|
|
142
|
+
// }
|
|
143
|
+
|
|
144
|
+
// retrieveQueryParameters(swaggerMethodInfo: SwaggerMethod) {
|
|
145
|
+
// if (swaggerMethodInfo.requestBody != null) return ``;
|
|
146
|
+
|
|
147
|
+
// let filteredParameters = swaggerMethodInfo.parameters.filter(x => x.in == 'query');
|
|
148
|
+
// if (filteredParameters.length == 0) return ``;
|
|
149
|
+
|
|
150
|
+
// let parameters = `?`;
|
|
151
|
+
// filteredParameters.forEach(parameter => {
|
|
152
|
+
// if (parameter.schema.$ref != null) {
|
|
153
|
+
// this.parametrizeObject(parameter.schema.$ref)
|
|
154
|
+
// //parameters += `${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}&`;
|
|
155
|
+
// } else {
|
|
156
|
+
// parameters += `${this.lowercaseFirstLetter(parameter.name)}=\${` + this.lowercaseFirstLetter(parameter.name) + `}&`;
|
|
157
|
+
// }
|
|
158
|
+
// });
|
|
159
|
+
|
|
160
|
+
// if (parameters.length > 1)
|
|
161
|
+
// parameters = parameters.substring(0, parameters.length - 1);
|
|
162
|
+
|
|
163
|
+
// return parameters;
|
|
164
|
+
// }
|
|
165
|
+
|
|
166
|
+
// retrieveReturnType(swaggerMethodInfo: SwaggerMethod) {
|
|
167
|
+
// if (swaggerMethodInfo.responses[200] == null)
|
|
168
|
+
// return 'void';
|
|
169
|
+
|
|
170
|
+
// if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null)
|
|
171
|
+
// return swaggerMethodInfo.responses[200]?.content[contentType].schema.$ref.replace('#/components/schemas/', '')
|
|
172
|
+
|
|
173
|
+
// if (swaggerMethodInfo.responses[200]?.content[contentType].schema.type != null)
|
|
174
|
+
// return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentType].schema);
|
|
175
|
+
|
|
176
|
+
// console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
|
|
177
|
+
// throw new Error("unmanaged swaggerMethodInfo");
|
|
178
|
+
// }
|
|
179
|
+
|
|
180
|
+
// retrieveType(swaggerComponentProperty: SwaggerComponentProperty) {
|
|
181
|
+
// if (swaggerComponentProperty.$ref != null)
|
|
182
|
+
// return swaggerComponentProperty.$ref.replace('#/components/schemas/', '');
|
|
183
|
+
|
|
184
|
+
// if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
|
|
185
|
+
// if (swaggerComponentProperty.items.$ref != null)
|
|
186
|
+
// return swaggerComponentProperty.items.$ref.replace('#/components/schemas/', '');
|
|
187
|
+
// else
|
|
188
|
+
// return this.getNativeType(swaggerComponentProperty);
|
|
189
|
+
|
|
190
|
+
// if (swaggerComponentProperty.type != null)
|
|
191
|
+
// return this.getNativeType(swaggerComponentProperty);
|
|
192
|
+
|
|
193
|
+
// if (swaggerComponentProperty.type == null)
|
|
194
|
+
// return '';
|
|
195
|
+
|
|
196
|
+
// console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
|
|
197
|
+
// throw new Error("unmanaged swaggerMethodInfo");
|
|
198
|
+
// }
|
|
199
|
+
|
|
200
|
+
// parametrizeObject(objectName: string) {
|
|
201
|
+
// let component = this._swagger.components.schemas[objectName.replace('#/components/schemas/', '')];
|
|
202
|
+
// if (component == null || component.properties == null) return ``;
|
|
203
|
+
|
|
204
|
+
// console.debug(component.properties);
|
|
205
|
+
|
|
206
|
+
// return ``;
|
|
207
|
+
// }
|
|
208
|
+
|
|
209
|
+
// retrieveObjectContent(name: string, swaggerComponent: SwaggerComponent) {
|
|
210
|
+
// if (swaggerComponent.type == 'object')
|
|
211
|
+
// return this.retrieveObjectProperties(swaggerComponent);
|
|
212
|
+
// else if (swaggerComponent.type == 'integer')
|
|
213
|
+
// return this.retrieveEnumValues(name, swaggerComponent);
|
|
214
|
+
// }
|
|
215
|
+
|
|
216
|
+
// retrieveObjectProperties(swaggerCopmponent: SwaggerComponent) {
|
|
217
|
+
// if (swaggerCopmponent.properties == null) return ``;
|
|
218
|
+
|
|
219
|
+
// // console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
|
|
220
|
+
|
|
221
|
+
// let properties = ``;
|
|
222
|
+
// for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
223
|
+
// const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
224
|
+
// properties +=
|
|
225
|
+
// `
|
|
226
|
+
// ${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`
|
|
227
|
+
// }
|
|
228
|
+
|
|
229
|
+
// return properties;
|
|
230
|
+
// }
|
|
231
|
+
|
|
232
|
+
// retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
|
|
233
|
+
// if (swaggerCopmponent.enum == null) return ``;
|
|
234
|
+
|
|
235
|
+
// let properties = ``;
|
|
236
|
+
// for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
|
|
237
|
+
// const name = swaggerCopmponent.enum[index].split('-')[0].trim();
|
|
238
|
+
// const value = swaggerCopmponent.enum[index].split('-')[1].trim();
|
|
239
|
+
// properties +=
|
|
240
|
+
// `
|
|
241
|
+
// ${name} = ${value},`;
|
|
242
|
+
// }
|
|
243
|
+
|
|
244
|
+
// return properties;
|
|
245
|
+
// }
|
|
246
|
+
|
|
247
|
+
// retrieveNestedObjects(usedTypes: string[]) {
|
|
248
|
+
// for (let i = 0; i < usedTypes.length; i++) {
|
|
249
|
+
// const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
|
|
250
|
+
// // const name = usedTypes[i]
|
|
251
|
+
// // const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
|
|
252
|
+
// this.retrieveNestedObjects2(swaggerCopmponent, usedTypes);
|
|
253
|
+
// }
|
|
254
|
+
// }
|
|
255
|
+
|
|
256
|
+
// /*
|
|
257
|
+
// "ActivatedVoucherSnackReadQueryResponse": {
|
|
258
|
+
// "type": "object",
|
|
259
|
+
// "properties": {
|
|
260
|
+
// "list": {
|
|
261
|
+
// "type": "array",
|
|
262
|
+
// "items": {
|
|
263
|
+
// "$ref": "#/components/schemas/ActivatedVoucherSnackListResponse"
|
|
264
|
+
// },
|
|
265
|
+
// "nullable": true
|
|
266
|
+
// }
|
|
267
|
+
// },
|
|
268
|
+
// "additionalProperties": false
|
|
269
|
+
// },
|
|
270
|
+
// */
|
|
271
|
+
|
|
272
|
+
// retrieveNestedObjects2(swaggerComponent: SwaggerComponent, usedTypes: string[]) {
|
|
273
|
+
// if (!swaggerComponent.properties) return;
|
|
274
|
+
|
|
275
|
+
// for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
|
|
276
|
+
// const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
|
|
277
|
+
// let nestedUsedType = '';
|
|
278
|
+
// if (swaggerComponent.properties[propertyName].$ref != null) {
|
|
279
|
+
// nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
|
|
280
|
+
// } else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
|
|
281
|
+
// nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
|
|
282
|
+
// }
|
|
283
|
+
|
|
284
|
+
// if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
|
|
285
|
+
// usedTypes.push(nestedUsedType);
|
|
286
|
+
// let nested = this._swagger.components.schemas[nestedUsedType];
|
|
287
|
+
// this.retrieveNestedObjects2(nested, usedTypes);
|
|
288
|
+
// }
|
|
289
|
+
// }
|
|
290
|
+
// }
|
|
291
|
+
|
|
292
|
+
// getNativeType(schema: SwaggerSchema): string {
|
|
293
|
+
// if (schema.type == 'integer') return 'number';
|
|
294
|
+
// if (schema.type == 'string' && schema.format == null) return 'string';
|
|
295
|
+
// if (schema.type == 'string' && schema.format == 'date-time') return 'moment.Moment';
|
|
296
|
+
// if (schema.type == 'string' && schema.format == 'uuid') return 'string';
|
|
297
|
+
// if (schema.type == 'number') return 'number';
|
|
298
|
+
// if (schema.type == 'array') return '[]';
|
|
299
|
+
// if (schema.type == 'boolean') return 'boolean';
|
|
300
|
+
// if (schema.type == 'object') return 'any';
|
|
301
|
+
|
|
302
|
+
// console.error("unmanaged schema type", schema);
|
|
303
|
+
// throw new Error("unmanaged schema");
|
|
304
|
+
// }
|
|
305
|
+
|
|
306
|
+
// lowercaseFirstLetter(value: string) {
|
|
307
|
+
// return value.charAt(0).toLowerCase() + value.slice(1);
|
|
308
|
+
// }
|
|
309
|
+
// }
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import { Swagger, SwaggerComponent, SwaggerComponentProperty, SwaggerMethod, SwaggerSchema } from './models/swagger.js';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { SwaggerComponent } from './models/swagger-component.js';
|
|
4
|
+
import { Swagger, SwaggerComponentProperty, SwaggerComponents, SwaggerMethod, SwaggerSchema } from './models/swagger.js';
|
|
6
5
|
import { SwaggerDownloader } from './swagger-downloader.js';
|
|
7
6
|
|
|
7
|
+
|
|
8
8
|
var args = process.argv.slice(2);
|
|
9
9
|
|
|
10
10
|
if (args.length !== 2) {
|
package/src/models/swagger.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { SwaggerComponent } from "./swagger-component.js";
|
|
2
|
+
|
|
1
3
|
export interface Swagger {
|
|
2
4
|
openApi: string;
|
|
3
5
|
info: SwaggerInfo;
|
|
@@ -37,13 +39,6 @@ export interface SwaggerComponents {
|
|
|
37
39
|
schemas: { [key: string]: SwaggerComponent; };
|
|
38
40
|
}
|
|
39
41
|
|
|
40
|
-
export interface SwaggerComponent {
|
|
41
|
-
type: string;
|
|
42
|
-
properties: { [key: string]: SwaggerComponentProperty; };
|
|
43
|
-
additionalProperties: boolean;
|
|
44
|
-
enum: string[];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
42
|
export interface SwaggerComponentProperty {
|
|
48
43
|
type: string;
|
|
49
44
|
$ref: string;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import fetch, { RequestInit } from 'node-fetch';
|
|
1
2
|
import { Swagger } from "./models/swagger.js";
|
|
2
3
|
|
|
3
4
|
const settings: RequestInit = { method: "Get" };
|
|
4
5
|
|
|
5
6
|
export class SwaggerDownloader {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
7
|
+
async download(swaggerJsonUrl: URL) {
|
|
8
|
+
let response = await fetch(swaggerJsonUrl, settings);
|
|
9
|
+
let json = await response.json();
|
|
10
|
+
return <Swagger>json;
|
|
11
|
+
}
|
|
12
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"target": "ES2022",
|
|
4
|
-
"module": "
|
|
4
|
+
"module": "ES2022",
|
|
5
5
|
"baseUrl": "./src",
|
|
6
6
|
"moduleResolution": "NodeNext",
|
|
7
7
|
"paths": {
|
|
@@ -26,23 +26,4 @@
|
|
|
26
26
|
"tsconfig-paths/register"
|
|
27
27
|
]
|
|
28
28
|
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// {
|
|
32
|
-
// "compilerOptions": {
|
|
33
|
-
// "target": "es6",
|
|
34
|
-
// "module": "commonjs",
|
|
35
|
-
// "esModuleInterop": true,
|
|
36
|
-
// "outDir": "./dist",
|
|
37
|
-
// "rootDir": "./src",
|
|
38
|
-
// "resolveJsonModule": true,
|
|
39
|
-
// "sourceMap": true
|
|
40
|
-
// },
|
|
41
|
-
// "include": [
|
|
42
|
-
// "src//*.ts"
|
|
43
|
-
// ],
|
|
44
|
-
// "exclude": [
|
|
45
|
-
// "node_modules",
|
|
46
|
-
// "/*.spec.ts"
|
|
47
|
-
// ]
|
|
48
|
-
// }
|
|
29
|
+
}
|