@devlearning/swagger-generator 0.0.6 → 0.0.8
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/dist/generator.js +249 -1
- package/dist/index.js +89 -33
- package/dist/models/swagger-component.js +1 -0
- package/dist/swagger-downloader.js +9 -14
- package/package.json +4 -3
- package/src/generator.ts +309 -0
- package/src/index.ts +4 -4
- package/src/models/swagger-component.ts +8 -0
- package/src/models/swagger.ts +2 -7
- 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/dist/generator.js
CHANGED
|
@@ -1 +1,249 @@
|
|
|
1
|
-
|
|
1
|
+
// import { Swagger } from "./models/swagger";
|
|
2
|
+
export {};
|
|
3
|
+
// class Generator {
|
|
4
|
+
// private _swagger: Swagger;
|
|
5
|
+
// constructor(swagger: Swagger) {
|
|
6
|
+
// this._swagger = swagger;
|
|
7
|
+
// }
|
|
8
|
+
// generateApi() {
|
|
9
|
+
// console.debug(`Start autogeneration Apis`);
|
|
10
|
+
// let apiMethods = ``;
|
|
11
|
+
// for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
12
|
+
// const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
13
|
+
// const swaggerMethod = this._swagger.paths[apiName];
|
|
14
|
+
// const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
15
|
+
// const swaggerMethodInfo = swaggerMethod[method];
|
|
16
|
+
// console.debug(`\tAPI - ${apiName} - ${method}`);
|
|
17
|
+
// let parametersString = this.retrieveParameters(swaggerMethodInfo);
|
|
18
|
+
// let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
|
|
19
|
+
// let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
|
|
20
|
+
// if (returnTypeString == null) returnTypeString = 'void';
|
|
21
|
+
// let prepareRequestString = ``; //request = this._handleRequest(request);
|
|
22
|
+
// let haveRequest = swaggerMethodInfo.requestBody != null;
|
|
23
|
+
// if (haveRequest) {
|
|
24
|
+
// prepareRequestString = `request = this._handleRequest(request);
|
|
25
|
+
// `;
|
|
26
|
+
// }
|
|
27
|
+
// apiMethods +=
|
|
28
|
+
// `
|
|
29
|
+
// public ${apiName.replace('/api/v{version}/', '').replaceAll('/', '_')}(${parametersString}): Observable<${returnTypeString}>{
|
|
30
|
+
// ${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${environment.BASE_URL}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', request' : ''}, httpOptions)
|
|
31
|
+
// .pipe(
|
|
32
|
+
// map(x => this._handleResponse(x)),
|
|
33
|
+
// catchError((err, obs) => {
|
|
34
|
+
// return this._handleError(err, <Observable<any>>obs);
|
|
35
|
+
// })
|
|
36
|
+
// );
|
|
37
|
+
// }
|
|
38
|
+
// `;
|
|
39
|
+
// }
|
|
40
|
+
// fs.writeFileSync(outputDirectory + "/api.autogenerated.ts",
|
|
41
|
+
// `${apiPre}
|
|
42
|
+
// ${apiMethods}
|
|
43
|
+
// ${apiPost}`,
|
|
44
|
+
// { flag: 'w' });
|
|
45
|
+
// }
|
|
46
|
+
// generateModel() {
|
|
47
|
+
// let usedTypes: string[] = [];
|
|
48
|
+
// for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
49
|
+
// const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
50
|
+
// const swaggerMethod = this._swagger.paths[apiName];
|
|
51
|
+
// const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
52
|
+
// const swaggerMethodInfo = swaggerMethod[method];
|
|
53
|
+
// // if(apiName == "/api/v{version}/Ticket/Create"){
|
|
54
|
+
// // debugger
|
|
55
|
+
// // }
|
|
56
|
+
// let parametersRefType = swaggerMethodInfo.parameters.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
|
|
57
|
+
// usedTypes = usedTypes.concat(parametersRefType);
|
|
58
|
+
// if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null) {
|
|
59
|
+
// usedTypes.push(swaggerMethodInfo.responses[200].content[contentType].schema.$ref.replace('#/components/schemas/', ''));
|
|
60
|
+
// }
|
|
61
|
+
// if (swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref) {
|
|
62
|
+
// usedTypes.push(swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref.replace('#/components/schemas/', ''));
|
|
63
|
+
// }
|
|
64
|
+
// }
|
|
65
|
+
// this.retrieveNestedObjects(usedTypes);
|
|
66
|
+
// usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
|
|
67
|
+
// // usedTypes = usedTypes.filter((value, index, array) => {
|
|
68
|
+
// // array.indexOf(value) === index;
|
|
69
|
+
// // });
|
|
70
|
+
// // usedTypes.forEach(element => {
|
|
71
|
+
// // console.debug(element);
|
|
72
|
+
// // });
|
|
73
|
+
// console.debug(`Start autogeneration Models`);
|
|
74
|
+
// let models = ``;
|
|
75
|
+
// for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
|
|
76
|
+
// const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
|
|
77
|
+
// if (modelName == 'ActivatedVoucherSnackListResponse') {
|
|
78
|
+
// console.debug("ActivatedVoucherSnackListResponse");
|
|
79
|
+
// }
|
|
80
|
+
// if (usedTypes.indexOf(modelName) < 0) {
|
|
81
|
+
// console.debug(`\tModel SKIP - ${modelName}`);
|
|
82
|
+
// continue
|
|
83
|
+
// };
|
|
84
|
+
// const swaggerCopmponent = this._swagger.components.schemas[modelName];
|
|
85
|
+
// console.debug(`\tModel - ${modelName}`);
|
|
86
|
+
// let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
|
|
87
|
+
// let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
|
|
88
|
+
// models +=
|
|
89
|
+
// `
|
|
90
|
+
// export ${type} ${modelName} {${content}
|
|
91
|
+
// }
|
|
92
|
+
// `;
|
|
93
|
+
// }
|
|
94
|
+
// fs.writeFileSync(outputDirectory + "/model.autogenerated.ts",
|
|
95
|
+
// `${modelPre}
|
|
96
|
+
// ${models}
|
|
97
|
+
// ${modelPost}`,
|
|
98
|
+
// { flag: 'w' });
|
|
99
|
+
// }
|
|
100
|
+
// retrieveParameters(swaggerMethodInfo: SwaggerMethod) {
|
|
101
|
+
// if (swaggerMethodInfo.requestBody != null) {
|
|
102
|
+
// return `request: ${swaggerMethodInfo.requestBody.content[contentType].schema.$ref.replace('#/components/schemas/', '')}`
|
|
103
|
+
// }
|
|
104
|
+
// let parameters = ``;
|
|
105
|
+
// swaggerMethodInfo.parameters.filter(x => x.in == 'query').forEach(parameter => {
|
|
106
|
+
// if (parameter.schema.$ref != null) {
|
|
107
|
+
// parameters += `${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
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: SwaggerMethod) {
|
|
117
|
+
// if (swaggerMethodInfo.requestBody != null) return ``;
|
|
118
|
+
// let filteredParameters = swaggerMethodInfo.parameters.filter(x => x.in == 'query');
|
|
119
|
+
// if (filteredParameters.length == 0) return ``;
|
|
120
|
+
// let parameters = `?`;
|
|
121
|
+
// filteredParameters.forEach(parameter => {
|
|
122
|
+
// if (parameter.schema.$ref != null) {
|
|
123
|
+
// this.parametrizeObject(parameter.schema.$ref)
|
|
124
|
+
// //parameters += `${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}&`;
|
|
125
|
+
// } else {
|
|
126
|
+
// parameters += `${this.lowercaseFirstLetter(parameter.name)}=\${` + this.lowercaseFirstLetter(parameter.name) + `}&`;
|
|
127
|
+
// }
|
|
128
|
+
// });
|
|
129
|
+
// if (parameters.length > 1)
|
|
130
|
+
// parameters = parameters.substring(0, parameters.length - 1);
|
|
131
|
+
// return parameters;
|
|
132
|
+
// }
|
|
133
|
+
// retrieveReturnType(swaggerMethodInfo: SwaggerMethod) {
|
|
134
|
+
// if (swaggerMethodInfo.responses[200] == null)
|
|
135
|
+
// return 'void';
|
|
136
|
+
// if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null)
|
|
137
|
+
// return swaggerMethodInfo.responses[200]?.content[contentType].schema.$ref.replace('#/components/schemas/', '')
|
|
138
|
+
// if (swaggerMethodInfo.responses[200]?.content[contentType].schema.type != null)
|
|
139
|
+
// return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentType].schema);
|
|
140
|
+
// console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
|
|
141
|
+
// throw new Error("unmanaged swaggerMethodInfo");
|
|
142
|
+
// }
|
|
143
|
+
// retrieveType(swaggerComponentProperty: SwaggerComponentProperty) {
|
|
144
|
+
// if (swaggerComponentProperty.$ref != null)
|
|
145
|
+
// return swaggerComponentProperty.$ref.replace('#/components/schemas/', '');
|
|
146
|
+
// if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
|
|
147
|
+
// if (swaggerComponentProperty.items.$ref != null)
|
|
148
|
+
// return swaggerComponentProperty.items.$ref.replace('#/components/schemas/', '');
|
|
149
|
+
// else
|
|
150
|
+
// return this.getNativeType(swaggerComponentProperty);
|
|
151
|
+
// if (swaggerComponentProperty.type != null)
|
|
152
|
+
// return this.getNativeType(swaggerComponentProperty);
|
|
153
|
+
// if (swaggerComponentProperty.type == null)
|
|
154
|
+
// return '';
|
|
155
|
+
// console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
|
|
156
|
+
// throw new Error("unmanaged swaggerMethodInfo");
|
|
157
|
+
// }
|
|
158
|
+
// parametrizeObject(objectName: string) {
|
|
159
|
+
// let component = this._swagger.components.schemas[objectName.replace('#/components/schemas/', '')];
|
|
160
|
+
// if (component == null || component.properties == null) return ``;
|
|
161
|
+
// console.debug(component.properties);
|
|
162
|
+
// return ``;
|
|
163
|
+
// }
|
|
164
|
+
// retrieveObjectContent(name: string, swaggerComponent: SwaggerComponent) {
|
|
165
|
+
// if (swaggerComponent.type == 'object')
|
|
166
|
+
// return this.retrieveObjectProperties(swaggerComponent);
|
|
167
|
+
// else if (swaggerComponent.type == 'integer')
|
|
168
|
+
// return this.retrieveEnumValues(name, swaggerComponent);
|
|
169
|
+
// }
|
|
170
|
+
// retrieveObjectProperties(swaggerCopmponent: SwaggerComponent) {
|
|
171
|
+
// if (swaggerCopmponent.properties == null) return ``;
|
|
172
|
+
// // console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
|
|
173
|
+
// let properties = ``;
|
|
174
|
+
// for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
175
|
+
// const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
176
|
+
// properties +=
|
|
177
|
+
// `
|
|
178
|
+
// ${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`
|
|
179
|
+
// }
|
|
180
|
+
// return properties;
|
|
181
|
+
// }
|
|
182
|
+
// retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
|
|
183
|
+
// if (swaggerCopmponent.enum == null) return ``;
|
|
184
|
+
// let properties = ``;
|
|
185
|
+
// for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
|
|
186
|
+
// const name = swaggerCopmponent.enum[index].split('-')[0].trim();
|
|
187
|
+
// const value = swaggerCopmponent.enum[index].split('-')[1].trim();
|
|
188
|
+
// properties +=
|
|
189
|
+
// `
|
|
190
|
+
// ${name} = ${value},`;
|
|
191
|
+
// }
|
|
192
|
+
// return properties;
|
|
193
|
+
// }
|
|
194
|
+
// retrieveNestedObjects(usedTypes: string[]) {
|
|
195
|
+
// for (let i = 0; i < usedTypes.length; i++) {
|
|
196
|
+
// const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
|
|
197
|
+
// // const name = usedTypes[i]
|
|
198
|
+
// // const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
|
|
199
|
+
// this.retrieveNestedObjects2(swaggerCopmponent, usedTypes);
|
|
200
|
+
// }
|
|
201
|
+
// }
|
|
202
|
+
// /*
|
|
203
|
+
// "ActivatedVoucherSnackReadQueryResponse": {
|
|
204
|
+
// "type": "object",
|
|
205
|
+
// "properties": {
|
|
206
|
+
// "list": {
|
|
207
|
+
// "type": "array",
|
|
208
|
+
// "items": {
|
|
209
|
+
// "$ref": "#/components/schemas/ActivatedVoucherSnackListResponse"
|
|
210
|
+
// },
|
|
211
|
+
// "nullable": true
|
|
212
|
+
// }
|
|
213
|
+
// },
|
|
214
|
+
// "additionalProperties": false
|
|
215
|
+
// },
|
|
216
|
+
// */
|
|
217
|
+
// retrieveNestedObjects2(swaggerComponent: SwaggerComponent, usedTypes: string[]) {
|
|
218
|
+
// if (!swaggerComponent.properties) return;
|
|
219
|
+
// for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
|
|
220
|
+
// const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
|
|
221
|
+
// let nestedUsedType = '';
|
|
222
|
+
// if (swaggerComponent.properties[propertyName].$ref != null) {
|
|
223
|
+
// nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
|
|
224
|
+
// } else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
|
|
225
|
+
// nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
|
|
226
|
+
// }
|
|
227
|
+
// if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
|
|
228
|
+
// usedTypes.push(nestedUsedType);
|
|
229
|
+
// let nested = this._swagger.components.schemas[nestedUsedType];
|
|
230
|
+
// this.retrieveNestedObjects2(nested, usedTypes);
|
|
231
|
+
// }
|
|
232
|
+
// }
|
|
233
|
+
// }
|
|
234
|
+
// getNativeType(schema: SwaggerSchema): string {
|
|
235
|
+
// if (schema.type == 'integer') return 'number';
|
|
236
|
+
// if (schema.type == 'string' && schema.format == null) return 'string';
|
|
237
|
+
// if (schema.type == 'string' && schema.format == 'date-time') return 'moment.Moment';
|
|
238
|
+
// if (schema.type == 'string' && schema.format == 'uuid') return 'string';
|
|
239
|
+
// if (schema.type == 'number') return 'number';
|
|
240
|
+
// if (schema.type == 'array') return '[]';
|
|
241
|
+
// if (schema.type == 'boolean') return 'boolean';
|
|
242
|
+
// if (schema.type == 'object') return 'any';
|
|
243
|
+
// console.error("unmanaged schema type", schema);
|
|
244
|
+
// throw new Error("unmanaged schema");
|
|
245
|
+
// }
|
|
246
|
+
// lowercaseFirstLetter(value: string) {
|
|
247
|
+
// return value.charAt(0).toLowerCase() + value.slice(1);
|
|
248
|
+
// }
|
|
249
|
+
// }
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
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
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'fs';
|
|
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();
|
|
14
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`;
|
|
16
|
+
const contentType = 'application/json';
|
|
15
17
|
export class Generator {
|
|
16
18
|
_swagger;
|
|
17
19
|
constructor(swagger) {
|
|
@@ -50,9 +52,9 @@ export class Generator {
|
|
|
50
52
|
}
|
|
51
53
|
`;
|
|
52
54
|
}
|
|
53
|
-
fs.writeFileSync("
|
|
55
|
+
fs.writeFileSync(outputDirectory + "/api.autogenerated.ts", `${apiPre}
|
|
54
56
|
${apiMethods}
|
|
55
|
-
${apiPost}
|
|
57
|
+
${apiPost}`, { flag: 'w' });
|
|
56
58
|
}
|
|
57
59
|
generateModel() {
|
|
58
60
|
let usedTypes = [];
|
|
@@ -61,6 +63,9 @@ ${apiPost}`);
|
|
|
61
63
|
const swaggerMethod = this._swagger.paths[apiName];
|
|
62
64
|
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
63
65
|
const swaggerMethodInfo = swaggerMethod[method];
|
|
66
|
+
// if(apiName == "/api/v{version}/Ticket/Create"){
|
|
67
|
+
// debugger
|
|
68
|
+
// }
|
|
64
69
|
let parametersRefType = swaggerMethodInfo.parameters.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''));
|
|
65
70
|
usedTypes = usedTypes.concat(parametersRefType);
|
|
66
71
|
if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null) {
|
|
@@ -70,6 +75,11 @@ ${apiPost}`);
|
|
|
70
75
|
usedTypes.push(swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref.replace('#/components/schemas/', ''));
|
|
71
76
|
}
|
|
72
77
|
}
|
|
78
|
+
this.retrieveNestedObjects(usedTypes);
|
|
79
|
+
usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
|
|
80
|
+
// usedTypes = usedTypes.filter((value, index, array) => {
|
|
81
|
+
// array.indexOf(value) === index;
|
|
82
|
+
// });
|
|
73
83
|
// usedTypes.forEach(element => {
|
|
74
84
|
// console.debug(element);
|
|
75
85
|
// });
|
|
@@ -77,6 +87,9 @@ ${apiPost}`);
|
|
|
77
87
|
let models = ``;
|
|
78
88
|
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
|
|
79
89
|
const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
|
|
90
|
+
if (modelName == 'ActivatedVoucherSnackListResponse') {
|
|
91
|
+
console.debug("ActivatedVoucherSnackListResponse");
|
|
92
|
+
}
|
|
80
93
|
if (usedTypes.indexOf(modelName) < 0) {
|
|
81
94
|
console.debug(`\tModel SKIP - ${modelName}`);
|
|
82
95
|
continue;
|
|
@@ -85,16 +98,16 @@ ${apiPost}`);
|
|
|
85
98
|
const swaggerCopmponent = this._swagger.components.schemas[modelName];
|
|
86
99
|
console.debug(`\tModel - ${modelName}`);
|
|
87
100
|
let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
|
|
88
|
-
let content = this.retrieveObjectContent(swaggerCopmponent);
|
|
101
|
+
let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
|
|
89
102
|
models +=
|
|
90
103
|
`
|
|
91
104
|
export ${type} ${modelName} {${content}
|
|
92
105
|
}
|
|
93
106
|
`;
|
|
94
107
|
}
|
|
95
|
-
fs.writeFileSync("
|
|
108
|
+
fs.writeFileSync(outputDirectory + "/model.autogenerated.ts", `${modelPre}
|
|
96
109
|
${models}
|
|
97
|
-
${modelPost}
|
|
110
|
+
${modelPost}`, { flag: 'w' });
|
|
98
111
|
}
|
|
99
112
|
retrieveParameters(swaggerMethodInfo) {
|
|
100
113
|
if (swaggerMethodInfo.requestBody != null) {
|
|
@@ -153,6 +166,8 @@ ${modelPost}`);
|
|
|
153
166
|
return this.getNativeType(swaggerComponentProperty);
|
|
154
167
|
if (swaggerComponentProperty.type != null)
|
|
155
168
|
return this.getNativeType(swaggerComponentProperty);
|
|
169
|
+
if (swaggerComponentProperty.type == null)
|
|
170
|
+
return '';
|
|
156
171
|
console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
|
|
157
172
|
throw new Error("unmanaged swaggerMethodInfo");
|
|
158
173
|
}
|
|
@@ -163,11 +178,11 @@ ${modelPost}`);
|
|
|
163
178
|
console.debug(component.properties);
|
|
164
179
|
return ``;
|
|
165
180
|
}
|
|
166
|
-
retrieveObjectContent(swaggerComponent) {
|
|
181
|
+
retrieveObjectContent(name, swaggerComponent) {
|
|
167
182
|
if (swaggerComponent.type == 'object')
|
|
168
183
|
return this.retrieveObjectProperties(swaggerComponent);
|
|
169
184
|
else if (swaggerComponent.type == 'integer')
|
|
170
|
-
return this.retrieveEnumValues(swaggerComponent);
|
|
185
|
+
return this.retrieveEnumValues(name, swaggerComponent);
|
|
171
186
|
}
|
|
172
187
|
retrieveObjectProperties(swaggerCopmponent) {
|
|
173
188
|
if (swaggerCopmponent.properties == null)
|
|
@@ -182,20 +197,58 @@ ${modelPost}`);
|
|
|
182
197
|
}
|
|
183
198
|
return properties;
|
|
184
199
|
}
|
|
185
|
-
retrieveEnumValues(swaggerCopmponent) {
|
|
200
|
+
retrieveEnumValues(name, swaggerCopmponent) {
|
|
201
|
+
if (swaggerCopmponent.enum == null)
|
|
202
|
+
return ``;
|
|
203
|
+
let properties = ``;
|
|
204
|
+
for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
|
|
205
|
+
const name = swaggerCopmponent.enum[index].split('-')[0].trim();
|
|
206
|
+
const value = swaggerCopmponent.enum[index].split('-')[1].trim();
|
|
207
|
+
properties +=
|
|
208
|
+
`
|
|
209
|
+
${name} = ${value},`;
|
|
210
|
+
}
|
|
211
|
+
return properties;
|
|
186
212
|
}
|
|
187
|
-
retrieveNestedObjects(
|
|
188
|
-
let
|
|
189
|
-
|
|
190
|
-
const
|
|
191
|
-
this.
|
|
213
|
+
retrieveNestedObjects(usedTypes) {
|
|
214
|
+
for (let i = 0; i < usedTypes.length; i++) {
|
|
215
|
+
const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
|
|
216
|
+
// const name = usedTypes[i]
|
|
217
|
+
// const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
|
|
218
|
+
this.retrieveNestedObjects2(swaggerCopmponent, usedTypes);
|
|
192
219
|
}
|
|
193
220
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
221
|
+
/*
|
|
222
|
+
"ActivatedVoucherSnackReadQueryResponse": {
|
|
223
|
+
"type": "object",
|
|
224
|
+
"properties": {
|
|
225
|
+
"list": {
|
|
226
|
+
"type": "array",
|
|
227
|
+
"items": {
|
|
228
|
+
"$ref": "#/components/schemas/ActivatedVoucherSnackListResponse"
|
|
229
|
+
},
|
|
230
|
+
"nullable": true
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
"additionalProperties": false
|
|
234
|
+
},
|
|
235
|
+
*/
|
|
236
|
+
retrieveNestedObjects2(swaggerComponent, usedTypes) {
|
|
237
|
+
if (!swaggerComponent.properties)
|
|
238
|
+
return;
|
|
239
|
+
for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
|
|
240
|
+
const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
|
|
241
|
+
let nestedUsedType = '';
|
|
242
|
+
if (swaggerComponent.properties[propertyName].$ref != null) {
|
|
243
|
+
nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
|
|
244
|
+
}
|
|
245
|
+
else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
|
|
246
|
+
nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
|
|
247
|
+
}
|
|
248
|
+
if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
|
|
249
|
+
usedTypes.push(nestedUsedType);
|
|
250
|
+
let nested = this._swagger.components.schemas[nestedUsedType];
|
|
251
|
+
this.retrieveNestedObjects2(nested, usedTypes);
|
|
199
252
|
}
|
|
200
253
|
}
|
|
201
254
|
}
|
|
@@ -214,6 +267,8 @@ ${modelPost}`);
|
|
|
214
267
|
return '[]';
|
|
215
268
|
if (schema.type == 'boolean')
|
|
216
269
|
return 'boolean';
|
|
270
|
+
if (schema.type == 'object')
|
|
271
|
+
return 'any';
|
|
217
272
|
console.error("unmanaged schema type", schema);
|
|
218
273
|
throw new Error("unmanaged schema");
|
|
219
274
|
}
|
|
@@ -253,12 +308,13 @@ export class ApiService extends ApiBase {
|
|
|
253
308
|
}
|
|
254
309
|
`;
|
|
255
310
|
const apiPost = `}`;
|
|
256
|
-
const modelPre = `
|
|
311
|
+
const modelPre = `import * as moment from 'moment';
|
|
257
312
|
`;
|
|
258
313
|
const modelPost = `
|
|
259
314
|
`;
|
|
260
|
-
swaggerDownloader.download()
|
|
315
|
+
swaggerDownloader.download(new URL(swaggerJsonUrl))
|
|
261
316
|
.then(swaggerDoc => {
|
|
262
317
|
return new Generator(swaggerDoc);
|
|
263
318
|
})
|
|
264
319
|
.then(generator => { generator.generateApi(); generator.generateModel(); });
|
|
320
|
+
// require('./index.js')({swaggerDownloader});
|
|
@@ -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,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devlearning/swagger-generator",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
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
|
+
"build": "ts-node --transpile-only ./src/index.ts"
|
|
9
10
|
},
|
|
10
11
|
"bin": {
|
|
11
|
-
"swgen": "./
|
|
12
|
+
"swgen": "./dist/index.js"
|
|
12
13
|
},
|
|
13
14
|
"keywords": [],
|
|
14
15
|
"author": "",
|