@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
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const API_PRE = `import { HttpClient } from '@angular/common/http';
|
|
2
|
+
import { Observable, catchError, map } from 'rxjs';
|
|
3
|
+
import { httpOptions } from 'src/app/core/utils/http-options';
|
|
4
|
+
import * as Models from './model.autogenerated';
|
|
5
|
+
|
|
6
|
+
export abstract class ApiAutogeneratedService {
|
|
7
|
+
constructor(
|
|
8
|
+
private _http: HttpClient,
|
|
9
|
+
private _baseUrl: string,
|
|
10
|
+
) { }
|
|
11
|
+
|
|
12
|
+
protected abstract _handleRequest<T>(request: T): T;
|
|
13
|
+
protected abstract _handleResponse<T>(response: T): T;
|
|
14
|
+
protected abstract _handleError(error: any, obs: any): any;
|
|
15
|
+
`;
|
|
16
|
+
export const API_POST = `}`;
|
package/dist/generator.js
CHANGED
|
@@ -1 +1,252 @@
|
|
|
1
|
-
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { API_PRE, API_POST } from './api.constants.js';
|
|
3
|
+
import { MODEL_POST, MODEL_PRE } from './model.constants.js';
|
|
4
|
+
const contentType = 'application/json';
|
|
5
|
+
export class Generator {
|
|
6
|
+
_swagger;
|
|
7
|
+
_outputDirectory;
|
|
8
|
+
constructor(swagger, outputDirectory) {
|
|
9
|
+
this._swagger = swagger;
|
|
10
|
+
this._outputDirectory = outputDirectory;
|
|
11
|
+
}
|
|
12
|
+
generateApi() {
|
|
13
|
+
console.debug(`Start autogeneration Apis`);
|
|
14
|
+
let apiMethods = ``;
|
|
15
|
+
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
16
|
+
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
17
|
+
const swaggerMethod = this._swagger.paths[apiName];
|
|
18
|
+
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
19
|
+
const swaggerMethodInfo = swaggerMethod[method];
|
|
20
|
+
console.debug(`\tAPI - ${apiName} - ${method}`);
|
|
21
|
+
let parametersString = this.retrieveParameters(swaggerMethodInfo);
|
|
22
|
+
let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
|
|
23
|
+
let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
|
|
24
|
+
if (returnTypeString == null)
|
|
25
|
+
returnTypeString = 'void';
|
|
26
|
+
let prepareRequestString = ``; //request = this._handleRequest(request);
|
|
27
|
+
let haveRequest = swaggerMethodInfo.requestBody != null;
|
|
28
|
+
if (haveRequest) {
|
|
29
|
+
prepareRequestString = `request = this._handleRequest(request);
|
|
30
|
+
`;
|
|
31
|
+
}
|
|
32
|
+
apiMethods +=
|
|
33
|
+
`
|
|
34
|
+
public ${apiName.replace('/api/v{version}/', '').replaceAll('/', '_')}(${parametersString}): Observable<${returnTypeString}>{
|
|
35
|
+
${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${environment.BASE_URL}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', request' : ''}, httpOptions)
|
|
36
|
+
.pipe(
|
|
37
|
+
map(x => this._handleResponse(x)),
|
|
38
|
+
catchError((err, obs) => {
|
|
39
|
+
return this._handleError(err, <Observable<any>>obs);
|
|
40
|
+
})
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
`;
|
|
44
|
+
}
|
|
45
|
+
fs.writeFileSync(this._outputDirectory + "/api.autogenerated.ts", `${API_PRE}
|
|
46
|
+
${API_POST}`, { flag: 'w' });
|
|
47
|
+
}
|
|
48
|
+
generateModel() {
|
|
49
|
+
let usedTypes = [];
|
|
50
|
+
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
51
|
+
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
52
|
+
const swaggerMethod = this._swagger.paths[apiName];
|
|
53
|
+
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
54
|
+
const swaggerMethodInfo = swaggerMethod[method];
|
|
55
|
+
// if(apiName == "/api/v{version}/Ticket/Create"){
|
|
56
|
+
// debugger
|
|
57
|
+
// }
|
|
58
|
+
let parametersRefType = swaggerMethodInfo.parameters.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''));
|
|
59
|
+
usedTypes = usedTypes.concat(parametersRefType);
|
|
60
|
+
if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null) {
|
|
61
|
+
usedTypes.push(swaggerMethodInfo.responses[200].content[contentType].schema.$ref.replace('#/components/schemas/', ''));
|
|
62
|
+
}
|
|
63
|
+
if (swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref) {
|
|
64
|
+
usedTypes.push(swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref.replace('#/components/schemas/', ''));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.retrieveNestedObjects(usedTypes);
|
|
68
|
+
usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
|
|
69
|
+
// usedTypes = usedTypes.filter((value, index, array) => {
|
|
70
|
+
// array.indexOf(value) === index;
|
|
71
|
+
// });
|
|
72
|
+
// usedTypes.forEach(element => {
|
|
73
|
+
// console.debug(element);
|
|
74
|
+
// });
|
|
75
|
+
console.debug(`Start autogeneration Models`);
|
|
76
|
+
let models = ``;
|
|
77
|
+
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
|
|
78
|
+
const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
|
|
79
|
+
if (modelName == 'ActivatedVoucherSnackListResponse') {
|
|
80
|
+
console.debug("ActivatedVoucherSnackListResponse");
|
|
81
|
+
}
|
|
82
|
+
if (usedTypes.indexOf(modelName) < 0) {
|
|
83
|
+
console.debug(`\tModel SKIP - ${modelName}`);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
;
|
|
87
|
+
const swaggerCopmponent = this._swagger.components.schemas[modelName];
|
|
88
|
+
console.debug(`\tModel - ${modelName}`);
|
|
89
|
+
let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
|
|
90
|
+
let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
|
|
91
|
+
models +=
|
|
92
|
+
`
|
|
93
|
+
export ${type} ${modelName} {${content}
|
|
94
|
+
}
|
|
95
|
+
`;
|
|
96
|
+
}
|
|
97
|
+
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts", `${MODEL_PRE}
|
|
98
|
+
${models}
|
|
99
|
+
${MODEL_POST}`, { flag: 'w' });
|
|
100
|
+
}
|
|
101
|
+
retrieveParameters(swaggerMethodInfo) {
|
|
102
|
+
if (swaggerMethodInfo.requestBody != null) {
|
|
103
|
+
return `request: Models.${swaggerMethodInfo.requestBody.content[contentType].schema.$ref.replace('#/components/schemas/', '')}`;
|
|
104
|
+
}
|
|
105
|
+
let parameters = ``;
|
|
106
|
+
swaggerMethodInfo.parameters.filter(x => x.in == 'query').forEach(parameter => {
|
|
107
|
+
if (parameter.schema.$ref != null) {
|
|
108
|
+
parameters += `Models.${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
parameters += `${parameter.name}: ${this.getNativeType(parameter.schema)}, `;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
if (parameters.length > 2)
|
|
115
|
+
parameters = parameters.substring(0, parameters.length - 2);
|
|
116
|
+
return parameters;
|
|
117
|
+
}
|
|
118
|
+
retrieveQueryParameters(swaggerMethodInfo) {
|
|
119
|
+
if (swaggerMethodInfo.requestBody != null)
|
|
120
|
+
return ``;
|
|
121
|
+
let filteredParameters = swaggerMethodInfo.parameters.filter(x => x.in == 'query');
|
|
122
|
+
if (filteredParameters.length == 0)
|
|
123
|
+
return ``;
|
|
124
|
+
let parameters = `?`;
|
|
125
|
+
filteredParameters.forEach(parameter => {
|
|
126
|
+
if (parameter.schema.$ref != null) {
|
|
127
|
+
this.parametrizeObject(parameter.schema.$ref);
|
|
128
|
+
//parameters += `${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}&`;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
parameters += `${this.lowercaseFirstLetter(parameter.name)}=\${` + this.lowercaseFirstLetter(parameter.name) + `}&`;
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
if (parameters.length > 1)
|
|
135
|
+
parameters = parameters.substring(0, parameters.length - 1);
|
|
136
|
+
return parameters;
|
|
137
|
+
}
|
|
138
|
+
retrieveReturnType(swaggerMethodInfo) {
|
|
139
|
+
if (swaggerMethodInfo.responses[200] == null)
|
|
140
|
+
return 'void';
|
|
141
|
+
if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null)
|
|
142
|
+
return `Models.${swaggerMethodInfo.responses[200]?.content[contentType].schema.$ref.replace('#/components/schemas/', '')}`;
|
|
143
|
+
if (swaggerMethodInfo.responses[200]?.content[contentType].schema.type != null)
|
|
144
|
+
return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentType].schema);
|
|
145
|
+
console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
|
|
146
|
+
throw new Error("unmanaged swaggerMethodInfo");
|
|
147
|
+
}
|
|
148
|
+
retrieveType(swaggerComponentProperty) {
|
|
149
|
+
if (swaggerComponentProperty.$ref != null)
|
|
150
|
+
return swaggerComponentProperty.$ref.replace('#/components/schemas/', '');
|
|
151
|
+
if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
|
|
152
|
+
if (swaggerComponentProperty.items.$ref != null)
|
|
153
|
+
return swaggerComponentProperty.items.$ref.replace('#/components/schemas/', '');
|
|
154
|
+
else
|
|
155
|
+
return this.getNativeType(swaggerComponentProperty);
|
|
156
|
+
if (swaggerComponentProperty.type != null)
|
|
157
|
+
return this.getNativeType(swaggerComponentProperty);
|
|
158
|
+
if (swaggerComponentProperty.type == null)
|
|
159
|
+
return '';
|
|
160
|
+
console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
|
|
161
|
+
throw new Error("unmanaged swaggerMethodInfo");
|
|
162
|
+
}
|
|
163
|
+
parametrizeObject(objectName) {
|
|
164
|
+
let component = this._swagger.components.schemas[objectName.replace('#/components/schemas/', '')];
|
|
165
|
+
if (component == null || component.properties == null)
|
|
166
|
+
return ``;
|
|
167
|
+
console.debug(component.properties);
|
|
168
|
+
return ``;
|
|
169
|
+
}
|
|
170
|
+
retrieveObjectContent(name, swaggerComponent) {
|
|
171
|
+
if (swaggerComponent.type == 'object')
|
|
172
|
+
return this.retrieveObjectProperties(swaggerComponent);
|
|
173
|
+
else if (swaggerComponent.type == 'integer')
|
|
174
|
+
return this.retrieveEnumValues(name, swaggerComponent);
|
|
175
|
+
}
|
|
176
|
+
retrieveObjectProperties(swaggerCopmponent) {
|
|
177
|
+
if (swaggerCopmponent.properties == null)
|
|
178
|
+
return ``;
|
|
179
|
+
// console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
|
|
180
|
+
let properties = ``;
|
|
181
|
+
for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
182
|
+
const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
183
|
+
properties +=
|
|
184
|
+
`
|
|
185
|
+
${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`;
|
|
186
|
+
}
|
|
187
|
+
return properties;
|
|
188
|
+
}
|
|
189
|
+
retrieveEnumValues(name, swaggerCopmponent) {
|
|
190
|
+
if (swaggerCopmponent.enum == null)
|
|
191
|
+
return ``;
|
|
192
|
+
let properties = ``;
|
|
193
|
+
for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
|
|
194
|
+
const name = swaggerCopmponent.enum[index].split('-')[0].trim();
|
|
195
|
+
const value = swaggerCopmponent.enum[index].split('-')[1].trim();
|
|
196
|
+
properties +=
|
|
197
|
+
`
|
|
198
|
+
${name} = ${value},`;
|
|
199
|
+
}
|
|
200
|
+
return properties;
|
|
201
|
+
}
|
|
202
|
+
retrieveNestedObjects(usedTypes) {
|
|
203
|
+
for (let i = 0; i < usedTypes.length; i++) {
|
|
204
|
+
const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
|
|
205
|
+
// const name = usedTypes[i]
|
|
206
|
+
// const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
|
|
207
|
+
this.retrieveNestedObjects2(swaggerCopmponent, usedTypes);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
retrieveNestedObjects2(swaggerComponent, usedTypes) {
|
|
211
|
+
if (!swaggerComponent.properties)
|
|
212
|
+
return;
|
|
213
|
+
for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
|
|
214
|
+
const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
|
|
215
|
+
let nestedUsedType = '';
|
|
216
|
+
if (swaggerComponent.properties[propertyName].$ref != null) {
|
|
217
|
+
nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
|
|
218
|
+
}
|
|
219
|
+
else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
|
|
220
|
+
nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
|
|
221
|
+
}
|
|
222
|
+
if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
|
|
223
|
+
usedTypes.push(nestedUsedType);
|
|
224
|
+
let nested = this._swagger.components.schemas[nestedUsedType];
|
|
225
|
+
this.retrieveNestedObjects2(nested, usedTypes);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
getNativeType(schema) {
|
|
230
|
+
if (schema.type == 'integer')
|
|
231
|
+
return 'number';
|
|
232
|
+
if (schema.type == 'string' && schema.format == null)
|
|
233
|
+
return 'string';
|
|
234
|
+
if (schema.type == 'string' && schema.format == 'date-time')
|
|
235
|
+
return 'moment.Moment';
|
|
236
|
+
if (schema.type == 'string' && schema.format == 'uuid')
|
|
237
|
+
return 'string';
|
|
238
|
+
if (schema.type == 'number')
|
|
239
|
+
return 'number';
|
|
240
|
+
if (schema.type == 'array')
|
|
241
|
+
return '[]';
|
|
242
|
+
if (schema.type == 'boolean')
|
|
243
|
+
return 'boolean';
|
|
244
|
+
if (schema.type == 'object')
|
|
245
|
+
return 'any';
|
|
246
|
+
console.error("unmanaged schema type", schema);
|
|
247
|
+
throw new Error("unmanaged schema");
|
|
248
|
+
}
|
|
249
|
+
lowercaseFirstLetter(value) {
|
|
250
|
+
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
251
|
+
}
|
|
252
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,264 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
async download() {
|
|
10
|
-
let response = await fetch(swaggerJsonUrl, settings);
|
|
11
|
-
let json = await response.json();
|
|
12
|
-
return json;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
export class Generator {
|
|
16
|
-
_swagger;
|
|
17
|
-
constructor(swagger) {
|
|
18
|
-
this._swagger = swagger;
|
|
19
|
-
}
|
|
20
|
-
generateApi() {
|
|
21
|
-
console.debug(`Start autogeneration Apis`);
|
|
22
|
-
let apiMethods = ``;
|
|
23
|
-
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
24
|
-
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
25
|
-
const swaggerMethod = this._swagger.paths[apiName];
|
|
26
|
-
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
27
|
-
const swaggerMethodInfo = swaggerMethod[method];
|
|
28
|
-
console.debug(`\tAPI - ${apiName} - ${method}`);
|
|
29
|
-
let parametersString = this.retrieveParameters(swaggerMethodInfo);
|
|
30
|
-
let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
|
|
31
|
-
let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
|
|
32
|
-
if (returnTypeString == null)
|
|
33
|
-
returnTypeString = 'void';
|
|
34
|
-
let prepareRequestString = ``; //request = this._handleRequest(request);
|
|
35
|
-
let haveRequest = swaggerMethodInfo.requestBody != null;
|
|
36
|
-
if (haveRequest) {
|
|
37
|
-
prepareRequestString = `request = this._handleRequest(request);
|
|
38
|
-
`;
|
|
39
|
-
}
|
|
40
|
-
apiMethods +=
|
|
41
|
-
`
|
|
42
|
-
public ${apiName.replace('/api/v{version}/', '').replaceAll('/', '_')}(${parametersString}): Observable<${returnTypeString}>{
|
|
43
|
-
${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${environment.BASE_URL}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', request' : ''}, httpOptions)
|
|
44
|
-
.pipe(
|
|
45
|
-
map(x => this._handleResponse(x)),
|
|
46
|
-
catchError((err, obs) => {
|
|
47
|
-
return this._handleError(err, <Observable<any>>obs);
|
|
48
|
-
})
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
`;
|
|
52
|
-
}
|
|
53
|
-
fs.writeFileSync("autogeneration/output/api.autogenerated.ts", `${apiPre}
|
|
54
|
-
${apiMethods}
|
|
55
|
-
${apiPost}`);
|
|
56
|
-
}
|
|
57
|
-
generateModel() {
|
|
58
|
-
let usedTypes = [];
|
|
59
|
-
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
60
|
-
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
61
|
-
const swaggerMethod = this._swagger.paths[apiName];
|
|
62
|
-
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
63
|
-
const swaggerMethodInfo = swaggerMethod[method];
|
|
64
|
-
let parametersRefType = swaggerMethodInfo.parameters.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''));
|
|
65
|
-
usedTypes = usedTypes.concat(parametersRefType);
|
|
66
|
-
if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null) {
|
|
67
|
-
usedTypes.push(swaggerMethodInfo.responses[200].content[contentType].schema.$ref.replace('#/components/schemas/', ''));
|
|
68
|
-
}
|
|
69
|
-
if (swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref) {
|
|
70
|
-
usedTypes.push(swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref.replace('#/components/schemas/', ''));
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
// usedTypes.forEach(element => {
|
|
74
|
-
// console.debug(element);
|
|
75
|
-
// });
|
|
76
|
-
console.debug(`Start autogeneration Models`);
|
|
77
|
-
let models = ``;
|
|
78
|
-
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
|
|
79
|
-
const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
|
|
80
|
-
if (usedTypes.indexOf(modelName) < 0) {
|
|
81
|
-
console.debug(`\tModel SKIP - ${modelName}`);
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
;
|
|
85
|
-
const swaggerCopmponent = this._swagger.components.schemas[modelName];
|
|
86
|
-
console.debug(`\tModel - ${modelName}`);
|
|
87
|
-
let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
|
|
88
|
-
let content = this.retrieveObjectContent(swaggerCopmponent);
|
|
89
|
-
models +=
|
|
90
|
-
`
|
|
91
|
-
export ${type} ${modelName} {${content}
|
|
92
|
-
}
|
|
93
|
-
`;
|
|
94
|
-
}
|
|
95
|
-
fs.writeFileSync("autogeneration/output/model.autogenerated.ts", `${modelPre}
|
|
96
|
-
${models}
|
|
97
|
-
${modelPost}`);
|
|
98
|
-
}
|
|
99
|
-
retrieveParameters(swaggerMethodInfo) {
|
|
100
|
-
if (swaggerMethodInfo.requestBody != null) {
|
|
101
|
-
return `request: ${swaggerMethodInfo.requestBody.content[contentType].schema.$ref.replace('#/components/schemas/', '')}`;
|
|
102
|
-
}
|
|
103
|
-
let parameters = ``;
|
|
104
|
-
swaggerMethodInfo.parameters.filter(x => x.in == 'query').forEach(parameter => {
|
|
105
|
-
if (parameter.schema.$ref != null) {
|
|
106
|
-
parameters += `${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
parameters += `${parameter.name}: ${this.getNativeType(parameter.schema)}, `;
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
if (parameters.length > 2)
|
|
113
|
-
parameters = parameters.substring(0, parameters.length - 2);
|
|
114
|
-
return parameters;
|
|
115
|
-
}
|
|
116
|
-
retrieveQueryParameters(swaggerMethodInfo) {
|
|
117
|
-
if (swaggerMethodInfo.requestBody != null)
|
|
118
|
-
return ``;
|
|
119
|
-
let filteredParameters = swaggerMethodInfo.parameters.filter(x => x.in == 'query');
|
|
120
|
-
if (filteredParameters.length == 0)
|
|
121
|
-
return ``;
|
|
122
|
-
let parameters = `?`;
|
|
123
|
-
filteredParameters.forEach(parameter => {
|
|
124
|
-
if (parameter.schema.$ref != null) {
|
|
125
|
-
this.parametrizeObject(parameter.schema.$ref);
|
|
126
|
-
//parameters += `${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}&`;
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
parameters += `${this.lowercaseFirstLetter(parameter.name)}=\${` + this.lowercaseFirstLetter(parameter.name) + `}&`;
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
if (parameters.length > 1)
|
|
133
|
-
parameters = parameters.substring(0, parameters.length - 1);
|
|
134
|
-
return parameters;
|
|
135
|
-
}
|
|
136
|
-
retrieveReturnType(swaggerMethodInfo) {
|
|
137
|
-
if (swaggerMethodInfo.responses[200] == null)
|
|
138
|
-
return 'void';
|
|
139
|
-
if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null)
|
|
140
|
-
return swaggerMethodInfo.responses[200]?.content[contentType].schema.$ref.replace('#/components/schemas/', '');
|
|
141
|
-
if (swaggerMethodInfo.responses[200]?.content[contentType].schema.type != null)
|
|
142
|
-
return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentType].schema);
|
|
143
|
-
console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
|
|
144
|
-
throw new Error("unmanaged swaggerMethodInfo");
|
|
145
|
-
}
|
|
146
|
-
retrieveType(swaggerComponentProperty) {
|
|
147
|
-
if (swaggerComponentProperty.$ref != null)
|
|
148
|
-
return swaggerComponentProperty.$ref.replace('#/components/schemas/', '');
|
|
149
|
-
if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
|
|
150
|
-
if (swaggerComponentProperty.items.$ref != null)
|
|
151
|
-
return swaggerComponentProperty.items.$ref.replace('#/components/schemas/', '');
|
|
152
|
-
else
|
|
153
|
-
return this.getNativeType(swaggerComponentProperty);
|
|
154
|
-
if (swaggerComponentProperty.type != null)
|
|
155
|
-
return this.getNativeType(swaggerComponentProperty);
|
|
156
|
-
console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
|
|
157
|
-
throw new Error("unmanaged swaggerMethodInfo");
|
|
158
|
-
}
|
|
159
|
-
parametrizeObject(objectName) {
|
|
160
|
-
let component = this._swagger.components.schemas[objectName.replace('#/components/schemas/', '')];
|
|
161
|
-
if (component == null || component.properties == null)
|
|
162
|
-
return ``;
|
|
163
|
-
console.debug(component.properties);
|
|
164
|
-
return ``;
|
|
165
|
-
}
|
|
166
|
-
retrieveObjectContent(swaggerComponent) {
|
|
167
|
-
if (swaggerComponent.type == 'object')
|
|
168
|
-
return this.retrieveObjectProperties(swaggerComponent);
|
|
169
|
-
else if (swaggerComponent.type == 'integer')
|
|
170
|
-
return this.retrieveEnumValues(swaggerComponent);
|
|
171
|
-
}
|
|
172
|
-
retrieveObjectProperties(swaggerCopmponent) {
|
|
173
|
-
if (swaggerCopmponent.properties == null)
|
|
174
|
-
return ``;
|
|
175
|
-
// console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
|
|
176
|
-
let properties = ``;
|
|
177
|
-
for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
178
|
-
const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
179
|
-
properties +=
|
|
180
|
-
`
|
|
181
|
-
${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`;
|
|
182
|
-
}
|
|
183
|
-
return properties;
|
|
184
|
-
}
|
|
185
|
-
retrieveEnumValues(swaggerCopmponent) {
|
|
186
|
-
}
|
|
187
|
-
retrieveNestedObjects(tmp) {
|
|
188
|
-
let usedTypes = [];
|
|
189
|
-
for (let i = 0; i < tmp.length; i++) {
|
|
190
|
-
const swaggerCopmponent = this._swagger.components.schemas['#/components/schemas/' + tmp[i]];
|
|
191
|
-
this.retrieveNestedObjects2(swaggerCopmponent);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
retrieveNestedObjects2(swaggerCopmponent) {
|
|
195
|
-
for (let j = 0; j < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; j++) {
|
|
196
|
-
const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[j];
|
|
197
|
-
if (swaggerCopmponent.properties[propertyName].$ref != null) {
|
|
198
|
-
swaggerCopmponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
getNativeType(schema) {
|
|
203
|
-
if (schema.type == 'integer')
|
|
204
|
-
return 'number';
|
|
205
|
-
if (schema.type == 'string' && schema.format == null)
|
|
206
|
-
return 'string';
|
|
207
|
-
if (schema.type == 'string' && schema.format == 'date-time')
|
|
208
|
-
return 'moment.Moment';
|
|
209
|
-
if (schema.type == 'string' && schema.format == 'uuid')
|
|
210
|
-
return 'string';
|
|
211
|
-
if (schema.type == 'number')
|
|
212
|
-
return 'number';
|
|
213
|
-
if (schema.type == 'array')
|
|
214
|
-
return '[]';
|
|
215
|
-
if (schema.type == 'boolean')
|
|
216
|
-
return 'boolean';
|
|
217
|
-
console.error("unmanaged schema type", schema);
|
|
218
|
-
throw new Error("unmanaged schema");
|
|
219
|
-
}
|
|
220
|
-
lowercaseFirstLetter(value) {
|
|
221
|
-
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
222
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Generator } from './generator.js';
|
|
3
|
+
import { SwaggerDownloader } from './swagger-downloader.js';
|
|
4
|
+
var args = process.argv.slice(2);
|
|
5
|
+
if (args.length !== 2) {
|
|
6
|
+
console.log("Warning: Requires 2 arguments");
|
|
7
|
+
console.log("node index.js [swaggerJsonUrl] [outputDirectory]");
|
|
8
|
+
process.exit();
|
|
223
9
|
}
|
|
10
|
+
const swaggerJsonUrl = args[0];
|
|
11
|
+
const outputDirectory = args[1];
|
|
12
|
+
//const excludedModels = ['Type', 'MethodBase', 'Assembly', 'MethodInfo']
|
|
13
|
+
// const apiUrl = args[0]//"http://localhost:5208";
|
|
14
|
+
// const version = args[1]; //"1";
|
|
15
|
+
// const swaggerJsonUrl = `${apiUrl}/swagger/v${version}/swagger.json`;
|
|
224
16
|
const swaggerDownloader = new SwaggerDownloader();
|
|
225
|
-
|
|
226
|
-
import { Injectable } from '@angular/core';
|
|
227
|
-
import { Observable, catchError, map } from 'rxjs';
|
|
228
|
-
import { httpOptions } from 'src/app/core/utils/http-options';
|
|
229
|
-
import { DatiUtenteTestataResponse } from '../models/dati-utente-testata-response';
|
|
230
|
-
import { LanguageCultureListQueryResponse } from '../models/language-culture';
|
|
231
|
-
import { API_AIRLINE_COMPANY, API_LANGUAGE_CULTURE, API_TEST, API_USER, API_VOUCHER_SNACK } from '../utils/constants';
|
|
232
|
-
import { ApiBase } from './../utils/api-base';
|
|
233
|
-
import { DialogMessageService } from './dialog-message.service';
|
|
234
|
-
import * as moment from 'moment-timezone';
|
|
235
|
-
import { VoucherSnackResponse } from '../models/api/voucher-snack-response.models';
|
|
236
|
-
import { VoucherSnackRequest } from '../models/api/voucher-snack-request.models';
|
|
237
|
-
import { ActivatedSnackVoucherResponse, SnackType } from 'src/app/ticket/models/richiesta-servizio-snack.models';
|
|
238
|
-
import { VoucherSnackRequestEmission } from '../models/api/voucher-snack-request-emission';
|
|
239
|
-
import { ActivateSnackVoucherResponse } from '../models/api/activate-snack-voucher-response.models';
|
|
240
|
-
import { VoucherActivationRequest } from '../models/api/voucher-activation-request.model';
|
|
241
|
-
import { Utente } from '../models/utente';
|
|
242
|
-
import { AirlineCompany } from 'src/app/sharedmodules/dialog-compagnie-aeree/compagnia-aerea.model';
|
|
243
|
-
|
|
244
|
-
@Injectable({
|
|
245
|
-
providedIn: 'root',
|
|
246
|
-
})
|
|
247
|
-
export class ApiService extends ApiBase {
|
|
248
|
-
constructor(
|
|
249
|
-
private _http: HttpClient,
|
|
250
|
-
_dialogMessage: DialogMessageService
|
|
251
|
-
) {
|
|
252
|
-
super(_dialogMessage);
|
|
253
|
-
}
|
|
254
|
-
`;
|
|
255
|
-
const apiPost = `}`;
|
|
256
|
-
const modelPre = `
|
|
257
|
-
`;
|
|
258
|
-
const modelPost = `
|
|
259
|
-
`;
|
|
260
|
-
swaggerDownloader.download()
|
|
17
|
+
swaggerDownloader.download(new URL(swaggerJsonUrl))
|
|
261
18
|
.then(swaggerDoc => {
|
|
262
|
-
return new Generator(swaggerDoc);
|
|
19
|
+
return new Generator(swaggerDoc, outputDirectory);
|
|
263
20
|
})
|
|
264
21
|
.then(generator => { generator.generateApi(); generator.generateModel(); });
|
|
22
|
+
// require('./index.js')({swaggerDownloader});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// let response = await fetch(swaggerJsonUrl, settings);
|
|
11
|
-
// let json = await response.json();
|
|
12
|
-
// return <Swagger>json;
|
|
13
|
-
// }
|
|
14
|
-
// }
|
|
1
|
+
import fetch from 'node-fetch';
|
|
2
|
+
const settings = { method: "Get" };
|
|
3
|
+
export class SwaggerDownloader {
|
|
4
|
+
async download(swaggerJsonUrl) {
|
|
5
|
+
let response = await fetch(swaggerJsonUrl, settings);
|
|
6
|
+
let json = await response.json();
|
|
7
|
+
return json;
|
|
8
|
+
}
|
|
9
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devlearning/swagger-generator",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"dev": "ts-node --esm src/index.ts http://localhost:5208/swagger/v1/swagger.json autogeneration/output"
|
|
8
|
+
"dev": "ts-node --esm ./src/index.ts http://localhost:5208/swagger/v1/swagger.json autogeneration/output",
|
|
9
|
+
"publish": "npx tsc && npm publish"
|
|
9
10
|
},
|
|
10
11
|
"bin": {
|
|
11
12
|
"swgen": "./dist/index.js"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const API_PRE =
|
|
2
|
+
`import { HttpClient } from '@angular/common/http';
|
|
3
|
+
import { Observable, catchError, map } from 'rxjs';
|
|
4
|
+
import { httpOptions } from 'src/app/core/utils/http-options';
|
|
5
|
+
import * as Models from './model.autogenerated';
|
|
6
|
+
|
|
7
|
+
export abstract class ApiAutogeneratedService {
|
|
8
|
+
constructor(
|
|
9
|
+
private _http: HttpClient,
|
|
10
|
+
private _baseUrl: string,
|
|
11
|
+
) { }
|
|
12
|
+
|
|
13
|
+
protected abstract _handleRequest<T>(request: T): T;
|
|
14
|
+
protected abstract _handleResponse<T>(response: T): T;
|
|
15
|
+
protected abstract _handleError(error: any, obs: any): any;
|
|
16
|
+
`;
|
|
17
|
+
|
|
18
|
+
export const API_POST =
|
|
19
|
+
`}`;
|