@devlearning/swagger-generator 0.0.24 → 0.0.26
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 +80 -35
- package/package.json +1 -1
- package/src/generator.ts +85 -38
- 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/', ''));
|
|
74
|
+
}
|
|
75
|
+
if (swaggerMethodInfo.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref) {
|
|
76
|
+
usedTypes.push(swaggerMethodInfo.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref.replace('#/components/schemas/', ''));
|
|
65
77
|
}
|
|
66
|
-
if (swaggerMethodInfo.requestBody?.content[
|
|
67
|
-
|
|
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)
|
|
@@ -196,16 +227,16 @@ ${MODEL_POST}`, { flag: 'w' });
|
|
|
196
227
|
if (swaggerMethodInfo.responses[200] == null)
|
|
197
228
|
return 'void';
|
|
198
229
|
try {
|
|
199
|
-
if (swaggerMethodInfo.responses[200].content[
|
|
200
|
-
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)}`;
|
|
201
232
|
}
|
|
202
233
|
catch (error) {
|
|
203
234
|
const errorMessage = "\t\tAttenzione forse hai dimenticato IActionResult e non hai tipizzato il tipo restituito dal servizio";
|
|
204
235
|
console.error(`%c${errorMessage}`, 'color: red');
|
|
205
236
|
throw new Error(errorMessage);
|
|
206
237
|
}
|
|
207
|
-
if (swaggerMethodInfo.responses[200]?.content[
|
|
208
|
-
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);
|
|
209
240
|
console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
|
|
210
241
|
throw new Error("unmanaged swaggerMethodInfo");
|
|
211
242
|
}
|
|
@@ -243,14 +274,13 @@ ${MODEL_POST}`, { flag: 'w' });
|
|
|
243
274
|
}
|
|
244
275
|
retrieveObjectContent(name, swaggerComponent) {
|
|
245
276
|
if (swaggerComponent.type == 'object')
|
|
246
|
-
return this.
|
|
277
|
+
return this.retrieveComponentProperties(swaggerComponent);
|
|
247
278
|
else if (swaggerComponent.type == 'integer')
|
|
248
279
|
return this.retrieveEnumValues(name, swaggerComponent);
|
|
249
280
|
}
|
|
250
|
-
|
|
281
|
+
retrieveComponentProperties(swaggerCopmponent) {
|
|
251
282
|
if (swaggerCopmponent.properties == null)
|
|
252
283
|
return ``;
|
|
253
|
-
// console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
|
|
254
284
|
let properties = ``;
|
|
255
285
|
for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
256
286
|
const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
@@ -260,6 +290,18 @@ ${MODEL_POST}`, { flag: 'w' });
|
|
|
260
290
|
}
|
|
261
291
|
return properties;
|
|
262
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
|
+
properties +=
|
|
300
|
+
`
|
|
301
|
+
${this.toFirstLetterLowercase(propertyName)}: ${this.retrieveType(schema.properties[propertyName])} | undefined;`;
|
|
302
|
+
}
|
|
303
|
+
return properties;
|
|
304
|
+
}
|
|
263
305
|
retrieveEnumValues(name, swaggerCopmponent) {
|
|
264
306
|
if (swaggerCopmponent.enum == null)
|
|
265
307
|
return ``;
|
|
@@ -332,6 +374,9 @@ ${MODEL_POST}`, { flag: 'w' });
|
|
|
332
374
|
throw new Error("unmanaged schema");
|
|
333
375
|
}
|
|
334
376
|
}
|
|
377
|
+
getModelNameByApi(apiName) {
|
|
378
|
+
return apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
379
|
+
}
|
|
335
380
|
toFirstLetterLowercase(value) {
|
|
336
381
|
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
337
382
|
}
|
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/', ''));
|
|
85
93
|
}
|
|
86
94
|
|
|
87
|
-
if (swaggerMethodInfo.requestBody?.content[
|
|
88
|
-
usedTypes.push(swaggerMethodInfo.requestBody?.content[
|
|
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);
|
|
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) {
|
|
@@ -233,16 +265,16 @@ ${MODEL_POST}`,
|
|
|
233
265
|
return 'void';
|
|
234
266
|
|
|
235
267
|
try {
|
|
236
|
-
if (swaggerMethodInfo.responses[200].content[
|
|
237
|
-
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)}`;
|
|
238
270
|
} catch (error) {
|
|
239
271
|
const errorMessage = "\t\tAttenzione forse hai dimenticato IActionResult e non hai tipizzato il tipo restituito dal servizio";
|
|
240
272
|
console.error(`%c${errorMessage}`, 'color: red');
|
|
241
273
|
throw new Error(errorMessage);
|
|
242
274
|
}
|
|
243
275
|
|
|
244
|
-
if (swaggerMethodInfo.responses[200]?.content[
|
|
245
|
-
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);
|
|
246
278
|
|
|
247
279
|
console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
|
|
248
280
|
throw new Error("unmanaged swaggerMethodInfo");
|
|
@@ -293,20 +325,17 @@ ${MODEL_POST}`,
|
|
|
293
325
|
|
|
294
326
|
retrieveObjectContent(name: string, swaggerComponent: SwaggerComponent) {
|
|
295
327
|
if (swaggerComponent.type == 'object')
|
|
296
|
-
return this.
|
|
328
|
+
return this.retrieveComponentProperties(swaggerComponent);
|
|
297
329
|
else if (swaggerComponent.type == 'integer')
|
|
298
330
|
return this.retrieveEnumValues(name, swaggerComponent);
|
|
299
331
|
}
|
|
300
332
|
|
|
301
|
-
|
|
333
|
+
retrieveComponentProperties(swaggerCopmponent: SwaggerComponent) {
|
|
302
334
|
if (swaggerCopmponent.properties == null) return ``;
|
|
303
335
|
|
|
304
|
-
// console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
|
|
305
|
-
|
|
306
336
|
let properties = ``;
|
|
307
337
|
for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
308
338
|
const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
309
|
-
|
|
310
339
|
properties +=
|
|
311
340
|
`
|
|
312
341
|
${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`
|
|
@@ -315,6 +344,20 @@ ${MODEL_POST}`,
|
|
|
315
344
|
return properties;
|
|
316
345
|
}
|
|
317
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
|
+
properties +=
|
|
354
|
+
`
|
|
355
|
+
${this.toFirstLetterLowercase(propertyName)}: ${this.retrieveType(schema.properties[propertyName])} | undefined;`
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return properties;
|
|
359
|
+
}
|
|
360
|
+
|
|
318
361
|
retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
|
|
319
362
|
if (swaggerCopmponent.enum == null) return ``;
|
|
320
363
|
|
|
@@ -384,6 +427,10 @@ ${MODEL_POST}`,
|
|
|
384
427
|
}
|
|
385
428
|
}
|
|
386
429
|
|
|
430
|
+
getModelNameByApi(apiName: string) {
|
|
431
|
+
return apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
432
|
+
}
|
|
433
|
+
|
|
387
434
|
toFirstLetterLowercase(value: string) {
|
|
388
435
|
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
389
436
|
}
|
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
|
}
|