@devlearning/swagger-generator 1.1.16 → 1.1.17

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.
Files changed (58) hide show
  1. package/.vscode/launch.json +28 -28
  2. package/README-OLD.md +209 -209
  3. package/README.md +277 -277
  4. package/dist/api.constants.js +22 -22
  5. package/dist/generator.d.ts +4 -0
  6. package/dist/generator.js +118 -4
  7. package/dist/generators-writers/angular/api-angular-writer.js +38 -38
  8. package/dist/generators-writers/angular/constants.js +24 -24
  9. package/dist/generators-writers/angular/model-angular-writer.js +6 -6
  10. package/dist/generators-writers/dart/model-dart-writer.d.ts +1 -0
  11. package/dist/generators-writers/dart/model-dart-writer.js +15 -14
  12. package/dist/generators-writers/dart/templates/api.mustache +143 -143
  13. package/dist/generators-writers/dart/templates/enum.mustache +14 -14
  14. package/dist/generators-writers/dart/templates/model.mustache +20 -23
  15. package/dist/generators-writers/nextjs/api-nextjs-writer.js +12 -12
  16. package/dist/generators-writers/nextjs/constants.js +4 -4
  17. package/dist/generators-writers/nextjs/model-nextjs-writer.js +6 -6
  18. package/dist/models/swagger/swagger-component.d.ts +2 -2
  19. package/dist/models/swagger/swagger-schema.d.ts +1 -0
  20. package/package.json +49 -49
  21. package/src/api.constants.ts +26 -26
  22. package/src/generator-old.ts +449 -449
  23. package/src/generator.ts +752 -625
  24. package/src/generators-writers/angular/api-angular-writer.ts +187 -187
  25. package/src/generators-writers/angular/constants.ts +36 -36
  26. package/src/generators-writers/angular/model-angular-writer.ts +65 -65
  27. package/src/generators-writers/angular/normalizator.ts +41 -41
  28. package/src/generators-writers/dart/api-dart-writer.ts +303 -303
  29. package/src/generators-writers/dart/model-dart-writer.ts +212 -209
  30. package/src/generators-writers/dart/models/import-definition-dart.ts +5 -5
  31. package/src/generators-writers/dart/normalizator.ts +72 -72
  32. package/src/generators-writers/dart/templates/api.mustache +143 -143
  33. package/src/generators-writers/dart/templates/enum.mustache +14 -14
  34. package/src/generators-writers/dart/templates/model.mustache +20 -23
  35. package/src/generators-writers/nextjs/api-nextjs-writer.ts +157 -157
  36. package/src/generators-writers/nextjs/constants.ts +5 -5
  37. package/src/generators-writers/nextjs/model-nextjs-writer.ts +61 -61
  38. package/src/generators-writers/utils.ts +93 -93
  39. package/src/index.ts +103 -103
  40. package/src/models/api-dto.ts +17 -17
  41. package/src/models/enum-value-dto.ts +3 -3
  42. package/src/models/model-dto.ts +9 -9
  43. package/src/models/parameter-dto.ts +7 -7
  44. package/src/models/property-dto.ts +4 -4
  45. package/src/models/swagger/swagger-component-property.ts +11 -11
  46. package/src/models/swagger/swagger-component.ts +17 -17
  47. package/src/models/swagger/swagger-content.ts +4 -4
  48. package/src/models/swagger/swagger-info.ts +3 -3
  49. package/src/models/swagger/swagger-method.ts +7 -7
  50. package/src/models/swagger/swagger-schema.ts +21 -20
  51. package/src/models/swagger/swagger.ts +38 -38
  52. package/src/models/type-dto.ts +7 -7
  53. package/src/swagger-downloader.ts +46 -46
  54. package/src/utils/logger.ts +73 -73
  55. package/src/utils/swagger-validator.ts +89 -89
  56. package/tsconfig.json +33 -33
  57. package/dist/templates/api.mustache +0 -29
  58. package/dist/templates/model.mustache +0 -18
