@devlearning/swagger-generator 0.0.23 → 0.0.25
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/autogeneration/output/api.autogenerated.ts +248 -214
- package/autogeneration/output/model.autogenerated.ts +64 -22
- package/dist/generator.js +88 -36
- package/package.json +1 -1
- package/src/generator.ts +92 -39
- package/src/models/swagger-component-property.ts +1 -0
- package/src/models/swagger-schema.ts +3 -0
- package/src/models/swagger.ts +6 -0
|
@@ -40,7 +40,7 @@ export class AirlineCompanyInAirportCreateCommand {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
export class AirlineCompanyListAirportsQueryResponse {
|
|
43
|
-
items:
|
|
43
|
+
items: AirportInAirlineCompany[] | undefined;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
export class AirlineCompanyListQueryResponse {
|
|
@@ -72,6 +72,18 @@ export class Airport {
|
|
|
72
72
|
timezone: string | undefined;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
export class AirportInAirlineCompany {
|
|
76
|
+
idAirlineCompanyInAirport: number | undefined;
|
|
77
|
+
idAirport: number | undefined;
|
|
78
|
+
name: string | undefined;
|
|
79
|
+
iataCode: string | undefined;
|
|
80
|
+
cityCode: string | undefined;
|
|
81
|
+
cityName: string | undefined;
|
|
82
|
+
countryName: string | undefined;
|
|
83
|
+
countryCode: string | undefined;
|
|
84
|
+
timezone: string | undefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
75
87
|
export class AirportListQueryResponse {
|
|
76
88
|
items: Airport[] | undefined;
|
|
77
89
|
}
|
|
@@ -127,6 +139,28 @@ export class CountryListQueryResponse {
|
|
|
127
139
|
items: Country[] | undefined;
|
|
128
140
|
}
|
|
129
141
|
|
|
142
|
+
export class DashboardAdminDisponibilita {
|
|
143
|
+
idHotel: number | undefined;
|
|
144
|
+
data: moment.Moment | undefined;
|
|
145
|
+
singole: number | undefined;
|
|
146
|
+
doppie: number | undefined;
|
|
147
|
+
triple: number | undefined;
|
|
148
|
+
tariffaSingola: number | undefined;
|
|
149
|
+
tariffaDoppia: number | undefined;
|
|
150
|
+
tariffaTripla: number | undefined;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export class DashboardAdminRiga {
|
|
154
|
+
idHotel: number | undefined;
|
|
155
|
+
citta: string | undefined;
|
|
156
|
+
aeroporto: string | undefined;
|
|
157
|
+
hotel: string | undefined;
|
|
158
|
+
telefonoHotel: string | undefined;
|
|
159
|
+
disponibilitaOggi: DashboardAdminDisponibilita | undefined;
|
|
160
|
+
disponibilitaDomani: DashboardAdminDisponibilita | undefined;
|
|
161
|
+
disponibilitaDopodomani: DashboardAdminDisponibilita | undefined;
|
|
162
|
+
}
|
|
163
|
+
|
|
130
164
|
export class EnumListQueryResponse {
|
|
131
165
|
hotelContactTypes: HotelContactType[] | undefined;
|
|
132
166
|
hotelRateTypes: HotelRateType[] | undefined;
|
|
@@ -140,11 +174,6 @@ export class EnumListQueryResponse {
|
|
|
140
174
|
userTypes: UserType[] | undefined;
|
|
141
175
|
}
|
|
142
176
|
|
|
143
|
-
export class FileUploadRequest {
|
|
144
|
-
file: File | undefined;
|
|
145
|
-
id: number | undefined;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
177
|
export class Hotel {
|
|
149
178
|
idHotel: number | undefined;
|
|
150
179
|
idCountry: number | undefined;
|
|
@@ -192,7 +221,7 @@ export class HotelAvailabilityItem {
|
|
|
192
221
|
idHotelRoom: number | undefined;
|
|
193
222
|
idHotelRoomResult: number | undefined;
|
|
194
223
|
hotelRoomResultType: HotelRoomResultTypeEnum | undefined;
|
|
195
|
-
hotelRateType:
|
|
224
|
+
hotelRateType: HotelRateType | undefined;
|
|
196
225
|
aeroporto: string | undefined;
|
|
197
226
|
hotel: string | undefined;
|
|
198
227
|
prefissoHotel: string | undefined;
|
|
@@ -355,6 +384,13 @@ export class HotelGroupUpdateCommand {
|
|
|
355
384
|
description: string | undefined;
|
|
356
385
|
}
|
|
357
386
|
|
|
387
|
+
export class HotelListDashboardResponse {
|
|
388
|
+
dataOggi: moment.Moment | undefined;
|
|
389
|
+
dataDomani: moment.Moment | undefined;
|
|
390
|
+
dataDopodomani: moment.Moment | undefined;
|
|
391
|
+
righe: DashboardAdminRiga[] | undefined;
|
|
392
|
+
}
|
|
393
|
+
|
|
358
394
|
export class HotelListSummaryQuery {
|
|
359
395
|
}
|
|
360
396
|
|
|
@@ -433,7 +469,7 @@ export class HotelRoomResultDetails {
|
|
|
433
469
|
idHotel: number | undefined;
|
|
434
470
|
idHotelRoom: number | undefined;
|
|
435
471
|
idHotelRoomResult: number | undefined;
|
|
436
|
-
idTipoCameraHotelEsito:
|
|
472
|
+
idTipoCameraHotelEsito: HotelRoomResultTypeEnum | undefined;
|
|
437
473
|
nomeHotel: string | undefined;
|
|
438
474
|
prefissoHotel: string | undefined;
|
|
439
475
|
telefonoHotel: string | undefined;
|
|
@@ -456,6 +492,21 @@ export class HotelRoomResultDetails {
|
|
|
456
492
|
note: string | undefined;
|
|
457
493
|
}
|
|
458
494
|
|
|
495
|
+
export class HotelRoomResultSaveCommand {
|
|
496
|
+
idHotel: number | undefined;
|
|
497
|
+
idHotelRoom: number | undefined;
|
|
498
|
+
idHotelRoomResult: number | undefined;
|
|
499
|
+
idHotelRoomResultType: HotelRoomResultTypeEnum | undefined;
|
|
500
|
+
data: moment.Moment | undefined;
|
|
501
|
+
note: string | undefined;
|
|
502
|
+
singole: number | undefined;
|
|
503
|
+
tariffaSingola: number | undefined;
|
|
504
|
+
doppie: number | undefined;
|
|
505
|
+
tariffaDoppia: number | undefined;
|
|
506
|
+
triple: number | undefined;
|
|
507
|
+
tariffaTripla: number | undefined;
|
|
508
|
+
}
|
|
509
|
+
|
|
459
510
|
export class HotelRoomResultType {
|
|
460
511
|
idHotelRoomResultType: HotelRoomResultTypeEnum | undefined;
|
|
461
512
|
name: string | undefined;
|
|
@@ -690,20 +741,6 @@ export enum PassengerRegistrationTypeEnum {
|
|
|
690
741
|
WITHOUT_REGISTRATION = 2,
|
|
691
742
|
}
|
|
692
743
|
|
|
693
|
-
export class ReprotectionHotelRoomResultSaveCommand {
|
|
694
|
-
idHotel: number | undefined;
|
|
695
|
-
idHotelRoom: number | undefined;
|
|
696
|
-
hotelRoomResultTypeEnum: HotelRoomResultTypeEnum | undefined;
|
|
697
|
-
data: moment.Moment | undefined;
|
|
698
|
-
note: string | undefined;
|
|
699
|
-
singole: number | undefined;
|
|
700
|
-
tariffaSingola: number | undefined;
|
|
701
|
-
doppie: number | undefined;
|
|
702
|
-
tariffaDoppia: number | undefined;
|
|
703
|
-
triple: number | undefined;
|
|
704
|
-
tariffaTripla: number | undefined;
|
|
705
|
-
}
|
|
706
|
-
|
|
707
744
|
export class ReprotectionListQuery {
|
|
708
745
|
idServiceRequestAirportToHotel: number | undefined;
|
|
709
746
|
filter: string | undefined;
|
|
@@ -1346,4 +1383,9 @@ export class VoucherSnackResponse {
|
|
|
1346
1383
|
idUserCreatedBy: number | undefined;
|
|
1347
1384
|
}
|
|
1348
1385
|
|
|
1386
|
+
export class TicketFile_Create {
|
|
1387
|
+
File: File | undefined;
|
|
1388
|
+
Id: number | undefined;
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1349
1391
|
|
package/dist/generator.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import { API_PRE, API_POST } from './api.constants.js';
|
|
3
3
|
import { MODEL_POST, MODEL_PRE } from './model.constants.js';
|
|
4
|
-
const
|
|
4
|
+
const contentTypeApplicationJson = 'application/json';
|
|
5
|
+
const contentTypeMultipartFormData = 'multipart/form-data';
|
|
5
6
|
export class Generator {
|
|
6
7
|
_swagger;
|
|
7
8
|
_outputDirectory;
|
|
@@ -21,7 +22,7 @@ export class Generator {
|
|
|
21
22
|
if (apiName == '/api/v{version}/TransportOrder/ListByServiceRequest') {
|
|
22
23
|
console.debug('');
|
|
23
24
|
}
|
|
24
|
-
let parametersString = this.retrieveParameters(swaggerMethodInfo);
|
|
25
|
+
let parametersString = this.retrieveParameters(swaggerMethodInfo, apiName);
|
|
25
26
|
let queryParametersDeclaration = this.retrieveQueryParametersDeclaration(swaggerMethodInfo);
|
|
26
27
|
let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
|
|
27
28
|
let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
|
|
@@ -29,14 +30,21 @@ export class Generator {
|
|
|
29
30
|
returnTypeString = 'void';
|
|
30
31
|
let prepareRequestString = ``; //request = this._handleRequest(request);
|
|
31
32
|
let haveRequest = swaggerMethodInfo.requestBody != null;
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
let isMultiPart = haveRequest && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null;
|
|
34
|
+
let httpOptions = 'httpOptions';
|
|
35
|
+
if (haveRequest && !isMultiPart) {
|
|
36
|
+
prepareRequestString = `const wrappedRequest = this._handleRequest(request);
|
|
34
37
|
`;
|
|
35
38
|
}
|
|
39
|
+
else if (isMultiPart) {
|
|
40
|
+
prepareRequestString = ` const wrappedRequest = this._handleMultipart(request);
|
|
41
|
+
`;
|
|
42
|
+
httpOptions = 'httpOptionsMultipart';
|
|
43
|
+
}
|
|
36
44
|
apiMethods +=
|
|
37
45
|
`
|
|
38
46
|
public ${apiName.replace('/api/v{version}/', '').replaceAll('/', '_')}(${parametersString}): Observable<${returnTypeString}> {
|
|
39
|
-
${queryParametersDeclaration}${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ',
|
|
47
|
+
${queryParametersDeclaration}${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
|
|
40
48
|
.pipe(
|
|
41
49
|
map(x => this._handleResponse(x)),
|
|
42
50
|
catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
|
|
@@ -50,21 +58,25 @@ ${API_POST}`, { flag: 'w' });
|
|
|
50
58
|
}
|
|
51
59
|
generateModel() {
|
|
52
60
|
let usedTypes = [];
|
|
61
|
+
let usedMultiPart = [];
|
|
53
62
|
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
54
63
|
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
55
64
|
const swaggerMethod = this._swagger.paths[apiName];
|
|
56
65
|
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
57
66
|
const swaggerMethodInfo = swaggerMethod[method];
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
67
|
+
if (apiName == "/api/v{version}/TicketFile/Create") {
|
|
68
|
+
debugger;
|
|
69
|
+
}
|
|
61
70
|
let parametersRefType = swaggerMethodInfo.parameters.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''));
|
|
62
71
|
usedTypes = usedTypes.concat(parametersRefType);
|
|
63
|
-
if (swaggerMethodInfo.responses[200].content[
|
|
64
|
-
usedTypes.push(swaggerMethodInfo.responses[200].content[
|
|
72
|
+
if (swaggerMethodInfo.responses[200].content[contentTypeApplicationJson]?.schema.$ref != null) {
|
|
73
|
+
usedTypes.push(swaggerMethodInfo.responses[200].content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''));
|
|
65
74
|
}
|
|
66
|
-
if (swaggerMethodInfo.requestBody?.content[
|
|
67
|
-
usedTypes.push(swaggerMethodInfo.requestBody?.content[
|
|
75
|
+
if (swaggerMethodInfo.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref) {
|
|
76
|
+
usedTypes.push(swaggerMethodInfo.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref.replace('#/components/schemas/', ''));
|
|
77
|
+
}
|
|
78
|
+
if (swaggerMethodInfo.requestBody?.content[contentTypeMultipartFormData]?.schema != null) {
|
|
79
|
+
usedMultiPart.push(apiName);
|
|
68
80
|
}
|
|
69
81
|
}
|
|
70
82
|
this.retrieveNestedObjects(usedTypes);
|
|
@@ -97,28 +109,47 @@ export ${type} ${modelName} {${content}
|
|
|
97
109
|
}
|
|
98
110
|
`;
|
|
99
111
|
}
|
|
112
|
+
usedMultiPart.forEach(apiName => {
|
|
113
|
+
const swaggerMethod = this._swagger.paths[apiName];
|
|
114
|
+
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
115
|
+
const swaggerMethodInfo = swaggerMethod[method];
|
|
116
|
+
let type = 'class';
|
|
117
|
+
let modelName = this.getModelNameByApi(apiName);
|
|
118
|
+
let content = this.retrieveSchemaProperties(swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData].schema);
|
|
119
|
+
models +=
|
|
120
|
+
`
|
|
121
|
+
export ${type} ${modelName} {${content}
|
|
122
|
+
}
|
|
123
|
+
`;
|
|
124
|
+
});
|
|
100
125
|
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts", `${MODEL_PRE}
|
|
101
126
|
${models}
|
|
102
127
|
${MODEL_POST}`, { flag: 'w' });
|
|
103
128
|
}
|
|
104
|
-
retrieveParameters(swaggerMethodInfo) {
|
|
105
|
-
if (swaggerMethodInfo.requestBody != null) {
|
|
106
|
-
|
|
129
|
+
retrieveParameters(swaggerMethodInfo, apiName) {
|
|
130
|
+
if (swaggerMethodInfo.requestBody != null && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null) {
|
|
131
|
+
var modelName = this.getModelNameByApi(apiName);
|
|
132
|
+
return `request: Models.${modelName}`;
|
|
107
133
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
parameters += `${this.toFirstLetterLowercase(parameter.name)}: Models.${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
const nativeType = this.getNativeType(parameter.schema);
|
|
115
|
-
const nullable = !parameter.required ? '?' : '';
|
|
116
|
-
parameters += `${this.toFirstLetterLowercase(parameter.name)}${nullable}: ${nativeType}, `;
|
|
134
|
+
else {
|
|
135
|
+
if (swaggerMethodInfo.requestBody != null) {
|
|
136
|
+
return `request: Models.${swaggerMethodInfo.requestBody.content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', '')}`;
|
|
117
137
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
138
|
+
let parameters = ``;
|
|
139
|
+
swaggerMethodInfo.parameters.filter(x => x.in == 'query').forEach(parameter => {
|
|
140
|
+
if (parameter.schema.$ref != null) {
|
|
141
|
+
parameters += `${this.toFirstLetterLowercase(parameter.name)}: Models.${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
const nativeType = this.getNativeType(parameter.schema);
|
|
145
|
+
const nullable = !parameter.required ? '?' : '';
|
|
146
|
+
parameters += `${this.toFirstLetterLowercase(parameter.name)}${nullable}: ${nativeType}, `;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
if (parameters.length > 2)
|
|
150
|
+
parameters = parameters.substring(0, parameters.length - 2);
|
|
151
|
+
return parameters;
|
|
152
|
+
}
|
|
122
153
|
}
|
|
123
154
|
retrieveQueryParametersDeclaration(swaggerMethodInfo) {
|
|
124
155
|
if (swaggerMethodInfo.requestBody != null)
|
|
@@ -157,8 +188,14 @@ ${MODEL_POST}`, { flag: 'w' });
|
|
|
157
188
|
}
|
|
158
189
|
}
|
|
159
190
|
else {
|
|
160
|
-
|
|
191
|
+
if (this.isDate(parameter.schema)) {
|
|
192
|
+
return `let ${paramName}Param: string = encodeURIComponent(this._momentToString(${paramName}));
|
|
193
|
+
`;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
return `let ${paramName}Param: string = encodeURIComponent('' + ${paramName});
|
|
161
197
|
`;
|
|
198
|
+
}
|
|
162
199
|
}
|
|
163
200
|
}
|
|
164
201
|
retrieveQueryParameters(swaggerMethodInfo) {
|
|
@@ -190,16 +227,16 @@ ${MODEL_POST}`, { flag: 'w' });
|
|
|
190
227
|
if (swaggerMethodInfo.responses[200] == null)
|
|
191
228
|
return 'void';
|
|
192
229
|
try {
|
|
193
|
-
if (swaggerMethodInfo.responses[200].content[
|
|
194
|
-
return `Models.${this.getObjectName(swaggerMethodInfo.responses[200]?.content[
|
|
230
|
+
if (swaggerMethodInfo.responses[200].content[contentTypeApplicationJson].schema.$ref != null)
|
|
231
|
+
return `Models.${this.getObjectName(swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema.$ref)}`;
|
|
195
232
|
}
|
|
196
233
|
catch (error) {
|
|
197
234
|
const errorMessage = "\t\tAttenzione forse hai dimenticato IActionResult e non hai tipizzato il tipo restituito dal servizio";
|
|
198
235
|
console.error(`%c${errorMessage}`, 'color: red');
|
|
199
236
|
throw new Error(errorMessage);
|
|
200
237
|
}
|
|
201
|
-
if (swaggerMethodInfo.responses[200]?.content[
|
|
202
|
-
return this.getNativeType(swaggerMethodInfo.responses[200]?.content[
|
|
238
|
+
if (swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema.type != null)
|
|
239
|
+
return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema);
|
|
203
240
|
console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
|
|
204
241
|
throw new Error("unmanaged swaggerMethodInfo");
|
|
205
242
|
}
|
|
@@ -237,14 +274,13 @@ ${MODEL_POST}`, { flag: 'w' });
|
|
|
237
274
|
}
|
|
238
275
|
retrieveObjectContent(name, swaggerComponent) {
|
|
239
276
|
if (swaggerComponent.type == 'object')
|
|
240
|
-
return this.
|
|
277
|
+
return this.retrieveComponentProperties(swaggerComponent);
|
|
241
278
|
else if (swaggerComponent.type == 'integer')
|
|
242
279
|
return this.retrieveEnumValues(name, swaggerComponent);
|
|
243
280
|
}
|
|
244
|
-
|
|
281
|
+
retrieveComponentProperties(swaggerCopmponent) {
|
|
245
282
|
if (swaggerCopmponent.properties == null)
|
|
246
283
|
return ``;
|
|
247
|
-
// console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
|
|
248
284
|
let properties = ``;
|
|
249
285
|
for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
250
286
|
const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
@@ -254,6 +290,19 @@ ${MODEL_POST}`, { flag: 'w' });
|
|
|
254
290
|
}
|
|
255
291
|
return properties;
|
|
256
292
|
}
|
|
293
|
+
retrieveSchemaProperties(schema) {
|
|
294
|
+
if (schema.properties == null)
|
|
295
|
+
return ``;
|
|
296
|
+
let properties = ``;
|
|
297
|
+
for (let j = 0; j < Object.getOwnPropertyNames(schema.properties).length; j++) {
|
|
298
|
+
let propertyName = Object.getOwnPropertyNames(schema.properties)[j];
|
|
299
|
+
propertyName = this.toFirstLetterLowercase(propertyName);
|
|
300
|
+
properties +=
|
|
301
|
+
`
|
|
302
|
+
${propertyName}: ${this.retrieveType(schema.properties[propertyName])} | undefined;`;
|
|
303
|
+
}
|
|
304
|
+
return properties;
|
|
305
|
+
}
|
|
257
306
|
retrieveEnumValues(name, swaggerCopmponent) {
|
|
258
307
|
if (swaggerCopmponent.enum == null)
|
|
259
308
|
return ``;
|
|
@@ -326,6 +375,9 @@ ${MODEL_POST}`, { flag: 'w' });
|
|
|
326
375
|
throw new Error("unmanaged schema");
|
|
327
376
|
}
|
|
328
377
|
}
|
|
378
|
+
getModelNameByApi(apiName) {
|
|
379
|
+
return apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
380
|
+
}
|
|
329
381
|
toFirstLetterLowercase(value) {
|
|
330
382
|
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
331
383
|
}
|
package/package.json
CHANGED
package/src/generator.ts
CHANGED
|
@@ -7,7 +7,8 @@ import { SwaggerSchema } from './models/swagger-schema.js';
|
|
|
7
7
|
import { API_PRE, API_POST } from './api.constants.js';
|
|
8
8
|
import { MODEL_POST, MODEL_PRE } from './model.constants.js';
|
|
9
9
|
|
|
10
|
-
const
|
|
10
|
+
const contentTypeApplicationJson = 'application/json';
|
|
11
|
+
const contentTypeMultipartFormData = 'multipart/form-data';
|
|
11
12
|
|
|
12
13
|
export class Generator {
|
|
13
14
|
private _swagger: Swagger;
|
|
@@ -33,23 +34,29 @@ export class Generator {
|
|
|
33
34
|
console.debug('');
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
let parametersString = this.retrieveParameters(swaggerMethodInfo);
|
|
37
|
+
let parametersString = this.retrieveParameters(swaggerMethodInfo, apiName);
|
|
37
38
|
let queryParametersDeclaration = this.retrieveQueryParametersDeclaration(swaggerMethodInfo);
|
|
38
39
|
let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
|
|
39
40
|
let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
|
|
40
41
|
if (returnTypeString == null) returnTypeString = 'void';
|
|
41
42
|
let prepareRequestString = ``; //request = this._handleRequest(request);
|
|
42
43
|
let haveRequest = swaggerMethodInfo.requestBody != null;
|
|
44
|
+
let isMultiPart = haveRequest && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null;
|
|
45
|
+
let httpOptions = 'httpOptions';
|
|
43
46
|
|
|
44
|
-
if (haveRequest) {
|
|
45
|
-
prepareRequestString = `
|
|
47
|
+
if (haveRequest && !isMultiPart) {
|
|
48
|
+
prepareRequestString = `const wrappedRequest = this._handleRequest(request);
|
|
46
49
|
`;
|
|
50
|
+
} else if (isMultiPart) {
|
|
51
|
+
prepareRequestString = ` const wrappedRequest = this._handleMultipart(request);
|
|
52
|
+
`;
|
|
53
|
+
httpOptions = 'httpOptionsMultipart';
|
|
47
54
|
}
|
|
48
55
|
|
|
49
56
|
apiMethods +=
|
|
50
57
|
`
|
|
51
58
|
public ${apiName.replace('/api/v{version}/', '').replaceAll('/', '_')}(${parametersString}): Observable<${returnTypeString}> {
|
|
52
|
-
${queryParametersDeclaration}${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ',
|
|
59
|
+
${queryParametersDeclaration}${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
|
|
53
60
|
.pipe(
|
|
54
61
|
map(x => this._handleResponse(x)),
|
|
55
62
|
catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
|
|
@@ -67,6 +74,7 @@ ${API_POST}`,
|
|
|
67
74
|
|
|
68
75
|
generateModel() {
|
|
69
76
|
let usedTypes: string[] = [];
|
|
77
|
+
let usedMultiPart: string[] = [];
|
|
70
78
|
|
|
71
79
|
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
72
80
|
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
@@ -74,18 +82,22 @@ ${API_POST}`,
|
|
|
74
82
|
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
75
83
|
const swaggerMethodInfo = swaggerMethod[method];
|
|
76
84
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
85
|
+
if(apiName == "/api/v{version}/TicketFile/Create"){
|
|
86
|
+
debugger
|
|
87
|
+
}
|
|
80
88
|
let parametersRefType = swaggerMethodInfo.parameters.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
|
|
81
89
|
usedTypes = usedTypes.concat(parametersRefType);
|
|
82
90
|
|
|
83
|
-
if (swaggerMethodInfo.responses[200].content[
|
|
84
|
-
usedTypes.push(swaggerMethodInfo.responses[200].content[
|
|
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/', ''));
|
|
85
97
|
}
|
|
86
98
|
|
|
87
|
-
if (swaggerMethodInfo.requestBody?.content[
|
|
88
|
-
|
|
99
|
+
if (swaggerMethodInfo.requestBody?.content[contentTypeMultipartFormData]?.schema != null) {
|
|
100
|
+
usedMultiPart.push(apiName);
|
|
89
101
|
}
|
|
90
102
|
}
|
|
91
103
|
|
|
@@ -128,6 +140,21 @@ export ${type} ${modelName} {${content}
|
|
|
128
140
|
`;
|
|
129
141
|
}
|
|
130
142
|
|
|
143
|
+
usedMultiPart.forEach(apiName => {
|
|
144
|
+
const swaggerMethod = this._swagger.paths[apiName];
|
|
145
|
+
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
146
|
+
const swaggerMethodInfo = swaggerMethod[method];
|
|
147
|
+
|
|
148
|
+
let type = 'class';
|
|
149
|
+
let modelName = this.getModelNameByApi(apiName);
|
|
150
|
+
let content = this.retrieveSchemaProperties(swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData].schema);
|
|
151
|
+
models +=
|
|
152
|
+
`
|
|
153
|
+
export ${type} ${modelName} {${content}
|
|
154
|
+
}
|
|
155
|
+
`;
|
|
156
|
+
});
|
|
157
|
+
|
|
131
158
|
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
|
|
132
159
|
`${MODEL_PRE}
|
|
133
160
|
${models}
|
|
@@ -135,26 +162,31 @@ ${MODEL_POST}`,
|
|
|
135
162
|
{ flag: 'w' });
|
|
136
163
|
}
|
|
137
164
|
|
|
138
|
-
retrieveParameters(swaggerMethodInfo: SwaggerMethod) {
|
|
139
|
-
if (swaggerMethodInfo.requestBody != null) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (parameter.schema.$ref != null) {
|
|
146
|
-
parameters += `${this.toFirstLetterLowercase(parameter.name)}: Models.${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
147
|
-
} else {
|
|
148
|
-
const nativeType = this.getNativeType(parameter.schema);
|
|
149
|
-
const nullable = !parameter.required ? '?' : '';
|
|
150
|
-
parameters += `${this.toFirstLetterLowercase(parameter.name)}${nullable}: ${nativeType}, `;
|
|
165
|
+
retrieveParameters(swaggerMethodInfo: SwaggerMethod, apiName: string) {
|
|
166
|
+
if (swaggerMethodInfo.requestBody != null && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null) {
|
|
167
|
+
var modelName = this.getModelNameByApi(apiName);
|
|
168
|
+
return `request: Models.${modelName}`;
|
|
169
|
+
} else {
|
|
170
|
+
if (swaggerMethodInfo.requestBody != null) {
|
|
171
|
+
return `request: Models.${swaggerMethodInfo.requestBody.content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', '')}`
|
|
151
172
|
}
|
|
152
|
-
});
|
|
153
173
|
|
|
154
|
-
|
|
155
|
-
parameters
|
|
174
|
+
let parameters = ``;
|
|
175
|
+
swaggerMethodInfo.parameters.filter(x => x.in == 'query').forEach(parameter => {
|
|
176
|
+
if (parameter.schema.$ref != null) {
|
|
177
|
+
parameters += `${this.toFirstLetterLowercase(parameter.name)}: Models.${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
178
|
+
} else {
|
|
179
|
+
const nativeType = this.getNativeType(parameter.schema);
|
|
180
|
+
const nullable = !parameter.required ? '?' : '';
|
|
181
|
+
parameters += `${this.toFirstLetterLowercase(parameter.name)}${nullable}: ${nativeType}, `;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
156
184
|
|
|
157
|
-
|
|
185
|
+
if (parameters.length > 2)
|
|
186
|
+
parameters = parameters.substring(0, parameters.length - 2);
|
|
187
|
+
|
|
188
|
+
return parameters;
|
|
189
|
+
}
|
|
158
190
|
}
|
|
159
191
|
|
|
160
192
|
retrieveQueryParametersDeclaration(swaggerMethodInfo: SwaggerMethod) {
|
|
@@ -193,8 +225,13 @@ ${MODEL_POST}`,
|
|
|
193
225
|
`;
|
|
194
226
|
}
|
|
195
227
|
} else {
|
|
196
|
-
|
|
228
|
+
if (this.isDate(parameter.schema)) {
|
|
229
|
+
return `let ${paramName}Param: string = encodeURIComponent(this._momentToString(${paramName}));
|
|
197
230
|
`;
|
|
231
|
+
} else {
|
|
232
|
+
return `let ${paramName}Param: string = encodeURIComponent('' + ${paramName});
|
|
233
|
+
`;
|
|
234
|
+
}
|
|
198
235
|
}
|
|
199
236
|
}
|
|
200
237
|
|
|
@@ -228,16 +265,16 @@ ${MODEL_POST}`,
|
|
|
228
265
|
return 'void';
|
|
229
266
|
|
|
230
267
|
try {
|
|
231
|
-
if (swaggerMethodInfo.responses[200].content[
|
|
232
|
-
return `Models.${this.getObjectName(swaggerMethodInfo.responses[200]?.content[
|
|
268
|
+
if (swaggerMethodInfo.responses[200].content[contentTypeApplicationJson].schema.$ref != null)
|
|
269
|
+
return `Models.${this.getObjectName(swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema.$ref)}`;
|
|
233
270
|
} catch (error) {
|
|
234
271
|
const errorMessage = "\t\tAttenzione forse hai dimenticato IActionResult e non hai tipizzato il tipo restituito dal servizio";
|
|
235
272
|
console.error(`%c${errorMessage}`, 'color: red');
|
|
236
273
|
throw new Error(errorMessage);
|
|
237
274
|
}
|
|
238
275
|
|
|
239
|
-
if (swaggerMethodInfo.responses[200]?.content[
|
|
240
|
-
return this.getNativeType(swaggerMethodInfo.responses[200]?.content[
|
|
276
|
+
if (swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema.type != null)
|
|
277
|
+
return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema);
|
|
241
278
|
|
|
242
279
|
console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
|
|
243
280
|
throw new Error("unmanaged swaggerMethodInfo");
|
|
@@ -288,20 +325,17 @@ ${MODEL_POST}`,
|
|
|
288
325
|
|
|
289
326
|
retrieveObjectContent(name: string, swaggerComponent: SwaggerComponent) {
|
|
290
327
|
if (swaggerComponent.type == 'object')
|
|
291
|
-
return this.
|
|
328
|
+
return this.retrieveComponentProperties(swaggerComponent);
|
|
292
329
|
else if (swaggerComponent.type == 'integer')
|
|
293
330
|
return this.retrieveEnumValues(name, swaggerComponent);
|
|
294
331
|
}
|
|
295
332
|
|
|
296
|
-
|
|
333
|
+
retrieveComponentProperties(swaggerCopmponent: SwaggerComponent) {
|
|
297
334
|
if (swaggerCopmponent.properties == null) return ``;
|
|
298
335
|
|
|
299
|
-
// console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
|
|
300
|
-
|
|
301
336
|
let properties = ``;
|
|
302
337
|
for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
303
338
|
const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
304
|
-
|
|
305
339
|
properties +=
|
|
306
340
|
`
|
|
307
341
|
${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`
|
|
@@ -310,6 +344,21 @@ ${MODEL_POST}`,
|
|
|
310
344
|
return properties;
|
|
311
345
|
}
|
|
312
346
|
|
|
347
|
+
retrieveSchemaProperties(schema: SwaggerSchema) {
|
|
348
|
+
if (schema.properties == null) return ``;
|
|
349
|
+
|
|
350
|
+
let properties = ``;
|
|
351
|
+
for (let j = 0; j < Object.getOwnPropertyNames(schema.properties).length; j++) {
|
|
352
|
+
let propertyName = Object.getOwnPropertyNames(schema.properties)[j];
|
|
353
|
+
propertyName = this.toFirstLetterLowercase(propertyName)
|
|
354
|
+
properties +=
|
|
355
|
+
`
|
|
356
|
+
${propertyName}: ${this.retrieveType(schema.properties[propertyName])} | undefined;`
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return properties;
|
|
360
|
+
}
|
|
361
|
+
|
|
313
362
|
retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
|
|
314
363
|
if (swaggerCopmponent.enum == null) return ``;
|
|
315
364
|
|
|
@@ -379,6 +428,10 @@ ${MODEL_POST}`,
|
|
|
379
428
|
}
|
|
380
429
|
}
|
|
381
430
|
|
|
431
|
+
getModelNameByApi(apiName: string) {
|
|
432
|
+
return apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
433
|
+
}
|
|
434
|
+
|
|
382
435
|
toFirstLetterLowercase(value: string) {
|
|
383
436
|
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
384
437
|
}
|
package/src/models/swagger.ts
CHANGED
|
@@ -20,6 +20,7 @@ export interface SwaggerParameter {
|
|
|
20
20
|
|
|
21
21
|
export interface SwaggerRequestBody {
|
|
22
22
|
content: { [key: string]: SwaggerContent; };
|
|
23
|
+
encoding : SwaggerEncoding;
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
export interface SwaggerResponses {
|
|
@@ -29,4 +30,9 @@ export interface SwaggerResponses {
|
|
|
29
30
|
|
|
30
31
|
export interface SwaggerComponents {
|
|
31
32
|
schemas: { [key: string]: SwaggerComponent; };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface SwaggerEncoding{
|
|
36
|
+
File: {style: 'form'},
|
|
37
|
+
Id: {style: 'form'},
|
|
32
38
|
}
|