@devlearning/swagger-generator 0.0.7 → 0.0.9
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 +13 -745
- package/dist/api.constants.js +16 -0
- package/dist/generator.js +252 -1
- package/dist/index.js +17 -259
- package/dist/model.constants.js +4 -0
- package/dist/models/swagger-component-property.js +1 -0
- package/dist/models/swagger-component.js +1 -0
- package/dist/models/swagger-content.js +1 -0
- package/dist/models/swagger-info.js +1 -0
- package/dist/models/swagger-method.js +1 -0
- package/dist/models/swagger-schema.js +1 -0
- package/dist/swagger-downloader.js +9 -14
- package/package.json +3 -2
- package/src/api.constants.ts +19 -0
- package/src/generator.ts +303 -0
- package/src/index.ts +2 -354
- package/src/model.constants.ts +7 -0
- package/src/models/swagger-component-property.ts +8 -0
- package/src/models/swagger-component.ts +8 -0
- package/src/models/swagger-content.ts +5 -0
- package/src/models/swagger-info.ts +4 -0
- package/src/models/swagger-method.ts +8 -0
- package/src/models/swagger-schema.ts +6 -0
- package/src/models/swagger.ts +6 -37
- package/src/swagger-downloader.ts +7 -6
- package/tsconfig.json +2 -21
- package/dist/output/api.autogenerated.js +0 -392
- package/dist/output/model.autogenerated.js +0 -354
package/src/generator.ts
CHANGED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { SwaggerComponent } from './models/swagger-component.js';
|
|
3
|
+
import { Swagger } from './models/swagger.js';
|
|
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 { API_PRE, API_POST } from './api.constants.js';
|
|
8
|
+
import { MODEL_POST, MODEL_PRE } from './model.constants.js';
|
|
9
|
+
|
|
10
|
+
const contentType = 'application/json';
|
|
11
|
+
|
|
12
|
+
export class Generator {
|
|
13
|
+
private _swagger: Swagger;
|
|
14
|
+
private _outputDirectory: string;
|
|
15
|
+
|
|
16
|
+
constructor(swagger: Swagger, outputDirectory: string) {
|
|
17
|
+
this._swagger = swagger;
|
|
18
|
+
this._outputDirectory = outputDirectory;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
generateApi() {
|
|
22
|
+
console.debug(`Start autogeneration Apis`);
|
|
23
|
+
|
|
24
|
+
let apiMethods = ``;
|
|
25
|
+
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
26
|
+
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
27
|
+
const swaggerMethod = this._swagger.paths[apiName];
|
|
28
|
+
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
29
|
+
const swaggerMethodInfo = swaggerMethod[method];
|
|
30
|
+
console.debug(`\tAPI - ${apiName} - ${method}`);
|
|
31
|
+
|
|
32
|
+
let parametersString = this.retrieveParameters(swaggerMethodInfo);
|
|
33
|
+
let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
|
|
34
|
+
let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
|
|
35
|
+
if (returnTypeString == null) returnTypeString = 'void';
|
|
36
|
+
let prepareRequestString = ``; //request = this._handleRequest(request);
|
|
37
|
+
let haveRequest = swaggerMethodInfo.requestBody != null;
|
|
38
|
+
|
|
39
|
+
if (haveRequest) {
|
|
40
|
+
prepareRequestString = `request = this._handleRequest(request);
|
|
41
|
+
`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
apiMethods +=
|
|
45
|
+
`
|
|
46
|
+
public ${apiName.replace('/api/v{version}/', '').replaceAll('/', '_')}(${parametersString}): Observable<${returnTypeString}>{
|
|
47
|
+
${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${environment.BASE_URL}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', request' : ''}, httpOptions)
|
|
48
|
+
.pipe(
|
|
49
|
+
map(x => this._handleResponse(x)),
|
|
50
|
+
catchError((err, obs) => {
|
|
51
|
+
return this._handleError(err, <Observable<any>>obs);
|
|
52
|
+
})
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fs.writeFileSync(this._outputDirectory + "/api.autogenerated.ts",
|
|
59
|
+
`${API_PRE}
|
|
60
|
+
${API_POST}`,
|
|
61
|
+
{ flag: 'w' });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
generateModel() {
|
|
65
|
+
let usedTypes: string[] = [];
|
|
66
|
+
|
|
67
|
+
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
68
|
+
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
69
|
+
const swaggerMethod = this._swagger.paths[apiName];
|
|
70
|
+
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
71
|
+
const swaggerMethodInfo = swaggerMethod[method];
|
|
72
|
+
|
|
73
|
+
// if(apiName == "/api/v{version}/Ticket/Create"){
|
|
74
|
+
// debugger
|
|
75
|
+
// }
|
|
76
|
+
let parametersRefType = swaggerMethodInfo.parameters.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
|
|
77
|
+
usedTypes = usedTypes.concat(parametersRefType);
|
|
78
|
+
|
|
79
|
+
if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null) {
|
|
80
|
+
usedTypes.push(swaggerMethodInfo.responses[200].content[contentType].schema.$ref.replace('#/components/schemas/', ''));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref) {
|
|
84
|
+
usedTypes.push(swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref.replace('#/components/schemas/', ''));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this.retrieveNestedObjects(usedTypes);
|
|
89
|
+
|
|
90
|
+
usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
|
|
91
|
+
|
|
92
|
+
// usedTypes = usedTypes.filter((value, index, array) => {
|
|
93
|
+
// array.indexOf(value) === index;
|
|
94
|
+
// });
|
|
95
|
+
// usedTypes.forEach(element => {
|
|
96
|
+
// console.debug(element);
|
|
97
|
+
// });
|
|
98
|
+
|
|
99
|
+
console.debug(`Start autogeneration Models`);
|
|
100
|
+
let models = ``;
|
|
101
|
+
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
|
|
102
|
+
const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
|
|
103
|
+
|
|
104
|
+
if (modelName == 'ActivatedVoucherSnackListResponse') {
|
|
105
|
+
console.debug("ActivatedVoucherSnackListResponse");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (usedTypes.indexOf(modelName) < 0) {
|
|
109
|
+
console.debug(`\tModel SKIP - ${modelName}`);
|
|
110
|
+
continue
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const swaggerCopmponent = this._swagger.components.schemas[modelName];
|
|
114
|
+
|
|
115
|
+
console.debug(`\tModel - ${modelName}`);
|
|
116
|
+
|
|
117
|
+
let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
|
|
118
|
+
let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
|
|
119
|
+
|
|
120
|
+
models +=
|
|
121
|
+
`
|
|
122
|
+
export ${type} ${modelName} {${content}
|
|
123
|
+
}
|
|
124
|
+
`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
|
|
128
|
+
`${MODEL_PRE}
|
|
129
|
+
${models}
|
|
130
|
+
${MODEL_POST}`,
|
|
131
|
+
{ flag: 'w' });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
retrieveParameters(swaggerMethodInfo: SwaggerMethod) {
|
|
135
|
+
if (swaggerMethodInfo.requestBody != null) {
|
|
136
|
+
return `request: Models.${swaggerMethodInfo.requestBody.content[contentType].schema.$ref.replace('#/components/schemas/', '')}`
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
let parameters = ``;
|
|
140
|
+
swaggerMethodInfo.parameters.filter(x => x.in == 'query').forEach(parameter => {
|
|
141
|
+
if (parameter.schema.$ref != null) {
|
|
142
|
+
parameters += `Models.${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
143
|
+
} else {
|
|
144
|
+
parameters += `${parameter.name}: ${this.getNativeType(parameter.schema)}, `;
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
if (parameters.length > 2)
|
|
149
|
+
parameters = parameters.substring(0, parameters.length - 2);
|
|
150
|
+
|
|
151
|
+
return parameters;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
retrieveQueryParameters(swaggerMethodInfo: SwaggerMethod) {
|
|
155
|
+
if (swaggerMethodInfo.requestBody != null) return ``;
|
|
156
|
+
|
|
157
|
+
let filteredParameters = swaggerMethodInfo.parameters.filter(x => x.in == 'query');
|
|
158
|
+
if (filteredParameters.length == 0) return ``;
|
|
159
|
+
|
|
160
|
+
let parameters = `?`;
|
|
161
|
+
filteredParameters.forEach(parameter => {
|
|
162
|
+
if (parameter.schema.$ref != null) {
|
|
163
|
+
this.parametrizeObject(parameter.schema.$ref)
|
|
164
|
+
//parameters += `${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}&`;
|
|
165
|
+
} else {
|
|
166
|
+
parameters += `${this.lowercaseFirstLetter(parameter.name)}=\${` + this.lowercaseFirstLetter(parameter.name) + `}&`;
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
if (parameters.length > 1)
|
|
171
|
+
parameters = parameters.substring(0, parameters.length - 1);
|
|
172
|
+
|
|
173
|
+
return parameters;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
retrieveReturnType(swaggerMethodInfo: SwaggerMethod) {
|
|
177
|
+
if (swaggerMethodInfo.responses[200] == null)
|
|
178
|
+
return 'void';
|
|
179
|
+
|
|
180
|
+
if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null)
|
|
181
|
+
return `Models.${swaggerMethodInfo.responses[200]?.content[contentType].schema.$ref.replace('#/components/schemas/', '')}`;
|
|
182
|
+
|
|
183
|
+
if (swaggerMethodInfo.responses[200]?.content[contentType].schema.type != null)
|
|
184
|
+
return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentType].schema);
|
|
185
|
+
|
|
186
|
+
console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
|
|
187
|
+
throw new Error("unmanaged swaggerMethodInfo");
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
retrieveType(swaggerComponentProperty: SwaggerComponentProperty) {
|
|
191
|
+
if (swaggerComponentProperty.$ref != null)
|
|
192
|
+
return swaggerComponentProperty.$ref.replace('#/components/schemas/', '');
|
|
193
|
+
|
|
194
|
+
if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
|
|
195
|
+
if (swaggerComponentProperty.items.$ref != null)
|
|
196
|
+
return swaggerComponentProperty.items.$ref.replace('#/components/schemas/', '');
|
|
197
|
+
else
|
|
198
|
+
return this.getNativeType(swaggerComponentProperty);
|
|
199
|
+
|
|
200
|
+
if (swaggerComponentProperty.type != null)
|
|
201
|
+
return this.getNativeType(swaggerComponentProperty);
|
|
202
|
+
|
|
203
|
+
if (swaggerComponentProperty.type == null)
|
|
204
|
+
return '';
|
|
205
|
+
|
|
206
|
+
console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
|
|
207
|
+
throw new Error("unmanaged swaggerMethodInfo");
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
parametrizeObject(objectName: string) {
|
|
211
|
+
let component = this._swagger.components.schemas[objectName.replace('#/components/schemas/', '')];
|
|
212
|
+
if (component == null || component.properties == null) return ``;
|
|
213
|
+
|
|
214
|
+
console.debug(component.properties);
|
|
215
|
+
|
|
216
|
+
return ``;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
retrieveObjectContent(name: string, swaggerComponent: SwaggerComponent) {
|
|
220
|
+
if (swaggerComponent.type == 'object')
|
|
221
|
+
return this.retrieveObjectProperties(swaggerComponent);
|
|
222
|
+
else if (swaggerComponent.type == 'integer')
|
|
223
|
+
return this.retrieveEnumValues(name, swaggerComponent);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
retrieveObjectProperties(swaggerCopmponent: SwaggerComponent) {
|
|
227
|
+
if (swaggerCopmponent.properties == null) return ``;
|
|
228
|
+
|
|
229
|
+
// console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
|
|
230
|
+
|
|
231
|
+
let properties = ``;
|
|
232
|
+
for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
233
|
+
const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
234
|
+
properties +=
|
|
235
|
+
`
|
|
236
|
+
${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return properties;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
|
|
243
|
+
if (swaggerCopmponent.enum == null) return ``;
|
|
244
|
+
|
|
245
|
+
let properties = ``;
|
|
246
|
+
for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
|
|
247
|
+
const name = swaggerCopmponent.enum[index].split('-')[0].trim();
|
|
248
|
+
const value = swaggerCopmponent.enum[index].split('-')[1].trim();
|
|
249
|
+
properties +=
|
|
250
|
+
`
|
|
251
|
+
${name} = ${value},`;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return properties;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
retrieveNestedObjects(usedTypes: string[]) {
|
|
258
|
+
for (let i = 0; i < usedTypes.length; i++) {
|
|
259
|
+
const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
|
|
260
|
+
// const name = usedTypes[i]
|
|
261
|
+
// const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
|
|
262
|
+
this.retrieveNestedObjects2(swaggerCopmponent, usedTypes);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
retrieveNestedObjects2(swaggerComponent: SwaggerComponent, usedTypes: string[]) {
|
|
267
|
+
if (!swaggerComponent.properties) return;
|
|
268
|
+
|
|
269
|
+
for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
|
|
270
|
+
const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
|
|
271
|
+
let nestedUsedType = '';
|
|
272
|
+
if (swaggerComponent.properties[propertyName].$ref != null) {
|
|
273
|
+
nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
|
|
274
|
+
} else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
|
|
275
|
+
nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
|
|
279
|
+
usedTypes.push(nestedUsedType);
|
|
280
|
+
let nested = this._swagger.components.schemas[nestedUsedType];
|
|
281
|
+
this.retrieveNestedObjects2(nested, usedTypes);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
getNativeType(schema: SwaggerSchema): string {
|
|
287
|
+
if (schema.type == 'integer') return 'number';
|
|
288
|
+
if (schema.type == 'string' && schema.format == null) return 'string';
|
|
289
|
+
if (schema.type == 'string' && schema.format == 'date-time') return 'moment.Moment';
|
|
290
|
+
if (schema.type == 'string' && schema.format == 'uuid') return 'string';
|
|
291
|
+
if (schema.type == 'number') return 'number';
|
|
292
|
+
if (schema.type == 'array') return '[]';
|
|
293
|
+
if (schema.type == 'boolean') return 'boolean';
|
|
294
|
+
if (schema.type == 'object') return 'any';
|
|
295
|
+
|
|
296
|
+
console.error("unmanaged schema type", schema);
|
|
297
|
+
throw new Error("unmanaged schema");
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
lowercaseFirstLetter(value: string) {
|
|
301
|
+
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
302
|
+
}
|
|
303
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import * as fs from 'fs';
|
|
5
|
-
import { Swagger, SwaggerComponent, SwaggerComponentProperty, SwaggerMethod, SwaggerSchema } from './models/swagger.js';
|
|
3
|
+
import { Generator } from './generator.js';
|
|
6
4
|
import { SwaggerDownloader } from './swagger-downloader.js';
|
|
7
5
|
|
|
8
6
|
var args = process.argv.slice(2);
|
|
@@ -20,362 +18,12 @@ const outputDirectory = args[1];
|
|
|
20
18
|
// const apiUrl = args[0]//"http://localhost:5208";
|
|
21
19
|
// const version = args[1]; //"1";
|
|
22
20
|
// const swaggerJsonUrl = `${apiUrl}/swagger/v${version}/swagger.json`;
|
|
23
|
-
const contentType = 'application/json';
|
|
24
|
-
|
|
25
|
-
export class Generator {
|
|
26
|
-
private _swagger: Swagger;
|
|
27
|
-
|
|
28
|
-
constructor(swagger: Swagger) {
|
|
29
|
-
this._swagger = swagger;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
generateApi() {
|
|
33
|
-
console.debug(`Start autogeneration Apis`);
|
|
34
|
-
|
|
35
|
-
let apiMethods = ``;
|
|
36
|
-
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
37
|
-
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
38
|
-
const swaggerMethod = this._swagger.paths[apiName];
|
|
39
|
-
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
40
|
-
const swaggerMethodInfo = swaggerMethod[method];
|
|
41
|
-
console.debug(`\tAPI - ${apiName} - ${method}`);
|
|
42
|
-
|
|
43
|
-
let parametersString = this.retrieveParameters(swaggerMethodInfo);
|
|
44
|
-
let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
|
|
45
|
-
let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
|
|
46
|
-
if (returnTypeString == null) returnTypeString = 'void';
|
|
47
|
-
let prepareRequestString = ``; //request = this._handleRequest(request);
|
|
48
|
-
let haveRequest = swaggerMethodInfo.requestBody != null;
|
|
49
|
-
|
|
50
|
-
if (haveRequest) {
|
|
51
|
-
prepareRequestString = `request = this._handleRequest(request);
|
|
52
|
-
`;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
apiMethods +=
|
|
56
|
-
`
|
|
57
|
-
public ${apiName.replace('/api/v{version}/', '').replaceAll('/', '_')}(${parametersString}): Observable<${returnTypeString}>{
|
|
58
|
-
${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${environment.BASE_URL}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', request' : ''}, httpOptions)
|
|
59
|
-
.pipe(
|
|
60
|
-
map(x => this._handleResponse(x)),
|
|
61
|
-
catchError((err, obs) => {
|
|
62
|
-
return this._handleError(err, <Observable<any>>obs);
|
|
63
|
-
})
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
`;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
fs.writeFileSync(outputDirectory + "/api.autogenerated.ts",
|
|
70
|
-
`${apiPre}
|
|
71
|
-
${apiMethods}
|
|
72
|
-
${apiPost}`,
|
|
73
|
-
{ flag: 'w' });
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
generateModel() {
|
|
77
|
-
let usedTypes: string[] = [];
|
|
78
|
-
|
|
79
|
-
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
80
|
-
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
81
|
-
const swaggerMethod = this._swagger.paths[apiName];
|
|
82
|
-
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
83
|
-
const swaggerMethodInfo = swaggerMethod[method];
|
|
84
|
-
|
|
85
|
-
// if(apiName == "/api/v{version}/Ticket/Create"){
|
|
86
|
-
// debugger
|
|
87
|
-
// }
|
|
88
|
-
let parametersRefType = swaggerMethodInfo.parameters.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
|
|
89
|
-
usedTypes = usedTypes.concat(parametersRefType);
|
|
90
|
-
|
|
91
|
-
if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null) {
|
|
92
|
-
usedTypes.push(swaggerMethodInfo.responses[200].content[contentType].schema.$ref.replace('#/components/schemas/', ''));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref) {
|
|
96
|
-
usedTypes.push(swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref.replace('#/components/schemas/', ''));
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
this.retrieveNestedObjects(usedTypes);
|
|
101
|
-
|
|
102
|
-
usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
|
|
103
|
-
|
|
104
|
-
// usedTypes = usedTypes.filter((value, index, array) => {
|
|
105
|
-
// array.indexOf(value) === index;
|
|
106
|
-
// });
|
|
107
|
-
// usedTypes.forEach(element => {
|
|
108
|
-
// console.debug(element);
|
|
109
|
-
// });
|
|
110
|
-
|
|
111
|
-
console.debug(`Start autogeneration Models`);
|
|
112
|
-
let models = ``;
|
|
113
|
-
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
|
|
114
|
-
const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
|
|
115
|
-
|
|
116
|
-
if (modelName == 'ActivatedVoucherSnackListResponse') {
|
|
117
|
-
console.debug("ActivatedVoucherSnackListResponse");
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (usedTypes.indexOf(modelName) < 0) {
|
|
121
|
-
console.debug(`\tModel SKIP - ${modelName}`);
|
|
122
|
-
continue
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
const swaggerCopmponent = this._swagger.components.schemas[modelName];
|
|
126
|
-
|
|
127
|
-
console.debug(`\tModel - ${modelName}`);
|
|
128
|
-
|
|
129
|
-
let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
|
|
130
|
-
let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
|
|
131
|
-
|
|
132
|
-
models +=
|
|
133
|
-
`
|
|
134
|
-
export ${type} ${modelName} {${content}
|
|
135
|
-
}
|
|
136
|
-
`;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
fs.writeFileSync(outputDirectory + "/model.autogenerated.ts",
|
|
140
|
-
`${modelPre}
|
|
141
|
-
${models}
|
|
142
|
-
${modelPost}`,
|
|
143
|
-
{ flag: 'w' });
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
retrieveParameters(swaggerMethodInfo: SwaggerMethod) {
|
|
147
|
-
if (swaggerMethodInfo.requestBody != null) {
|
|
148
|
-
return `request: ${swaggerMethodInfo.requestBody.content[contentType].schema.$ref.replace('#/components/schemas/', '')}`
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
let parameters = ``;
|
|
152
|
-
swaggerMethodInfo.parameters.filter(x => x.in == 'query').forEach(parameter => {
|
|
153
|
-
if (parameter.schema.$ref != null) {
|
|
154
|
-
parameters += `${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
155
|
-
} else {
|
|
156
|
-
parameters += `${parameter.name}: ${this.getNativeType(parameter.schema)}, `;
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
if (parameters.length > 2)
|
|
161
|
-
parameters = parameters.substring(0, parameters.length - 2);
|
|
162
|
-
|
|
163
|
-
return parameters;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
retrieveQueryParameters(swaggerMethodInfo: SwaggerMethod) {
|
|
167
|
-
if (swaggerMethodInfo.requestBody != null) return ``;
|
|
168
|
-
|
|
169
|
-
let filteredParameters = swaggerMethodInfo.parameters.filter(x => x.in == 'query');
|
|
170
|
-
if (filteredParameters.length == 0) return ``;
|
|
171
|
-
|
|
172
|
-
let parameters = `?`;
|
|
173
|
-
filteredParameters.forEach(parameter => {
|
|
174
|
-
if (parameter.schema.$ref != null) {
|
|
175
|
-
this.parametrizeObject(parameter.schema.$ref)
|
|
176
|
-
//parameters += `${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}&`;
|
|
177
|
-
} else {
|
|
178
|
-
parameters += `${this.lowercaseFirstLetter(parameter.name)}=\${` + this.lowercaseFirstLetter(parameter.name) + `}&`;
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
if (parameters.length > 1)
|
|
183
|
-
parameters = parameters.substring(0, parameters.length - 1);
|
|
184
|
-
|
|
185
|
-
return parameters;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
retrieveReturnType(swaggerMethodInfo: SwaggerMethod) {
|
|
189
|
-
if (swaggerMethodInfo.responses[200] == null)
|
|
190
|
-
return 'void';
|
|
191
|
-
|
|
192
|
-
if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null)
|
|
193
|
-
return swaggerMethodInfo.responses[200]?.content[contentType].schema.$ref.replace('#/components/schemas/', '')
|
|
194
|
-
|
|
195
|
-
if (swaggerMethodInfo.responses[200]?.content[contentType].schema.type != null)
|
|
196
|
-
return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentType].schema);
|
|
197
|
-
|
|
198
|
-
console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
|
|
199
|
-
throw new Error("unmanaged swaggerMethodInfo");
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
retrieveType(swaggerComponentProperty: SwaggerComponentProperty) {
|
|
203
|
-
if (swaggerComponentProperty.$ref != null)
|
|
204
|
-
return swaggerComponentProperty.$ref.replace('#/components/schemas/', '');
|
|
205
|
-
|
|
206
|
-
if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
|
|
207
|
-
if (swaggerComponentProperty.items.$ref != null)
|
|
208
|
-
return swaggerComponentProperty.items.$ref.replace('#/components/schemas/', '');
|
|
209
|
-
else
|
|
210
|
-
return this.getNativeType(swaggerComponentProperty);
|
|
211
|
-
|
|
212
|
-
if (swaggerComponentProperty.type != null)
|
|
213
|
-
return this.getNativeType(swaggerComponentProperty);
|
|
214
|
-
|
|
215
|
-
if (swaggerComponentProperty.type == null)
|
|
216
|
-
return '';
|
|
217
|
-
|
|
218
|
-
console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
|
|
219
|
-
throw new Error("unmanaged swaggerMethodInfo");
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
parametrizeObject(objectName: string) {
|
|
223
|
-
let component = this._swagger.components.schemas[objectName.replace('#/components/schemas/', '')];
|
|
224
|
-
if (component == null || component.properties == null) return ``;
|
|
225
|
-
|
|
226
|
-
console.debug(component.properties);
|
|
227
|
-
|
|
228
|
-
return ``;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
retrieveObjectContent(name: string, swaggerComponent: SwaggerComponent) {
|
|
232
|
-
if (swaggerComponent.type == 'object')
|
|
233
|
-
return this.retrieveObjectProperties(swaggerComponent);
|
|
234
|
-
else if (swaggerComponent.type == 'integer')
|
|
235
|
-
return this.retrieveEnumValues(name, swaggerComponent);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
retrieveObjectProperties(swaggerCopmponent: SwaggerComponent) {
|
|
239
|
-
if (swaggerCopmponent.properties == null) return ``;
|
|
240
|
-
|
|
241
|
-
// console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
|
|
242
|
-
|
|
243
|
-
let properties = ``;
|
|
244
|
-
for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
245
|
-
const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
246
|
-
properties +=
|
|
247
|
-
`
|
|
248
|
-
${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return properties;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
|
|
255
|
-
if (swaggerCopmponent.enum == null) return ``;
|
|
256
|
-
|
|
257
|
-
let properties = ``;
|
|
258
|
-
for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
|
|
259
|
-
const name = swaggerCopmponent.enum[index].split('-')[0].trim();
|
|
260
|
-
const value = swaggerCopmponent.enum[index].split('-')[1].trim();
|
|
261
|
-
properties +=
|
|
262
|
-
`
|
|
263
|
-
${name} = ${value},`;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
return properties;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
retrieveNestedObjects(usedTypes: string[]) {
|
|
270
|
-
for (let i = 0; i < usedTypes.length; i++) {
|
|
271
|
-
const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
|
|
272
|
-
// const name = usedTypes[i]
|
|
273
|
-
// const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
|
|
274
|
-
this.retrieveNestedObjects2(swaggerCopmponent, usedTypes);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/*
|
|
279
|
-
"ActivatedVoucherSnackReadQueryResponse": {
|
|
280
|
-
"type": "object",
|
|
281
|
-
"properties": {
|
|
282
|
-
"list": {
|
|
283
|
-
"type": "array",
|
|
284
|
-
"items": {
|
|
285
|
-
"$ref": "#/components/schemas/ActivatedVoucherSnackListResponse"
|
|
286
|
-
},
|
|
287
|
-
"nullable": true
|
|
288
|
-
}
|
|
289
|
-
},
|
|
290
|
-
"additionalProperties": false
|
|
291
|
-
},
|
|
292
|
-
*/
|
|
293
|
-
|
|
294
|
-
retrieveNestedObjects2(swaggerComponent: SwaggerComponent, usedTypes: string[]) {
|
|
295
|
-
if (!swaggerComponent.properties) return;
|
|
296
|
-
|
|
297
|
-
for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
|
|
298
|
-
const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
|
|
299
|
-
let nestedUsedType = '';
|
|
300
|
-
if (swaggerComponent.properties[propertyName].$ref != null) {
|
|
301
|
-
nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
|
|
302
|
-
} else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
|
|
303
|
-
nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
|
|
307
|
-
usedTypes.push(nestedUsedType);
|
|
308
|
-
let nested = this._swagger.components.schemas[nestedUsedType];
|
|
309
|
-
this.retrieveNestedObjects2(nested, usedTypes);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
getNativeType(schema: SwaggerSchema): string {
|
|
315
|
-
if (schema.type == 'integer') return 'number';
|
|
316
|
-
if (schema.type == 'string' && schema.format == null) return 'string';
|
|
317
|
-
if (schema.type == 'string' && schema.format == 'date-time') return 'moment.Moment';
|
|
318
|
-
if (schema.type == 'string' && schema.format == 'uuid') return 'string';
|
|
319
|
-
if (schema.type == 'number') return 'number';
|
|
320
|
-
if (schema.type == 'array') return '[]';
|
|
321
|
-
if (schema.type == 'boolean') return 'boolean';
|
|
322
|
-
if (schema.type == 'object') return 'any';
|
|
323
|
-
|
|
324
|
-
console.error("unmanaged schema type", schema);
|
|
325
|
-
throw new Error("unmanaged schema");
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
lowercaseFirstLetter(value: string) {
|
|
329
|
-
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
21
|
|
|
333
22
|
const swaggerDownloader = new SwaggerDownloader();
|
|
334
|
-
const apiPre =
|
|
335
|
-
`import { HttpClient } from '@angular/common/http';
|
|
336
|
-
import { Injectable } from '@angular/core';
|
|
337
|
-
import { Observable, catchError, map } from 'rxjs';
|
|
338
|
-
import { httpOptions } from 'src/app/core/utils/http-options';
|
|
339
|
-
import { DatiUtenteTestataResponse } from '../models/dati-utente-testata-response';
|
|
340
|
-
import { LanguageCultureListQueryResponse } from '../models/language-culture';
|
|
341
|
-
import { API_AIRLINE_COMPANY, API_LANGUAGE_CULTURE, API_TEST, API_USER, API_VOUCHER_SNACK } from '../utils/constants';
|
|
342
|
-
import { ApiBase } from './../utils/api-base';
|
|
343
|
-
import { DialogMessageService } from './dialog-message.service';
|
|
344
|
-
import * as moment from 'moment-timezone';
|
|
345
|
-
import { VoucherSnackResponse } from '../models/api/voucher-snack-response.models';
|
|
346
|
-
import { VoucherSnackRequest } from '../models/api/voucher-snack-request.models';
|
|
347
|
-
import { ActivatedSnackVoucherResponse, SnackType } from 'src/app/ticket/models/richiesta-servizio-snack.models';
|
|
348
|
-
import { VoucherSnackRequestEmission } from '../models/api/voucher-snack-request-emission';
|
|
349
|
-
import { ActivateSnackVoucherResponse } from '../models/api/activate-snack-voucher-response.models';
|
|
350
|
-
import { VoucherActivationRequest } from '../models/api/voucher-activation-request.model';
|
|
351
|
-
import { Utente } from '../models/utente';
|
|
352
|
-
import { AirlineCompany } from 'src/app/sharedmodules/dialog-compagnie-aeree/compagnia-aerea.model';
|
|
353
|
-
|
|
354
|
-
@Injectable({
|
|
355
|
-
providedIn: 'root',
|
|
356
|
-
})
|
|
357
|
-
export class ApiService extends ApiBase {
|
|
358
|
-
constructor(
|
|
359
|
-
private _http: HttpClient,
|
|
360
|
-
_dialogMessage: DialogMessageService
|
|
361
|
-
) {
|
|
362
|
-
super(_dialogMessage);
|
|
363
|
-
}
|
|
364
|
-
`;
|
|
365
|
-
const apiPost =
|
|
366
|
-
`}`;
|
|
367
|
-
|
|
368
|
-
const modelPre =
|
|
369
|
-
`import * as moment from 'moment';
|
|
370
|
-
`;
|
|
371
|
-
|
|
372
|
-
const modelPost =
|
|
373
|
-
`
|
|
374
|
-
`;
|
|
375
23
|
|
|
376
24
|
swaggerDownloader.download(new URL(swaggerJsonUrl))
|
|
377
25
|
.then(swaggerDoc => {
|
|
378
|
-
return new Generator(swaggerDoc);
|
|
26
|
+
return new Generator(swaggerDoc, outputDirectory);
|
|
379
27
|
})
|
|
380
28
|
.then(generator => { generator.generateApi(); generator.generateModel() });
|
|
381
29
|
|