@devlearning/swagger-generator 0.0.39 → 1.0.0
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/.vscode/launch.json +8 -2
- package/autogeneration/output/api.autogenerated.ts +2144 -5
- package/autogeneration/output/model.autogenerated.ts +2789 -0
- package/dist/generator-old.js +369 -0
- package/dist/generator.js +217 -214
- package/dist/generators-writers/angular/api-angular-writer.js +111 -0
- package/dist/generators-writers/angular/constants.js +28 -0
- package/dist/generators-writers/angular/model-angular-writer.js +42 -0
- package/dist/generators-writers/nextjs/api-nextjs-writer.js +127 -0
- package/dist/generators-writers/nextjs/constants.js +4 -0
- package/dist/generators-writers/nextjs/model-nextjs-writer.js +41 -0
- package/dist/generators-writers/utils.js +20 -0
- package/dist/index.js +7 -5
- package/dist/model.constants.js +1 -4
- package/dist/models/api-dto.js +1 -0
- package/dist/models/enum-value-dto.js +1 -0
- package/dist/models/model-dto.js +1 -0
- package/dist/models/parameter-dto.js +1 -0
- package/dist/models/property-dto.js +1 -0
- package/dist/models/swagger/swagger-component-property.js +1 -0
- package/dist/models/swagger/swagger-component.js +1 -0
- package/dist/models/swagger/swagger-content.js +1 -0
- package/dist/models/swagger/swagger-info.js +1 -0
- package/dist/models/swagger/swagger-method.js +1 -0
- package/dist/models/swagger/swagger-schema.js +1 -0
- package/dist/models/swagger/swagger.js +1 -0
- package/dist/models/type-dto.js +1 -0
- package/package.json +6 -3
- package/src/generator-old.ts +450 -0
- package/src/generator.ts +238 -228
- package/src/generators-writers/angular/api-angular-writer.ts +139 -0
- package/src/generators-writers/angular/constants.ts +36 -0
- package/src/generators-writers/angular/model-angular-writer.ts +60 -0
- package/src/generators-writers/nextjs/api-nextjs-writer.ts +157 -0
- package/src/generators-writers/nextjs/constants.ts +5 -0
- package/src/generators-writers/nextjs/model-nextjs-writer.ts +59 -0
- package/src/generators-writers/utils.ts +27 -0
- package/src/index.ts +7 -5
- package/src/model.constants.ts +0 -7
- package/src/models/api-dto.ts +17 -0
- package/src/models/enum-value-dto.ts +4 -0
- package/src/models/model-dto.ts +9 -0
- package/src/models/parameter-dto.ts +8 -0
- package/src/models/property-dto.ts +5 -0
- package/src/models/type-dto.ts +4 -0
- package/src/swagger-downloader.ts +1 -1
- package/tsconfig.json +1 -1
- /package/src/models/{swagger-component-property.ts → swagger/swagger-component-property.ts} +0 -0
- /package/src/models/{swagger-component.ts → swagger/swagger-component.ts} +0 -0
- /package/src/models/{swagger-content.ts → swagger/swagger-content.ts} +0 -0
- /package/src/models/{swagger-info.ts → swagger/swagger-info.ts} +0 -0
- /package/src/models/{swagger-method.ts → swagger/swagger-method.ts} +0 -0
- /package/src/models/{swagger-schema.ts → swagger/swagger-schema.ts} +0 -0
- /package/src/models/{swagger.ts → swagger/swagger.ts} +0 -0
package/src/generator.ts
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
-
import { SwaggerComponent } from './models/swagger-component.js';
|
|
3
|
-
import { Swagger
|
|
4
|
-
import { SwaggerMethod } from './models/swagger-method.js';
|
|
5
|
-
import { SwaggerComponentProperty } from './models/swagger-component-property.js';
|
|
6
|
-
import { SwaggerSchema } from './models/swagger-schema.js';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
2
|
+
import { SwaggerComponent } from './models/swagger/swagger-component.js';
|
|
3
|
+
import { Swagger } 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 { ApiDto } from './models/api-dto.js';
|
|
8
|
+
import { TypeDto } from './models/type-dto.js';
|
|
9
|
+
import { ParameterDto } from './models/parameter-dto.js';
|
|
10
|
+
import { ApiNextJsWriter } from './generators-writers/nextjs/api-nextjs-writer.js';
|
|
11
|
+
import { ApiAngularWriter } from './generators-writers/angular/api-angular-writer.js';
|
|
12
|
+
import { ModelDto } from './models/model-dto.js';
|
|
13
|
+
import { SingleBar, Presets } from 'cli-progress';
|
|
14
|
+
import { ModelNextJsWriter } from './generators-writers/nextjs/model-nextjs-writer.js';
|
|
15
|
+
import { ModelAngularWriter } from './generators-writers/angular/model-angular-writer.js';
|
|
16
|
+
import { PropertyDto } from './models/property-dto.js';
|
|
17
|
+
import { EnumValueDto } from './models/enum-value-dto.js';
|
|
9
18
|
|
|
10
19
|
const contentTypeApplicationJson = 'application/json';
|
|
11
20
|
const contentTypeMultipartFormData = 'multipart/form-data';
|
|
@@ -13,88 +22,103 @@ const contentTypeMultipartFormData = 'multipart/form-data';
|
|
|
13
22
|
export class Generator {
|
|
14
23
|
private _swagger: Swagger;
|
|
15
24
|
private _outputDirectory: string;
|
|
16
|
-
|
|
17
|
-
|
|
25
|
+
private _outputFormat: 'angular' | 'next';
|
|
26
|
+
private _dateLibrary: 'moment' | 'date-fns';
|
|
27
|
+
|
|
28
|
+
private _apis: ApiDto[] = [];
|
|
29
|
+
private _models: ModelDto[] = [];
|
|
30
|
+
|
|
31
|
+
private _barApis = new SingleBar({
|
|
32
|
+
format: '{bar} {percentage}% | {message} {value}/{total} Elapsed: {duration_formatted}', // Formato della barra
|
|
33
|
+
barCompleteChar: '\u2588', // Carattere pieno per la barra
|
|
34
|
+
barIncompleteChar: '\u2591', // Carattere vuoto per la parte non completata
|
|
35
|
+
hideCursor: true, // Nascondi il cursore durante l'esecuzione
|
|
36
|
+
barsize: 20,
|
|
37
|
+
}, Presets.shades_classic);
|
|
38
|
+
|
|
39
|
+
private _barModels = new SingleBar({
|
|
40
|
+
format: '{bar} {percentage}% | {message} {value}/{total} Elapsed: {duration_formatted}', // Formato della barra
|
|
41
|
+
barCompleteChar: '\u2588', // Carattere pieno per la barra
|
|
42
|
+
barIncompleteChar: '\u2591', // Carattere vuoto per la parte non completata
|
|
43
|
+
hideCursor: true, // Nascondi il cursore durante l'esecuzione
|
|
44
|
+
barsize: 20,
|
|
45
|
+
}, Presets.shades_classic);
|
|
46
|
+
|
|
47
|
+
constructor(swagger: Swagger, outputDirectory: string, outputFormat: 'angular' | 'next', dateLibrary: 'moment' | 'date-fns') {
|
|
18
48
|
this._swagger = swagger;
|
|
19
49
|
this._outputDirectory = outputDirectory;
|
|
50
|
+
this._outputFormat = outputFormat;
|
|
51
|
+
this._dateLibrary = dateLibrary;
|
|
20
52
|
}
|
|
21
53
|
|
|
22
|
-
|
|
23
|
-
console.
|
|
54
|
+
generate() {
|
|
55
|
+
console.info('%c[Swagger API Generator] %cStarting to parse Swagger JSON file...', 'color: #4CAF50; font-weight: bold;', 'color: #2196F3;');
|
|
24
56
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
28
|
-
const swaggerMethod = this._swagger.paths[apiName];
|
|
29
|
-
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
30
|
-
const swaggerMethodInfo = swaggerMethod[method];
|
|
31
|
-
console.debug(`\tAPI - ${apiName} - ${method}`);
|
|
32
|
-
|
|
33
|
-
let parametersString = this.retrieveParameters(swaggerMethodInfo, apiName);
|
|
34
|
-
let queryParametersDeclaration = this.retrieveQueryParametersDeclaration(swaggerMethodInfo);
|
|
35
|
-
let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
|
|
36
|
-
let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
|
|
37
|
-
if (returnTypeString == null) returnTypeString = 'void';
|
|
38
|
-
let prepareRequestString = ``; //request = this._handleRequest(request);
|
|
39
|
-
let haveRequest = swaggerMethodInfo.requestBody != null;
|
|
40
|
-
let isMultiPart = haveRequest && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null;
|
|
41
|
-
let httpOptions = 'httpOptions';
|
|
42
|
-
|
|
43
|
-
if (haveRequest && !isMultiPart) {
|
|
44
|
-
prepareRequestString = `const wrappedRequest = this._handleRequest(request);
|
|
45
|
-
`;
|
|
46
|
-
} else if (isMultiPart) {
|
|
47
|
-
prepareRequestString = `const wrappedRequest = this._handleMultipart(request);
|
|
48
|
-
`;
|
|
49
|
-
httpOptions = `httpOptionsMultipart`;
|
|
50
|
-
}
|
|
57
|
+
this.computeApi();
|
|
58
|
+
this.generateApi();
|
|
51
59
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
.pipe(
|
|
57
|
-
map(x => this._handleResponse(x)),
|
|
58
|
-
catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
|
|
59
|
-
);
|
|
60
|
+
this.computeModel();
|
|
61
|
+
this.generateModel();
|
|
62
|
+
|
|
63
|
+
console.info('%c[Swagger Generator] %cSwagger file generated successfully!', 'color: #4CAF50; font-weight: bold;', 'color: #00C853;');
|
|
60
64
|
}
|
|
61
|
-
`;
|
|
62
|
-
}
|
|
63
65
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
{
|
|
66
|
+
computeApi() {
|
|
67
|
+
this._barApis.start(Object.getOwnPropertyNames(this._swagger.paths).length, 0);
|
|
68
|
+
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
69
|
+
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
70
|
+
this._barApis.update(index, { message: `Elaborando api... ${apiName}` });
|
|
71
|
+
const apiSwaggerMethodKey = this._swagger.paths[apiName];
|
|
72
|
+
const apiMethod = Object.getOwnPropertyNames(apiSwaggerMethodKey)[0];
|
|
73
|
+
const apiSwaggerMethod = apiSwaggerMethodKey[apiMethod];
|
|
74
|
+
|
|
75
|
+
// console.debug(`\tAPI - ${apiName} - ${apiMethod}`);
|
|
76
|
+
|
|
77
|
+
let apiDto: ApiDto = {
|
|
78
|
+
name: apiName,
|
|
79
|
+
url: apiName.replace('{version}', '1'),
|
|
80
|
+
method: apiMethod,
|
|
81
|
+
parameters: this.computeParameters(apiName, apiSwaggerMethod),
|
|
82
|
+
returnType: this.computeReturnType(apiSwaggerMethod),
|
|
83
|
+
swaggerMethodKey: apiSwaggerMethodKey,
|
|
84
|
+
swaggerMethod: apiSwaggerMethod,
|
|
85
|
+
haveRequest: apiSwaggerMethod.requestBody != null,
|
|
86
|
+
isMultiPart: apiSwaggerMethod.requestBody != null && apiSwaggerMethod.requestBody.content[contentTypeMultipartFormData] != null,
|
|
87
|
+
};
|
|
88
|
+
this._apis.push(apiDto);
|
|
89
|
+
}
|
|
69
90
|
}
|
|
70
91
|
|
|
71
|
-
|
|
92
|
+
computeModel() {
|
|
93
|
+
this._barModels.start(Object.getOwnPropertyNames(this._swagger.paths).length, 0);
|
|
94
|
+
|
|
72
95
|
let usedTypes: string[] = [];
|
|
73
96
|
let usedMultiPart: string[] = [];
|
|
74
97
|
|
|
75
98
|
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
76
99
|
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
const
|
|
100
|
+
const apiSwaggerMethodKey = this._swagger.paths[apiName];
|
|
101
|
+
const apiMethod = Object.getOwnPropertyNames(apiSwaggerMethodKey)[0];
|
|
102
|
+
const apiSwaggerMethod = apiSwaggerMethodKey[apiMethod];
|
|
80
103
|
|
|
81
|
-
if (apiName == "/api/v{version}/TicketFile/Create") {
|
|
82
|
-
|
|
83
|
-
}
|
|
104
|
+
// if (apiName == "/api/v{version}/TicketFile/Create") {
|
|
105
|
+
// debugger
|
|
106
|
+
// }
|
|
84
107
|
|
|
85
|
-
let parametersRefType =
|
|
108
|
+
let parametersRefType = apiSwaggerMethod.parameters?.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
|
|
86
109
|
if (parametersRefType) {
|
|
87
110
|
usedTypes = usedTypes.concat(parametersRefType);
|
|
111
|
+
this._barModels.update(index, { message: `Elaborando model... ${usedTypes}` });
|
|
88
112
|
|
|
89
|
-
if (
|
|
90
|
-
usedTypes.push(
|
|
113
|
+
if (apiSwaggerMethod.responses[200].content[contentTypeApplicationJson]?.schema.$ref != null) {
|
|
114
|
+
usedTypes.push(apiSwaggerMethod.responses[200].content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''));
|
|
91
115
|
}
|
|
92
116
|
|
|
93
|
-
if (
|
|
94
|
-
usedTypes.push(
|
|
117
|
+
if (apiSwaggerMethod.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref) {
|
|
118
|
+
usedTypes.push(apiSwaggerMethod.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref.replace('#/components/schemas/', ''));
|
|
95
119
|
}
|
|
96
120
|
|
|
97
|
-
if (
|
|
121
|
+
if (apiSwaggerMethod.requestBody?.content[contentTypeMultipartFormData]?.schema != null) {
|
|
98
122
|
usedMultiPart.push(apiName);
|
|
99
123
|
}
|
|
100
124
|
}
|
|
@@ -104,14 +128,6 @@ ${API_POST}`,
|
|
|
104
128
|
|
|
105
129
|
usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
|
|
106
130
|
|
|
107
|
-
// usedTypes = usedTypes.filter((value, index, array) => {
|
|
108
|
-
// array.indexOf(value) === index;
|
|
109
|
-
// });
|
|
110
|
-
// usedTypes.forEach(element => {
|
|
111
|
-
// console.debug(element);
|
|
112
|
-
// });
|
|
113
|
-
|
|
114
|
-
console.debug(`Start autogeneration Models`);
|
|
115
131
|
let models = ``;
|
|
116
132
|
if (this._swagger.components != null
|
|
117
133
|
&& this._swagger.components != undefined
|
|
@@ -121,27 +137,20 @@ ${API_POST}`,
|
|
|
121
137
|
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
|
|
122
138
|
const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
|
|
123
139
|
|
|
124
|
-
if (modelName == 'ActivatedVoucherSnackListResponse') {
|
|
125
|
-
console.debug("ActivatedVoucherSnackListResponse");
|
|
126
|
-
}
|
|
127
|
-
|
|
128
140
|
if (usedTypes.indexOf(modelName) < 0) {
|
|
129
|
-
console.debug(`\tModel SKIP - ${modelName}`);
|
|
141
|
+
// console.debug(`\tModel SKIP - ${modelName}`);
|
|
130
142
|
continue
|
|
131
143
|
};
|
|
132
144
|
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
console.debug(`\tModel - ${modelName}`);
|
|
145
|
+
const swaggerComponent = this._swagger.components.schemas[modelName];
|
|
136
146
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
`;
|
|
147
|
+
// console.debug(`\tModel - ${modelName}`);
|
|
148
|
+
this._models.push({
|
|
149
|
+
modelType: swaggerComponent.type == 'integer' ? 'enum' : 'class',
|
|
150
|
+
name: modelName,
|
|
151
|
+
properties: (swaggerComponent.type == 'object') ? this.retrieveComponentProperties(swaggerComponent) : [],
|
|
152
|
+
enumValues: (swaggerComponent.type == 'integer') ? this.retrieveEnumValues(modelName, swaggerComponent) : [],
|
|
153
|
+
});
|
|
145
154
|
}
|
|
146
155
|
|
|
147
156
|
}
|
|
@@ -151,139 +160,144 @@ export ${type} ${modelName} {${content}
|
|
|
151
160
|
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
152
161
|
const swaggerMethodInfo = swaggerMethod[method];
|
|
153
162
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
`;
|
|
163
|
+
this._models.push({
|
|
164
|
+
modelType: 'class',
|
|
165
|
+
name: this.getApiNameNormalized(apiName),
|
|
166
|
+
properties: this.retrieveComponentProperties(swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData].schema),
|
|
167
|
+
enumValues: [],
|
|
168
|
+
});
|
|
162
169
|
});
|
|
163
|
-
|
|
164
|
-
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
|
|
165
|
-
`${MODEL_PRE}
|
|
166
|
-
${models}
|
|
167
|
-
${MODEL_POST}`,
|
|
168
|
-
{ flag: 'w' });
|
|
169
170
|
}
|
|
170
171
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
let parameters = ``;
|
|
181
|
-
swaggerMethodInfo.parameters?.filter(x => x.in == 'query').forEach(parameter => {
|
|
182
|
-
if (parameter.schema.$ref != null) {
|
|
183
|
-
parameters += `${this.toFirstLetterLowercase(parameter.name)}: Models.${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
184
|
-
} else {
|
|
185
|
-
const nativeType = this.getNativeType(parameter.schema);
|
|
186
|
-
const nullable = !parameter.required ? '?' : '';
|
|
187
|
-
parameters += `${this.toFirstLetterLowercase(parameter.name)}${nullable}: ${nativeType}, `;
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
if (parameters.length > 2)
|
|
192
|
-
parameters = parameters.substring(0, parameters.length - 2);
|
|
193
|
-
|
|
194
|
-
return parameters;
|
|
172
|
+
generateApi() {
|
|
173
|
+
this._barApis.update(this._apis.length, { message: `Inizio scrittura api...` });
|
|
174
|
+
|
|
175
|
+
if (this._outputFormat == 'angular') {
|
|
176
|
+
const apiWriter = new ApiAngularWriter(this._outputDirectory);
|
|
177
|
+
apiWriter.write(this._apis);
|
|
178
|
+
} else if (this._outputFormat == 'next') {
|
|
179
|
+
const apiWriter = new ApiNextJsWriter(this._outputDirectory);
|
|
180
|
+
apiWriter.write(this._apis);
|
|
195
181
|
}
|
|
196
|
-
}
|
|
197
182
|
|
|
198
|
-
|
|
199
|
-
if (swaggerMethodInfo.requestBody != null) return ``;
|
|
183
|
+
this._barApis.update(this._apis.length, { message: `Scrittura api completata` });
|
|
200
184
|
|
|
201
|
-
|
|
202
|
-
|
|
185
|
+
this._barApis.stop();
|
|
186
|
+
}
|
|
203
187
|
|
|
204
|
-
let parameters = ``;
|
|
205
|
-
filteredParameters.forEach(parameter => {
|
|
206
|
-
if (parameter.schema.$ref != null) {
|
|
207
|
-
if (this.isEnum(parameter.schema.$ref) != null) {
|
|
208
|
-
parameters += this.retrieveQueryParametersDeclarationStatement(parameter);
|
|
209
|
-
} else {
|
|
210
|
-
throw new Error("retrieveQueryParameters unmanaged parameter.schema.$ref");
|
|
211
|
-
}
|
|
212
|
-
} else {
|
|
213
|
-
parameters += this.retrieveQueryParametersDeclarationStatement(parameter);
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
188
|
|
|
217
|
-
|
|
218
|
-
|
|
189
|
+
generateModel() {
|
|
190
|
+
this._barModels.update(this._apis.length, { message: `Inizio scrittura model...` });
|
|
191
|
+
|
|
192
|
+
if (this._outputFormat == 'angular') {
|
|
193
|
+
const apiWriter = new ModelAngularWriter(this._outputDirectory);
|
|
194
|
+
apiWriter.write(this._models);
|
|
195
|
+
} else if (this._outputFormat == 'next') {
|
|
196
|
+
const apiWriter = new ModelNextJsWriter(this._outputDirectory);
|
|
197
|
+
apiWriter.write(this._models);
|
|
198
|
+
}
|
|
219
199
|
|
|
220
|
-
|
|
221
|
-
}
|
|
200
|
+
this._barModels.update(this._apis.length, { message: `Scrittura model completata` });
|
|
222
201
|
|
|
223
|
-
|
|
224
|
-
let paramName = this.toFirstLetterLowercase(parameter.name);
|
|
225
|
-
if (!parameter.required) {
|
|
226
|
-
if (this.isDate(parameter.schema)) {
|
|
227
|
-
return `let ${paramName}Param: string = ${paramName} != null && ${paramName} != undefined && ${paramName}.isValid() ? encodeURIComponent(this._momentToString(${paramName})) : '';
|
|
228
|
-
`;
|
|
229
|
-
} else {
|
|
230
|
-
return `let ${paramName}Param: string = ${paramName} != null && ${paramName} != undefined ? encodeURIComponent('' + ${paramName}) : '';
|
|
231
|
-
`;
|
|
232
|
-
}
|
|
233
|
-
} else {
|
|
234
|
-
if (this.isDate(parameter.schema)) {
|
|
235
|
-
return `let ${paramName}Param: string = encodeURIComponent(this._momentToString(${paramName}));
|
|
236
|
-
`;
|
|
237
|
-
} else {
|
|
238
|
-
return `let ${paramName}Param: string = encodeURIComponent('' + ${paramName});
|
|
239
|
-
`;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
202
|
+
this._barModels.stop();
|
|
242
203
|
}
|
|
243
204
|
|
|
244
|
-
|
|
245
|
-
if (
|
|
205
|
+
computeParameters(apiName: string, swaggerMethod: SwaggerMethod | undefined) {
|
|
206
|
+
if (!apiName) return [];
|
|
207
|
+
if (!swaggerMethod || swaggerMethod == null) return [];
|
|
246
208
|
|
|
247
|
-
let
|
|
248
|
-
if (filteredParameters == null || filteredParameters == undefined || filteredParameters.length == 0) return ``;
|
|
209
|
+
let parameters: ParameterDto[] = [];
|
|
249
210
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
211
|
+
if (swaggerMethod.requestBody != null && swaggerMethod.requestBody.content[contentTypeMultipartFormData] != null) {
|
|
212
|
+
var modelName = this.getApiNameNormalized(apiName);
|
|
213
|
+
parameters.push({
|
|
214
|
+
name: 'request',
|
|
215
|
+
typeName: modelName,
|
|
216
|
+
nullable: false,
|
|
217
|
+
isQuery: false,
|
|
218
|
+
isEnum: false,
|
|
219
|
+
});
|
|
220
|
+
} else {
|
|
221
|
+
if (swaggerMethod.requestBody != null) {
|
|
222
|
+
parameters.push({
|
|
223
|
+
name: 'request',
|
|
224
|
+
typeName: swaggerMethod.requestBody.content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''),
|
|
225
|
+
nullable: false,
|
|
226
|
+
isQuery: false,
|
|
227
|
+
isEnum: false,
|
|
228
|
+
});
|
|
258
229
|
} else {
|
|
259
|
-
parameters
|
|
230
|
+
swaggerMethod.parameters?.filter(x => x.in == 'query').forEach(parameter => {
|
|
231
|
+
if (parameter.schema.$ref != null) {
|
|
232
|
+
parameters.push({
|
|
233
|
+
name: this.toFirstLetterLowercase(parameter.name),
|
|
234
|
+
typeName: parameter.schema.$ref.replace('#/components/schemas/', ''),
|
|
235
|
+
nullable: !parameter.required,
|
|
236
|
+
swaggerParameter: parameter,
|
|
237
|
+
isQuery: true,
|
|
238
|
+
isEnum: this.isEnum(parameter.schema.$ref),
|
|
239
|
+
});
|
|
240
|
+
} else {
|
|
241
|
+
parameters.push({
|
|
242
|
+
name: this.toFirstLetterLowercase(parameter.name),
|
|
243
|
+
typeName: this.getNativeType(parameter.schema),
|
|
244
|
+
nullable: !parameter.required,
|
|
245
|
+
swaggerParameter: parameter,
|
|
246
|
+
isQuery: true,
|
|
247
|
+
isEnum: false,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
});
|
|
260
251
|
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (parameters.length > 1)
|
|
264
|
-
parameters = parameters.substring(0, parameters.length - 1);
|
|
265
|
-
|
|
252
|
+
}
|
|
266
253
|
return parameters;
|
|
267
254
|
}
|
|
268
255
|
|
|
269
|
-
|
|
270
|
-
if (
|
|
271
|
-
return
|
|
256
|
+
computeReturnType(swaggerMethod: SwaggerMethod) {
|
|
257
|
+
if (swaggerMethod.responses[200] == null) {
|
|
258
|
+
return <TypeDto>{
|
|
259
|
+
typeName: 'void',
|
|
260
|
+
nullable: false,
|
|
261
|
+
}
|
|
262
|
+
}
|
|
272
263
|
|
|
273
264
|
try {
|
|
274
|
-
if (
|
|
275
|
-
return
|
|
265
|
+
if (swaggerMethod.responses[200].content[contentTypeApplicationJson].schema.$ref != null)
|
|
266
|
+
return <TypeDto>{
|
|
267
|
+
typeName: this.getObjectName(swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema.$ref),
|
|
268
|
+
nullable: false,
|
|
269
|
+
};
|
|
276
270
|
} catch (error) {
|
|
277
271
|
const errorMessage = "\t\tAttenzione forse hai dimenticato IActionResult e non hai tipizzato il tipo restituito dal servizio";
|
|
278
272
|
console.error(`%c${errorMessage}`, 'color: red');
|
|
279
273
|
throw new Error(errorMessage);
|
|
280
274
|
}
|
|
281
275
|
|
|
282
|
-
if (
|
|
283
|
-
|
|
276
|
+
if (swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema.type != null) {
|
|
277
|
+
let schema = swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema;
|
|
278
|
+
if (schema.type == 'array') {
|
|
279
|
+
if (schema.items.$ref != null) {
|
|
280
|
+
return <TypeDto>{
|
|
281
|
+
typeName: `${this.getObjectName(schema.items.$ref)}[]`,
|
|
282
|
+
nullable: false,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
} else {
|
|
286
|
+
return <TypeDto>{
|
|
287
|
+
typeName: this.getNativeType(swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema),
|
|
288
|
+
nullable: false,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
// if (swaggerComponentProperty.items.$ref != null)
|
|
292
|
+
// return `${this.getObjectName(swaggerComponentProperty.items.$ref)}[]`;
|
|
293
|
+
|
|
284
294
|
|
|
285
|
-
|
|
286
|
-
|
|
295
|
+
} else {
|
|
296
|
+
return <TypeDto>{
|
|
297
|
+
typeName: this.getNativeType(swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema),
|
|
298
|
+
nullable: false,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
287
301
|
}
|
|
288
302
|
|
|
289
303
|
retrieveType(swaggerComponentProperty: SwaggerComponentProperty) {
|
|
@@ -329,54 +343,48 @@ ${MODEL_POST}`,
|
|
|
329
343
|
return ref.replace('#/components/schemas/', '');
|
|
330
344
|
}
|
|
331
345
|
|
|
332
|
-
|
|
333
|
-
if (
|
|
334
|
-
return this.retrieveComponentProperties(swaggerComponent);
|
|
335
|
-
else if (swaggerComponent.type == 'integer')
|
|
336
|
-
return this.retrieveEnumValues(name, swaggerComponent);
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
retrieveComponentProperties(swaggerCopmponent: SwaggerComponent) {
|
|
340
|
-
if (swaggerCopmponent.properties == null) return ``;
|
|
346
|
+
retrieveComponentProperties(swaggerCopmponent: SwaggerComponent | SwaggerSchema) {
|
|
347
|
+
if (swaggerCopmponent.properties == null) return [];
|
|
341
348
|
|
|
342
|
-
let properties =
|
|
349
|
+
let properties: PropertyDto[] = [];
|
|
343
350
|
for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
344
351
|
const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
345
|
-
properties
|
|
346
|
-
|
|
347
|
-
|
|
352
|
+
properties.push({
|
|
353
|
+
name: propertyName,
|
|
354
|
+
typeName: this.retrieveType(swaggerCopmponent.properties[propertyName]),
|
|
355
|
+
nullable: true,
|
|
356
|
+
});
|
|
348
357
|
}
|
|
349
358
|
|
|
350
359
|
return properties;
|
|
351
360
|
}
|
|
352
361
|
|
|
353
|
-
retrieveSchemaProperties(schema: SwaggerSchema) {
|
|
354
|
-
|
|
362
|
+
// retrieveSchemaProperties(schema: SwaggerSchema) {
|
|
363
|
+
// if (schema.properties == null) return [];
|
|
355
364
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
${this.toFirstLetterLowercase(propertyName)}: ${this.retrieveType(schema.properties[propertyName])} | undefined;`
|
|
362
|
-
|
|
365
|
+
// let properties: PropertyDto[] = [];
|
|
366
|
+
// for (let j = 0; j < Object.getOwnPropertyNames(schema.properties).length; j++) {
|
|
367
|
+
// let propertyName = Object.getOwnPropertyNames(schema.properties)[j];
|
|
368
|
+
// properties +=
|
|
369
|
+
// `
|
|
370
|
+
// ${this.toFirstLetterLowercase(propertyName)}: ${this.retrieveType(schema.properties[propertyName])} | undefined;`
|
|
371
|
+
// }
|
|
363
372
|
|
|
364
|
-
|
|
365
|
-
}
|
|
373
|
+
// return properties;
|
|
374
|
+
// }
|
|
366
375
|
|
|
367
376
|
retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
|
|
368
|
-
if (swaggerCopmponent.enum == null) return
|
|
377
|
+
if (swaggerCopmponent.enum == null) return [];
|
|
369
378
|
|
|
370
|
-
let
|
|
379
|
+
let values: EnumValueDto[] = [];
|
|
371
380
|
for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
${name} = ${value},`;
|
|
381
|
+
values.push({
|
|
382
|
+
name: swaggerCopmponent.enum[index].split('-')[0].trim(),
|
|
383
|
+
value: swaggerCopmponent.enum[index].split('-')[1].trim(),
|
|
384
|
+
});
|
|
377
385
|
}
|
|
378
386
|
|
|
379
|
-
return
|
|
387
|
+
return values;
|
|
380
388
|
}
|
|
381
389
|
|
|
382
390
|
retrieveNestedObjects(usedTypes: string[]) {
|
|
@@ -411,13 +419,15 @@ ${MODEL_POST}`,
|
|
|
411
419
|
getNativeType(schema: SwaggerSchema): string {
|
|
412
420
|
let nativeType = 'n.d.';
|
|
413
421
|
|
|
414
|
-
if (schema
|
|
422
|
+
if (schema.$ref != null) {
|
|
423
|
+
debugger
|
|
424
|
+
} else if (schema.type == 'array') {
|
|
415
425
|
nativeType = this.getNativeType(schema.items);
|
|
416
426
|
nativeType += '[]';
|
|
417
427
|
} else {
|
|
418
428
|
if (schema.type == 'integer') nativeType = 'number';
|
|
419
429
|
if (schema.type == 'string' && schema.format == null) nativeType = 'string';
|
|
420
|
-
if (schema.type == 'string' && schema.format == 'date-time') nativeType = 'moment.Moment';
|
|
430
|
+
if (schema.type == 'string' && schema.format == 'date-time') nativeType = this._dateLibrary == 'moment' ? 'moment.Moment' : 'Date';
|
|
421
431
|
if (schema.type == 'string' && schema.format == 'uuid') nativeType = 'string';
|
|
422
432
|
if (schema.type == 'string' && schema.format == 'binary') nativeType = 'File';
|
|
423
433
|
if (schema.type == 'number') nativeType = 'number';
|