@devlearning/swagger-generator 0.0.8 → 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.
@@ -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,249 +1,252 @@
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
- // }
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
+ }