@@ -1,450 +1,450 @@
1
- // import fs from 'fs';
2
- // import { SwaggerComponent } from './models/swagger/swagger-component.js';
3
- // import { Swagger, SwaggerParameter } from './models/swagger/swagger.js';
4
- // import { SwaggerMethod } from './models/swagger/swagger-method.js';
5
- // import { SwaggerComponentProperty } from './models/swagger/swagger-component-property.js';
6
- // import { SwaggerSchema } from './models/swagger/swagger-schema.js';
7
- // import { API_PRE, API_POST } from './api.constants.js';
8
- // import { MODEL_POST, MODEL_PRE } from './model.constants.js';
9
-
10
- // const contentTypeApplicationJson = 'application/json';
11
- // const contentTypeMultipartFormData = 'multipart/form-data';
12
-
13
- // export class GeneratorOld {
14
- // private _swagger: Swagger;
15
- // private _outputDirectory: string;
16
- // private _outputFormat: 'angular' | 'next';
17
-
18
- // constructor(swagger: Swagger, outputDirectory: string, outputFormat: 'angular' | 'next') {
19
- // this._swagger = swagger;
20
- // this._outputDirectory = outputDirectory;
21
- // this._outputFormat = outputFormat
22
- // }
23
-
24
- // generateApi() {
25
- // console.debug(`Start autogeneration Apis`);
26
-
27
- // let apiMethods = ``;
28
- // for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
29
- // const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
30
- // const swaggerMethod = this._swagger.paths[apiName];
31
- // const method = Object.getOwnPropertyNames(swaggerMethod)[0];
32
- // const swaggerMethodInfo = swaggerMethod[method];
33
- // console.debug(`\tAPI - ${apiName} - ${method}`);
34
-
35
- // let parametersString = this.retrieveParameters(swaggerMethodInfo, apiName);
36
- // let queryParametersDeclaration = this.retrieveQueryParametersDeclaration(swaggerMethodInfo);
37
- // let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
38
- // let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
39
- // if (returnTypeString == null) returnTypeString = 'void';
40
- // let prepareRequestString = ``; //request = this._handleRequest(request);
41
- // let haveRequest = swaggerMethodInfo.requestBody != null;
42
- // let isMultiPart = haveRequest && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null;
43
- // let httpOptions = 'httpOptions';
44
-
45
- // if (haveRequest && !isMultiPart) {
46
- // prepareRequestString = `const wrappedRequest = this._handleRequest(request);
47
- // `;
48
- // } else if (isMultiPart) {
49
- // prepareRequestString = `const wrappedRequest = this._handleMultipart(request);
50
- // `;
51
- // httpOptions = `httpOptionsMultipart`;
52
- // }
53
-
54
- // apiMethods +=
55
- // `
56
- // public ${this.getApiNameNormalized(apiName)}(${parametersString}): Observable<${returnTypeString}> {
57
- // ${queryParametersDeclaration}${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
58
- // .pipe(
59
- // map(x => this._handleResponse(x)),
60
- // catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
61
- // );
62
- // }
63
- // `;
64
- // }
65
-
66
- // fs.writeFileSync(this._outputDirectory + "/api.autogenerated.ts",
67
- // `${API_PRE}
68
- // ${apiMethods}
69
- // ${API_POST}`,
70
- // { flag: 'w' });
71
- // }
72
-
73
- // generateModel() {
74
- // let usedTypes: string[] = [];
75
- // let usedMultiPart: string[] = [];
76
-
77
- // for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
78
- // const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
79
- // const swaggerMethod = this._swagger.paths[apiName];
80
- // const method = Object.getOwnPropertyNames(swaggerMethod)[0];
81
- // const swaggerMethodInfo = swaggerMethod[method];
82
-
83
- // if (apiName == "/api/v{version}/TicketFile/Create") {
84
- // debugger
85
- // }
86
-
87
- // let parametersRefType = swaggerMethodInfo.parameters?.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
88
- // if (parametersRefType) {
89
- // usedTypes = usedTypes.concat(parametersRefType);
90
-
91
- // if (swaggerMethodInfo.responses[200].content[contentTypeApplicationJson]?.schema.$ref != null) {
92
- // usedTypes.push(swaggerMethodInfo.responses[200].content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''));
93
- // }
94
-
95
- // if (swaggerMethodInfo.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref) {
96
- // usedTypes.push(swaggerMethodInfo.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref.replace('#/components/schemas/', ''));
97
- // }
98
-
99
- // if (swaggerMethodInfo.requestBody?.content[contentTypeMultipartFormData]?.schema != null) {
100
- // usedMultiPart.push(apiName);
101
- // }
102
- // }
103
- // }
104
-
105
- // this.retrieveNestedObjects(usedTypes);
106
-
107
- // usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
108
-
109
- // // usedTypes = usedTypes.filter((value, index, array) => {
110
- // // array.indexOf(value) === index;
111
- // // });
112
- // // usedTypes.forEach(element => {
113
- // // console.debug(element);
114
- // // });
115
-
116
- // console.debug(`Start autogeneration Models`);
117
- // let models = ``;
118
- // if (this._swagger.components != null
119
- // && this._swagger.components != undefined
120
- // && this._swagger.components.schemas != null
121
- // && this._swagger.components.schemas != undefined) {
122
-
123
- // for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
124
- // const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
125
-
126
- // if (modelName == 'ActivatedVoucherSnackListResponse') {
127
- // console.debug("ActivatedVoucherSnackListResponse");
128
- // }
129
-
130
- // if (usedTypes.indexOf(modelName) < 0) {
131
- // console.debug(`\tModel SKIP - ${modelName}`);
132
- // continue
133
- // };
134
-
135
- // const swaggerCopmponent = this._swagger.components.schemas[modelName];
136
-
137
- // console.debug(`\tModel - ${modelName}`);
138
-
139
- // let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
140
- // let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
141
-
142
- // models +=
143
- // `
144
- // export ${type} ${modelName} {${content}
145
- // }
146
- // `;
147
- // }
148
-
149
- // }
150
-
151
- // usedMultiPart.forEach(apiName => {
152
- // const swaggerMethod = this._swagger.paths[apiName];
153
- // const method = Object.getOwnPropertyNames(swaggerMethod)[0];
154
- // const swaggerMethodInfo = swaggerMethod[method];
155
-
156
- // let type = 'class';
157
- // let modelName = this.getApiNameNormalized(apiName);
158
- // let content = this.retrieveSchemaProperties(swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData].schema);
159
- // models +=
160
- // `
161
- // export ${type} ${modelName} {${content}
162
- // }
163
- // `;
164
- // });
165
-
166
- // fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
167
- // `${MODEL_PRE}
168
- // ${models}
169
- // ${MODEL_POST}`,
170
- // { flag: 'w' });
171
- // }
172
-
173
- // retrieveParameters(swaggerMethodInfo: SwaggerMethod, apiName: string) {
174
- // if (swaggerMethodInfo.requestBody != null && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null) {
175
- // var modelName = this.getApiNameNormalized(apiName);
176
- // return `request: Models.${modelName}`;
177
- // } else {
178
- // if (swaggerMethodInfo.requestBody != null) {
179
- // return `request: Models.${swaggerMethodInfo.requestBody.content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', '')}`
180
- // }
181
-
182
- // let parameters = ``;
183
- // swaggerMethodInfo.parameters?.filter(x => x.in == 'query').forEach(parameter => {
184
- // if (parameter.schema.$ref != null) {
185
- // parameters += `${this.toFirstLetterLowercase(parameter.name)}: Models.${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
186
- // } else {
187
- // const nativeType = this.getNativeType(parameter.schema);
188
- // const nullable = !parameter.required ? '?' : '';
189
- // parameters += `${this.toFirstLetterLowercase(parameter.name)}${nullable}: ${nativeType}, `;
190
- // }
191
- // });
192
-
193
- // if (parameters.length > 2)
194
- // parameters = parameters.substring(0, parameters.length - 2);
195
-
196
- // return parameters;
197
- // }
198
- // }
199
-
200
- // retrieveQueryParametersDeclaration(swaggerMethodInfo: SwaggerMethod) {
201
- // if (swaggerMethodInfo.requestBody != null) return ``;
202
-
203
- // let filteredParameters = swaggerMethodInfo.parameters?.filter(x => x.in == 'query');
204
- // if (filteredParameters == null || filteredParameters == undefined || filteredParameters.length == 0) return ``;
205
-
206
- // let parameters = ``;
207
- // filteredParameters.forEach(parameter => {
208
- // if (parameter.schema.$ref != null) {
209
- // if (this.isEnum(parameter.schema.$ref) != null) {
210
- // parameters += this.retrieveQueryParametersDeclarationStatement(parameter);
211
- // } else {
212
- // throw new Error("retrieveQueryParameters unmanaged parameter.schema.$ref");
213
- // }
214
- // } else {
215
- // parameters += this.retrieveQueryParametersDeclarationStatement(parameter);
216
- // }
217
- // });
218
-
219
- // // if (parameters.length > 2)
220
- // // parameters = parameters.substring(0, parameters.length - 1);
221
-
222
- // return parameters;
223
- // }
224
-
225
- // retrieveQueryParametersDeclarationStatement(parameter: SwaggerParameter) {
226
- // let paramName = this.toFirstLetterLowercase(parameter.name);
227
- // if (!parameter.required) {
228
- // if (this.isDate(parameter.schema)) {
229
- // return `let ${paramName}Param: string = ${paramName} != null && ${paramName} != undefined && ${paramName}.isValid() ? encodeURIComponent(this._momentToString(${paramName})) : '';
230
- // `;
231
- // } else {
232
- // return `let ${paramName}Param: string = ${paramName} != null && ${paramName} != undefined ? encodeURIComponent('' + ${paramName}) : '';
233
- // `;
234
- // }
235
- // } else {
236
- // if (this.isDate(parameter.schema)) {
237
- // return `let ${paramName}Param: string = encodeURIComponent(this._momentToString(${paramName}));
238
- // `;
239
- // } else {
240
- // return `let ${paramName}Param: string = encodeURIComponent('' + ${paramName});
241
- // `;
242
- // }
243
- // }
244
- // }
245
-
246
- // retrieveQueryParameters(swaggerMethodInfo: SwaggerMethod) {
247
- // if (swaggerMethodInfo.requestBody != null) return ``;
248
-
249
- // let filteredParameters = swaggerMethodInfo.parameters?.filter(x => x.in == 'query');
250
- // if (filteredParameters == null || filteredParameters == undefined || filteredParameters.length == 0) return ``;
251
-
252
- // let parameters = `?`;
253
- // filteredParameters.forEach(parameter => {
254
- // if (parameter.schema.$ref != null) {
255
- // if (this.isEnum(parameter.schema.$ref) != null) {
256
- // parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;;
257
- // } else {
258
- // throw new Error("retrieveQueryParameters unmanaged parameter.schema.$ref");
259
- // }
260
- // } else {
261
- // parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;
262
- // }
263
- // });
264
-
265
- // if (parameters.length > 1)
266
- // parameters = parameters.substring(0, parameters.length - 1);
267
-
268
- // return parameters;
269
- // }
270
-
271
- // retrieveReturnType(swaggerMethodInfo: SwaggerMethod) {
272
- // if (swaggerMethodInfo.responses[200] == null)
273
- // return 'void';
274
-
275
- // try {
276
- // if (swaggerMethodInfo.responses[200].content[contentTypeApplicationJson].schema.$ref != null)
277
- // return `Models.${this.getObjectName(swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema.$ref)}`;
278
- // } catch (error) {
279
- // const errorMessage = "\t\tAttenzione forse hai dimenticato IActionResult e non hai tipizzato il tipo restituito dal servizio";
280
- // console.error(`%c${errorMessage}`, 'color: red');
281
- // throw new Error(errorMessage);
282
- // }
283
-
284
- // if (swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema.type != null)
285
- // return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema);
286
-
287
- // console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
288
- // throw new Error("unmanaged swaggerMethodInfo");
289
- // }
290
-
291
- // retrieveType(swaggerComponentProperty: SwaggerComponentProperty) {
292
- // if (swaggerComponentProperty.$ref != null)
293
- // return swaggerComponentProperty.$ref.replace('#/components/schemas/', '');
294
-
295
- // if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
296
- // if (swaggerComponentProperty.items.$ref != null)
297
- // return `${this.getObjectName(swaggerComponentProperty.items.$ref)}[]`;
298
- // else
299
- // return this.getNativeType(swaggerComponentProperty);
300
-
301
- // if (swaggerComponentProperty.type != null)
302
- // return this.getNativeType(swaggerComponentProperty);
303
-
304
- // if (swaggerComponentProperty.type == null)
305
- // return '';
306
-
307
- // console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
308
- // throw new Error("unmanaged swaggerMethodInfo");
309
- // }
310
-
311
- // parametrizeObject(objectName: string) {
312
- // let component = this._swagger.components.schemas[this.getObjectName(objectName)];
313
-
314
- // if (component == null || component.properties == null) return ``;
315
-
316
- // console.debug(component.properties);
317
-
318
- // return ``;
319
- // }
320
-
321
- // isEnum(objectName: string) {
322
- // let component = this._swagger.components.schemas[this.getObjectName(objectName)];
323
- // return component.enum != null;
324
- // }
325
-
326
- // isDate(schema: SwaggerSchema) {
327
- // return schema.type == 'string' && schema.format == 'date-time';
328
- // }
329
-
330
- // getObjectName(ref: string) {
331
- // return ref.replace('#/components/schemas/', '');
332
- // }
333
-
334
- // retrieveObjectContent(name: string, swaggerComponent: SwaggerComponent) {
335
- // if (swaggerComponent.type == 'object')
336
- // return this.retrieveComponentProperties(swaggerComponent);
337
- // else if (swaggerComponent.type == 'integer')
338
- // return this.retrieveEnumValues(name, swaggerComponent);
339
- // }
340
-
341
- // retrieveComponentProperties(swaggerCopmponent: SwaggerComponent) {
342
- // if (swaggerCopmponent.properties == null) return ``;
343
-
344
- // let properties = ``;
345
- // for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
346
- // const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
347
- // properties +=
348
- // `
349
- // ${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`
350
- // }
351
-
352
- // return properties;
353
- // }
354
-
355
- // retrieveSchemaProperties(schema: SwaggerSchema) {
356
- // if (schema.properties == null) return ``;
357
-
358
- // let properties = ``;
359
- // for (let j = 0; j < Object.getOwnPropertyNames(schema.properties).length; j++) {
360
- // let propertyName = Object.getOwnPropertyNames(schema.properties)[j];
361
- // properties +=
362
- // `
363
- // ${this.toFirstLetterLowercase(propertyName)}: ${this.retrieveType(schema.properties[propertyName])} | undefined;`
364
- // }
365
-
366
- // return properties;
367
- // }
368
-
369
- // retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
370
- // if (swaggerCopmponent.enum == null) return ``;
371
-
372
- // let properties = ``;
373
- // for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
374
- // const name = swaggerCopmponent.enum[index].split('-')[0].trim();
375
- // const value = swaggerCopmponent.enum[index].split('-')[1].trim();
376
- // properties +=
377
- // `
378
- // ${name} = ${value},`;
379
- // }
380
-
381
- // return properties;
382
- // }
383
-
384
- // retrieveNestedObjects(usedTypes: string[]) {
385
- // for (let i = 0; i < usedTypes.length; i++) {
386
- // const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
387
- // // const name = usedTypes[i]
388
- // // const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
389
- // this.retrieveNestedObjectsRecursive(swaggerCopmponent, usedTypes);
390
- // }
391
- // }
392
-
393
- // retrieveNestedObjectsRecursive(swaggerComponent: SwaggerComponent, usedTypes: string[]) {
394
- // if (!swaggerComponent.properties) return;
395
-
396
- // for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
397
- // const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
398
- // let nestedUsedType = '';
399
- // if (swaggerComponent.properties[propertyName].$ref != null) {
400
- // nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
401
- // } else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
402
- // nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
403
- // }
404
-
405
- // if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
406
- // usedTypes.push(nestedUsedType);
407
- // let nested = this._swagger.components.schemas[nestedUsedType];
408
- // this.retrieveNestedObjectsRecursive(nested, usedTypes);
409
- // }
410
- // }
411
- // }
412
-
413
- // getNativeType(schema: SwaggerSchema): string {
414
- // let nativeType = 'n.d.';
415
-
416
- // if (schema.type == 'array') {
417
- // nativeType = this.getNativeType(schema.items);
418
- // nativeType += '[]';
419
- // } else {
420
- // if (schema.type == 'integer') nativeType = 'number';
421
- // if (schema.type == 'string' && schema.format == null) nativeType = 'string';
422
- // if (schema.type == 'string' && schema.format == 'date-time') nativeType = 'moment.Moment';
423
- // if (schema.type == 'string' && schema.format == 'uuid') nativeType = 'string';
424
- // if (schema.type == 'string' && schema.format == 'binary') nativeType = 'File';
425
- // if (schema.type == 'number') nativeType = 'number';
426
- // if (schema.type == 'boolean') nativeType = 'boolean';
427
- // if (schema.type == 'object') nativeType = 'any';
428
- // }
429
-
430
- // if (nativeType.indexOf('n.d') == -1) {
431
- // return nativeType;
432
- // } else {
433
- // console.error("unmanaged schema type", schema);
434
- // throw new Error("unmanaged schema");
435
- // }
436
- // }
437
-
438
- // getApiNameNormalized(apiName: string) {
439
- // let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
440
-
441
- // if (normalizedApiName.charAt(0) == '_') {
442
- // normalizedApiName = normalizedApiName.slice(1);
443
- // }
444
- // return this.toFirstLetterLowercase(normalizedApiName);
445
- // }
446
-
447
- // toFirstLetterLowercase(value: string) {
448
- // return value.charAt(0).toLowerCase() + value.slice(1);
449
- // }
1
+ // import fs from 'fs';
2
+ // import { SwaggerComponent } from './models/swagger/swagger-component.js';
3
+ // import { Swagger, SwaggerParameter } from './models/swagger/swagger.js';
4
+ // import { SwaggerMethod } from './models/swagger/swagger-method.js';
5
+ // import { SwaggerComponentProperty } from './models/swagger/swagger-component-property.js';
6
+ // import { SwaggerSchema } from './models/swagger/swagger-schema.js';
7
+ // import { API_PRE, API_POST } from './api.constants.js';
8
+ // import { MODEL_POST, MODEL_PRE } from './model.constants.js';
9
+
10
+ // const contentTypeApplicationJson = 'application/json';
11
+ // const contentTypeMultipartFormData = 'multipart/form-data';
12
+
13
+ // export class GeneratorOld {
14
+ // private _swagger: Swagger;
15
+ // private _outputDirectory: string;
16
+ // private _outputFormat: 'angular' | 'next';
17
+
18
+ // constructor(swagger: Swagger, outputDirectory: string, outputFormat: 'angular' | 'next') {
19
+ // this._swagger = swagger;
20
+ // this._outputDirectory = outputDirectory;
21
+ // this._outputFormat = outputFormat
22
+ // }
23
+
24
+ // generateApi() {
25
+ // console.debug(`Start autogeneration Apis`);
26
+
27
+ // let apiMethods = ``;
28
+ // for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
29
+ // const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
30
+ // const swaggerMethod = this._swagger.paths[apiName];
31
+ // const method = Object.getOwnPropertyNames(swaggerMethod)[0];
32
+ // const swaggerMethodInfo = swaggerMethod[method];
33
+ // console.debug(`\tAPI - ${apiName} - ${method}`);
34
+
35
+ // let parametersString = this.retrieveParameters(swaggerMethodInfo, apiName);
36
+ // let queryParametersDeclaration = this.retrieveQueryParametersDeclaration(swaggerMethodInfo);
37
+ // let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
38
+ // let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
39
+ // if (returnTypeString == null) returnTypeString = 'void';
40
+ // let prepareRequestString = ``; //request = this._handleRequest(request);
41
+ // let haveRequest = swaggerMethodInfo.requestBody != null;
42
+ // let isMultiPart = haveRequest && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null;
43
+ // let httpOptions = 'httpOptions';
44
+
45
+ // if (haveRequest && !isMultiPart) {
46
+ // prepareRequestString = `const wrappedRequest = this._handleRequest(request);
47
+ // `;
48
+ // } else if (isMultiPart) {
49
+ // prepareRequestString = `const wrappedRequest = this._handleMultipart(request);
50
+ // `;
51
+ // httpOptions = `httpOptionsMultipart`;
52
+ // }
53
+
54
+ // apiMethods +=
55
+ // `
56
+ // public ${this.getApiNameNormalized(apiName)}(${parametersString}): Observable<${returnTypeString}> {
57
+ // ${queryParametersDeclaration}${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
58
+ // .pipe(
59
+ // map(x => this._handleResponse(x)),
60
+ // catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
61
+ // );
62
+ // }
63
+ // `;
64
+ // }
65
+
66
+ // fs.writeFileSync(this._outputDirectory + "/api.autogenerated.ts",
67
+ // `${API_PRE}
68
+ // ${apiMethods}
69
+ // ${API_POST}`,
70
+ // { flag: 'w' });
71
+ // }
72
+
73
+ // generateModel() {
74
+ // let usedTypes: string[] = [];
75
+ // let usedMultiPart: string[] = [];
76
+
77
+ // for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
78
+ // const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
79
+ // const swaggerMethod = this._swagger.paths[apiName];
80
+ // const method = Object.getOwnPropertyNames(swaggerMethod)[0];
81
+ // const swaggerMethodInfo = swaggerMethod[method];
82
+
83
+ // if (apiName == "/api/v{version}/TicketFile/Create") {
84
+ // debugger
85
+ // }
86
+
87
+ // let parametersRefType = swaggerMethodInfo.parameters?.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
88
+ // if (parametersRefType) {
89
+ // usedTypes = usedTypes.concat(parametersRefType);
90
+
91
+ // if (swaggerMethodInfo.responses[200].content[contentTypeApplicationJson]?.schema.$ref != null) {
92
+ // usedTypes.push(swaggerMethodInfo.responses[200].content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''));
93
+ // }
94
+
95
+ // if (swaggerMethodInfo.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref) {
96
+ // usedTypes.push(swaggerMethodInfo.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref.replace('#/components/schemas/', ''));
97
+ // }
98
+
99
+ // if (swaggerMethodInfo.requestBody?.content[contentTypeMultipartFormData]?.schema != null) {
100
+ // usedMultiPart.push(apiName);
101
+ // }
102
+ // }
103
+ // }
104
+
105
+ // this.retrieveNestedObjects(usedTypes);
106
+
107
+ // usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
108
+
109
+ // // usedTypes = usedTypes.filter((value, index, array) => {
110
+ // // array.indexOf(value) === index;
111
+ // // });
112
+ // // usedTypes.forEach(element => {
113
+ // // console.debug(element);
114
+ // // });
115
+
116
+ // console.debug(`Start autogeneration Models`);
117
+ // let models = ``;
118
+ // if (this._swagger.components != null
119
+ // && this._swagger.components != undefined
120
+ // && this._swagger.components.schemas != null
121
+ // && this._swagger.components.schemas != undefined) {
122
+
123
+ // for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
124
+ // const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
125
+
126
+ // if (modelName == 'ActivatedVoucherSnackListResponse') {
127
+ // console.debug("ActivatedVoucherSnackListResponse");
128
+ // }
129
+
130
+ // if (usedTypes.indexOf(modelName) < 0) {
131
+ // console.debug(`\tModel SKIP - ${modelName}`);
132
+ // continue
133
+ // };
134
+
135
+ // const swaggerCopmponent = this._swagger.components.schemas[modelName];
136
+
137
+ // console.debug(`\tModel - ${modelName}`);
138
+
139
+ // let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
140
+ // let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
141
+
142
+ // models +=
143
+ // `
144
+ // export ${type} ${modelName} {${content}
145
+ // }
146
+ // `;
147
+ // }
148
+
149
+ // }
150
+
151
+ // usedMultiPart.forEach(apiName => {
152
+ // const swaggerMethod = this._swagger.paths[apiName];
153
+ // const method = Object.getOwnPropertyNames(swaggerMethod)[0];
154
+ // const swaggerMethodInfo = swaggerMethod[method];
155
+
156
+ // let type = 'class';
157
+ // let modelName = this.getApiNameNormalized(apiName);
158
+ // let content = this.retrieveSchemaProperties(swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData].schema);
159
+ // models +=
160
+ // `
161
+ // export ${type} ${modelName} {${content}
162
+ // }
163
+ // `;
164
+ // });
165
+
166
+ // fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
167
+ // `${MODEL_PRE}
168
+ // ${models}
169
+ // ${MODEL_POST}`,
170
+ // { flag: 'w' });
171
+ // }
172
+
173
+ // retrieveParameters(swaggerMethodInfo: SwaggerMethod, apiName: string) {
174
+ // if (swaggerMethodInfo.requestBody != null && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null) {
175
+ // var modelName = this.getApiNameNormalized(apiName);
176
+ // return `request: Models.${modelName}`;
177
+ // } else {
178
+ // if (swaggerMethodInfo.requestBody != null) {
179
+ // return `request: Models.${swaggerMethodInfo.requestBody.content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', '')}`
180
+ // }
181
+
182
+ // let parameters = ``;
183
+ // swaggerMethodInfo.parameters?.filter(x => x.in == 'query').forEach(parameter => {
184
+ // if (parameter.schema.$ref != null) {
185
+ // parameters += `${this.toFirstLetterLowercase(parameter.name)}: Models.${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
186
+ // } else {
187
+ // const nativeType = this.getNativeType(parameter.schema);
188
+ // const nullable = !parameter.required ? '?' : '';
189
+ // parameters += `${this.toFirstLetterLowercase(parameter.name)}${nullable}: ${nativeType}, `;
190
+ // }
191
+ // });
192
+
193
+ // if (parameters.length > 2)
194
+ // parameters = parameters.substring(0, parameters.length - 2);
195
+
196
+ // return parameters;
197
+ // }
198
+ // }
199
+
200
+ // retrieveQueryParametersDeclaration(swaggerMethodInfo: SwaggerMethod) {
201
+ // if (swaggerMethodInfo.requestBody != null) return ``;
202
+
203
+ // let filteredParameters = swaggerMethodInfo.parameters?.filter(x => x.in == 'query');
204
+ // if (filteredParameters == null || filteredParameters == undefined || filteredParameters.length == 0) return ``;
205
+
206
+ // let parameters = ``;
207
+ // filteredParameters.forEach(parameter => {
208
+ // if (parameter.schema.$ref != null) {
209
+ // if (this.isEnum(parameter.schema.$ref) != null) {
210
+ // parameters += this.retrieveQueryParametersDeclarationStatement(parameter);
211
+ // } else {
212
+ // throw new Error("retrieveQueryParameters unmanaged parameter.schema.$ref");
213
+ // }
214
+ // } else {
215
+ // parameters += this.retrieveQueryParametersDeclarationStatement(parameter);
216
+ // }
217
+ // });
218
+
219
+ // // if (parameters.length > 2)
220
+ // // parameters = parameters.substring(0, parameters.length - 1);
221
+
222
+ // return parameters;
223
+ // }
224
+
225
+ // retrieveQueryParametersDeclarationStatement(parameter: SwaggerParameter) {
226
+ // let paramName = this.toFirstLetterLowercase(parameter.name);
227
+ // if (!parameter.required) {
228
+ // if (this.isDate(parameter.schema)) {
229
+ // return `let ${paramName}Param: string = ${paramName} != null && ${paramName} != undefined && ${paramName}.isValid() ? encodeURIComponent(this._momentToString(${paramName})) : '';
230
+ // `;
231
+ // } else {
232
+ // return `let ${paramName}Param: string = ${paramName} != null && ${paramName} != undefined ? encodeURIComponent('' + ${paramName}) : '';
233
+ // `;
234
+ // }
235
+ // } else {
236
+ // if (this.isDate(parameter.schema)) {
237
+ // return `let ${paramName}Param: string = encodeURIComponent(this._momentToString(${paramName}));
238
+ // `;
239
+ // } else {
240
+ // return `let ${paramName}Param: string = encodeURIComponent('' + ${paramName});
241
+ // `;
242
+ // }
243
+ // }
244
+ // }
245
+
246
+ // retrieveQueryParameters(swaggerMethodInfo: SwaggerMethod) {
247
+ // if (swaggerMethodInfo.requestBody != null) return ``;
248
+
249
+ // let filteredParameters = swaggerMethodInfo.parameters?.filter(x => x.in == 'query');
250
+ // if (filteredParameters == null || filteredParameters == undefined || filteredParameters.length == 0) return ``;
251
+
252
+ // let parameters = `?`;
253
+ // filteredParameters.forEach(parameter => {
254
+ // if (parameter.schema.$ref != null) {
255
+ // if (this.isEnum(parameter.schema.$ref) != null) {
256
+ // parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;;
257
+ // } else {
258
+ // throw new Error("retrieveQueryParameters unmanaged parameter.schema.$ref");
259
+ // }
260
+ // } else {
261
+ // parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;
262
+ // }
263
+ // });
264
+
265
+ // if (parameters.length > 1)
266
+ // parameters = parameters.substring(0, parameters.length - 1);
267
+
268
+ // return parameters;
269
+ // }
270
+
271
+ // retrieveReturnType(swaggerMethodInfo: SwaggerMethod) {
272
+ // if (swaggerMethodInfo.responses[200] == null)
273
+ // return 'void';
274
+
275
+ // try {
276
+ // if (swaggerMethodInfo.responses[200].content[contentTypeApplicationJson].schema.$ref != null)
277
+ // return `Models.${this.getObjectName(swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema.$ref)}`;
278
+ // } catch (error) {
279
+ // const errorMessage = "\t\tAttenzione forse hai dimenticato IActionResult e non hai tipizzato il tipo restituito dal servizio";
280
+ // console.error(`%c${errorMessage}`, 'color: red');
281
+ // throw new Error(errorMessage);
282
+ // }
283
+
284
+ // if (swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema.type != null)
285
+ // return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema);
286
+
287
+ // console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
288
+ // throw new Error("unmanaged swaggerMethodInfo");
289
+ // }
290
+
291
+ // retrieveType(swaggerComponentProperty: SwaggerComponentProperty) {
292
+ // if (swaggerComponentProperty.$ref != null)
293
+ // return swaggerComponentProperty.$ref.replace('#/components/schemas/', '');
294
+
295
+ // if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
296
+ // if (swaggerComponentProperty.items.$ref != null)
297
+ // return `${this.getObjectName(swaggerComponentProperty.items.$ref)}[]`;
298
+ // else
299
+ // return this.getNativeType(swaggerComponentProperty);
300
+
301
+ // if (swaggerComponentProperty.type != null)
302
+ // return this.getNativeType(swaggerComponentProperty);
303
+
304
+ // if (swaggerComponentProperty.type == null)
305
+ // return '';
306
+
307
+ // console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
308
+ // throw new Error("unmanaged swaggerMethodInfo");
309
+ // }
310
+
311
+ // parametrizeObject(objectName: string) {
312
+ // let component = this._swagger.components.schemas[this.getObjectName(objectName)];
313
+
314
+ // if (component == null || component.properties == null) return ``;
315
+
316
+ // console.debug(component.properties);
317
+
318
+ // return ``;
319
+ // }
320
+
321
+ // isEnum(objectName: string) {
322
+ // let component = this._swagger.components.schemas[this.getObjectName(objectName)];
323
+ // return component.enum != null;
324
+ // }
325
+
326
+ // isDate(schema: SwaggerSchema) {
327
+ // return schema.type == 'string' && schema.format == 'date-time';
328
+ // }
329
+
330
+ // getObjectName(ref: string) {
331
+ // return ref.replace('#/components/schemas/', '');
332
+ // }
333
+
334
+ // retrieveObjectContent(name: string, swaggerComponent: SwaggerComponent) {
335
+ // if (swaggerComponent.type == 'object')
336
+ // return this.retrieveComponentProperties(swaggerComponent);
337
+ // else if (swaggerComponent.type == 'integer')
338
+ // return this.retrieveEnumValues(name, swaggerComponent);
339
+ // }
340
+
341
+ // retrieveComponentProperties(swaggerCopmponent: SwaggerComponent) {
342
+ // if (swaggerCopmponent.properties == null) return ``;
343
+
344
+ // let properties = ``;
345
+ // for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
346
+ // const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
347
+ // properties +=
348
+ // `
349
+ // ${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`
350
+ // }
351
+
352
+ // return properties;
353
+ // }
354
+
355
+ // retrieveSchemaProperties(schema: SwaggerSchema) {
356
+ // if (schema.properties == null) return ``;
357
+
358
+ // let properties = ``;
359
+ // for (let j = 0; j < Object.getOwnPropertyNames(schema.properties).length; j++) {
360
+ // let propertyName = Object.getOwnPropertyNames(schema.properties)[j];
361
+ // properties +=
362
+ // `
363
+ // ${this.toFirstLetterLowercase(propertyName)}: ${this.retrieveType(schema.properties[propertyName])} | undefined;`
364
+ // }
365
+
366
+ // return properties;
367
+ // }
368
+
369
+ // retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
370
+ // if (swaggerCopmponent.enum == null) return ``;
371
+
372
+ // let properties = ``;
373
+ // for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
374
+ // const name = swaggerCopmponent.enum[index].split('-')[0].trim();
375
+ // const value = swaggerCopmponent.enum[index].split('-')[1].trim();
376
+ // properties +=
377
+ // `
378
+ // ${name} = ${value},`;
379
+ // }
380
+
381
+ // return properties;
382
+ // }
383
+
384
+ // retrieveNestedObjects(usedTypes: string[]) {
385
+ // for (let i = 0; i < usedTypes.length; i++) {
386
+ // const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
387
+ // // const name = usedTypes[i]
388
+ // // const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
389
+ // this.retrieveNestedObjectsRecursive(swaggerCopmponent, usedTypes);
390
+ // }
391
+ // }
392
+
393
+ // retrieveNestedObjectsRecursive(swaggerComponent: SwaggerComponent, usedTypes: string[]) {
394
+ // if (!swaggerComponent.properties) return;
395
+
396
+ // for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
397
+ // const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
398
+ // let nestedUsedType = '';
399
+ // if (swaggerComponent.properties[propertyName].$ref != null) {
400
+ // nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
401
+ // } else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
402
+ // nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
403
+ // }
404
+
405
+ // if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
406
+ // usedTypes.push(nestedUsedType);
407
+ // let nested = this._swagger.components.schemas[nestedUsedType];
408
+ // this.retrieveNestedObjectsRecursive(nested, usedTypes);
409
+ // }
410
+ // }
411
+ // }
412
+
413
+ // getNativeType(schema: SwaggerSchema): string {
414
+ // let nativeType = 'n.d.';
415
+
416
+ // if (schema.type == 'array') {
417
+ // nativeType = this.getNativeType(schema.items);
418
+ // nativeType += '[]';
419
+ // } else {
420
+ // if (schema.type == 'integer') nativeType = 'number';
421
+ // if (schema.type == 'string' && schema.format == null) nativeType = 'string';
422
+ // if (schema.type == 'string' && schema.format == 'date-time') nativeType = 'moment.Moment';
423
+ // if (schema.type == 'string' && schema.format == 'uuid') nativeType = 'string';
424
+ // if (schema.type == 'string' && schema.format == 'binary') nativeType = 'File';
425
+ // if (schema.type == 'number') nativeType = 'number';
426
+ // if (schema.type == 'boolean') nativeType = 'boolean';
427
+ // if (schema.type == 'object') nativeType = 'any';
428
+ // }
429
+
430
+ // if (nativeType.indexOf('n.d') == -1) {
431
+ // return nativeType;
432
+ // } else {
433
+ // console.error("unmanaged schema type", schema);
434
+ // throw new Error("unmanaged schema");
435
+ // }
436
+ // }
437
+
438
+ // getApiNameNormalized(apiName: string) {
439
+ // let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
440
+
441
+ // if (normalizedApiName.charAt(0) == '_') {
442
+ // normalizedApiName = normalizedApiName.slice(1);
443
+ // }
444
+ // return this.toFirstLetterLowercase(normalizedApiName);
445
+ // }
446
+
447
+ // toFirstLetterLowercase(value: string) {
448
+ // return value.charAt(0).toLowerCase() + value.slice(1);
449
+ // }
450
450
  // }