@devlearning/swagger-generator 1.1.1 → 1.1.3
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/.vscode/launch.json +28 -28
- package/README.md +209 -209
- package/dist/api.constants.d.ts +2 -0
- package/dist/api.constants.js +22 -22
- package/dist/generator-old.d.ts +0 -0
- package/dist/generator-old.js +1 -1
- package/dist/generator.d.ts +41 -0
- package/dist/generator.js +63 -45
- package/dist/generators-writers/angular/api-angular-writer.d.ts +15 -0
- package/dist/generators-writers/angular/api-angular-writer.js +22 -19
- package/dist/generators-writers/angular/constants.d.ts +4 -0
- package/dist/generators-writers/angular/constants.js +24 -24
- package/dist/generators-writers/angular/model-angular-writer.d.ts +10 -0
- package/dist/generators-writers/angular/model-angular-writer.js +11 -8
- package/dist/generators-writers/angular/normalizator.d.ts +3 -0
- package/dist/generators-writers/angular/normalizator.js +37 -0
- package/dist/generators-writers/dart/api-dart-writer.d.ts +11 -0
- package/dist/generators-writers/dart/api-dart-writer.js +2 -2
- package/dist/generators-writers/dart/model-dart-writer.d.ts +9 -0
- package/dist/generators-writers/dart/model-dart-writer.js +2 -2
- package/dist/generators-writers/dart/models/import-definition-dart.d.ts +5 -0
- package/dist/generators-writers/dart/normalizator.d.ts +10 -0
- package/dist/generators-writers/dart/templates/api.mustache +49 -49
- package/dist/generators-writers/dart/templates/enum.mustache +14 -14
- package/dist/generators-writers/dart/templates/model.mustache +17 -17
- package/dist/generators-writers/nextjs/api-nextjs-writer.d.ts +15 -0
- package/dist/generators-writers/nextjs/api-nextjs-writer.js +13 -13
- package/dist/generators-writers/nextjs/constants.d.ts +1 -0
- package/dist/generators-writers/nextjs/constants.js +4 -4
- package/dist/generators-writers/nextjs/model-nextjs-writer.d.ts +10 -0
- package/dist/generators-writers/nextjs/model-nextjs-writer.js +6 -6
- package/dist/generators-writers/utils.d.ts +13 -0
- package/dist/generators-writers/utils.js +9 -1
- package/dist/index.d.ts +17 -0
- package/dist/model.constants.d.ts +0 -0
- package/dist/model.constants.js +1 -1
- package/dist/models/api-dto.d.ts +18 -0
- package/dist/models/enum-value-dto.d.ts +4 -0
- package/dist/models/model-dto.d.ts +9 -0
- package/dist/models/parameter-dto.d.ts +7 -0
- package/dist/models/property-dto.d.ts +4 -0
- package/dist/models/swagger/swagger-component-property.d.ts +1 -0
- package/dist/models/swagger/swagger-component.d.ts +10 -0
- package/dist/models/swagger/swagger-content.d.ts +4 -0
- package/dist/models/swagger/swagger-info.d.ts +4 -0
- package/dist/models/swagger/swagger-method.d.ts +9 -0
- package/dist/models/swagger/swagger-schema.d.ts +12 -0
- package/dist/models/swagger/swagger.d.ts +47 -0
- package/dist/models/type-dto.d.ts +8 -0
- package/dist/swagger-downloader.d.ts +5 -0
- package/dist/templates/api.mustache +29 -0
- package/dist/templates/model.mustache +18 -0
- package/package.json +45 -45
- package/src/api.constants.ts +26 -26
- package/src/generator-old.ts +449 -449
- package/src/generator.ts +599 -582
- package/src/generators-writers/angular/api-angular-writer.ts +146 -141
- package/src/generators-writers/angular/constants.ts +36 -36
- package/src/generators-writers/angular/model-angular-writer.ts +65 -62
- package/src/generators-writers/angular/normalizator.ts +42 -0
- package/src/generators-writers/dart/api-dart-writer.ts +198 -198
- package/src/generators-writers/dart/model-dart-writer.ts +162 -162
- package/src/generators-writers/dart/models/import-definition-dart.ts +5 -5
- package/src/generators-writers/dart/normalizator.ts +72 -72
- package/src/generators-writers/dart/templates/api.mustache +49 -49
- package/src/generators-writers/dart/templates/enum.mustache +14 -14
- package/src/generators-writers/dart/templates/model.mustache +17 -17
- package/src/generators-writers/nextjs/api-nextjs-writer.ts +156 -156
- package/src/generators-writers/nextjs/constants.ts +5 -5
- package/src/generators-writers/nextjs/model-nextjs-writer.ts +61 -61
- package/src/generators-writers/utils.ts +90 -78
- package/src/index.ts +96 -96
- package/src/models/api-dto.ts +17 -17
- package/src/models/enum-value-dto.ts +3 -3
- package/src/models/model-dto.ts +9 -9
- package/src/models/parameter-dto.ts +7 -7
- package/src/models/property-dto.ts +4 -4
- package/src/models/swagger/swagger-component-property.ts +11 -11
- package/src/models/swagger/swagger-component.ts +17 -17
- package/src/models/swagger/swagger-content.ts +4 -4
- package/src/models/swagger/swagger-info.ts +3 -3
- package/src/models/swagger/swagger-method.ts +7 -7
- package/src/models/swagger/swagger-schema.ts +20 -20
- package/src/models/swagger/swagger.ts +38 -38
- package/src/models/type-dto.ts +7 -7
- package/src/swagger-downloader.ts +12 -12
- package/tsconfig.json +33 -28
package/dist/generator.js
CHANGED
|
@@ -68,32 +68,41 @@ export class Generator {
|
|
|
68
68
|
this._barModels.start(Object.getOwnPropertyNames(this._swagger.paths).length, 0);
|
|
69
69
|
let usedTypes = [];
|
|
70
70
|
let usedMultiPart = [];
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
71
|
+
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
72
|
+
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
73
|
+
const apiSwaggerMethodKey = this._swagger.paths[apiName];
|
|
74
|
+
const apiMethod = Object.getOwnPropertyNames(apiSwaggerMethodKey)[0];
|
|
75
|
+
const apiSwaggerMethod = apiSwaggerMethodKey[apiMethod];
|
|
76
|
+
// const parametersRefType = apiSwaggerMethod.parameters?.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''));
|
|
77
|
+
// if (parametersRefType) {
|
|
78
|
+
// usedTypes = usedTypes.concat(parametersRefType);
|
|
79
|
+
// }
|
|
80
|
+
// const responseRefType = this.computeRequestBodyType(apiSwaggerMethod!);
|
|
81
|
+
// if (responseRefType && !responseRefType.isVoid && !responseRefType.isNativeType) {
|
|
82
|
+
// usedTypes = usedTypes.concat(responseRefType.typeName);
|
|
83
|
+
// }
|
|
84
|
+
// const contentRefType = this.computeResponseType(apiSwaggerMethod!);
|
|
85
|
+
// if (contentRefType && !contentRefType.isVoid && !contentRefType.isNativeType) {
|
|
86
|
+
// usedTypes = usedTypes.concat(contentRefType.typeName);
|
|
87
|
+
// }
|
|
88
|
+
if (apiSwaggerMethod.requestBody?.content[contentTypeMultipartFormData]?.schema != null) {
|
|
89
|
+
usedMultiPart.push(apiName);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
89
92
|
for (let index = 0; index < this._apis.length; index++) {
|
|
90
93
|
if (this._apis[index].returnType && this._apis[index].returnType.isTypeReference) {
|
|
91
94
|
usedTypes.push(this._apis[index].returnType);
|
|
95
|
+
// if (this._apis[index].returnType!.typeName.startsWith("hotac_")) {
|
|
96
|
+
// debugger
|
|
97
|
+
// }
|
|
92
98
|
}
|
|
93
99
|
if (this._apis[index].parameters) {
|
|
94
100
|
this._apis[index].parameters.forEach(parameter => {
|
|
95
101
|
if (parameter.isTypeReference) {
|
|
96
102
|
usedTypes.push(parameter);
|
|
103
|
+
// if (parameter.typeName.startsWith("hotac_")) {
|
|
104
|
+
// debugger
|
|
105
|
+
// }
|
|
97
106
|
}
|
|
98
107
|
});
|
|
99
108
|
}
|
|
@@ -121,14 +130,14 @@ export class Generator {
|
|
|
121
130
|
const swaggerMethod = this._swagger.paths[apiName];
|
|
122
131
|
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
123
132
|
const swaggerMethodInfo = swaggerMethod[method];
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
133
|
+
const schema = swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData].schema;
|
|
134
|
+
this._models.push({
|
|
135
|
+
typeName: this.getApiNameNormalized(apiName),
|
|
136
|
+
modelType: 'class',
|
|
137
|
+
name: this.getApiNameNormalized(apiName),
|
|
138
|
+
properties: this.retrieveSchemaProperties(schema),
|
|
139
|
+
enumValues: [],
|
|
140
|
+
});
|
|
132
141
|
});
|
|
133
142
|
}
|
|
134
143
|
generateApi() {
|
|
@@ -401,17 +410,22 @@ export class Generator {
|
|
|
401
410
|
}
|
|
402
411
|
return properties;
|
|
403
412
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
413
|
+
retrieveSchemaProperties(schema) {
|
|
414
|
+
if (schema.properties == null)
|
|
415
|
+
return [];
|
|
416
|
+
let properties = [];
|
|
417
|
+
for (let index = 0; index < Object.getOwnPropertyNames(schema.properties).length; index++) {
|
|
418
|
+
const propertyName = Object.getOwnPropertyNames(schema.properties)[index];
|
|
419
|
+
const property = schema.properties[propertyName];
|
|
420
|
+
const type = this.retrieveType(property);
|
|
421
|
+
properties.push({
|
|
422
|
+
...type,
|
|
423
|
+
name: propertyName,
|
|
424
|
+
nullable: type.nullable,
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
return properties;
|
|
428
|
+
}
|
|
415
429
|
retrieveEnumValues(name, swaggerCopmponent) {
|
|
416
430
|
if (swaggerCopmponent.enum == null)
|
|
417
431
|
return [];
|
|
@@ -432,14 +446,16 @@ export class Generator {
|
|
|
432
446
|
retrieveNestedObjects(usedTypes) {
|
|
433
447
|
for (let i = 0; i < usedTypes.length; i++) {
|
|
434
448
|
const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i].typeName];
|
|
449
|
+
if (!swaggerCopmponent)
|
|
450
|
+
continue;
|
|
435
451
|
this.retrieveNestedObjectsRecursive(swaggerCopmponent, usedTypes);
|
|
436
452
|
}
|
|
437
453
|
}
|
|
438
454
|
retrieveNestedObjectsRecursive(swaggerComponent, usedTypes) {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
455
|
+
if (!swaggerComponent.properties)
|
|
456
|
+
return;
|
|
457
|
+
for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
|
|
458
|
+
try {
|
|
443
459
|
const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
|
|
444
460
|
let nestedUsedType = '';
|
|
445
461
|
if (!swaggerComponent.properties[propertyName])
|
|
@@ -468,9 +484,9 @@ export class Generator {
|
|
|
468
484
|
this.retrieveNestedObjectsRecursive(nested, usedTypes);
|
|
469
485
|
}
|
|
470
486
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
487
|
+
catch (error) {
|
|
488
|
+
debugger;
|
|
489
|
+
}
|
|
474
490
|
}
|
|
475
491
|
}
|
|
476
492
|
getNativeType(schema) {
|
|
@@ -489,6 +505,8 @@ export class Generator {
|
|
|
489
505
|
nativeType = 'string';
|
|
490
506
|
if (schema.type == 'string' && schema.format == 'date-time')
|
|
491
507
|
nativeType = 'dateTime';
|
|
508
|
+
if (schema.type == 'string' && schema.format == 'date-time-local-tz')
|
|
509
|
+
nativeType = 'dateTime';
|
|
492
510
|
if (schema.type == 'string' && schema.format == 'uuid')
|
|
493
511
|
nativeType = 'string';
|
|
494
512
|
if (schema.type == 'string' && schema.format == 'binary')
|
|
@@ -513,7 +531,7 @@ export class Generator {
|
|
|
513
531
|
if (normalizedApiName.charAt(0) == '_') {
|
|
514
532
|
normalizedApiName = normalizedApiName.slice(1);
|
|
515
533
|
}
|
|
516
|
-
return
|
|
534
|
+
return normalizedApiName;
|
|
517
535
|
}
|
|
518
536
|
toFirstLetterLowercase(value) {
|
|
519
537
|
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ApiDto } from '@src/models/api-dto.js';
|
|
2
|
+
export declare class ApiAngularWriter {
|
|
3
|
+
private _outputDirectory;
|
|
4
|
+
constructor(_outputDirectory: string);
|
|
5
|
+
write(apis: ApiDto[]): void;
|
|
6
|
+
private _apiString;
|
|
7
|
+
private _parameters;
|
|
8
|
+
private _returnType;
|
|
9
|
+
private _queryParametersPreparation;
|
|
10
|
+
private _queryParametersPreparationStatement;
|
|
11
|
+
private _queryParameters;
|
|
12
|
+
private _queryParametersStatement;
|
|
13
|
+
private _requestPreparation;
|
|
14
|
+
private _writeFile;
|
|
15
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import { API_POST, API_PRE } from './constants.js';
|
|
3
3
|
import { Utils } from '../utils.js';
|
|
4
|
+
import { Normalizator } from './normalizator.js';
|
|
4
5
|
export class ApiAngularWriter {
|
|
5
6
|
_outputDirectory;
|
|
6
7
|
constructor(_outputDirectory) {
|
|
@@ -14,7 +15,7 @@ export class ApiAngularWriter {
|
|
|
14
15
|
this._writeFile(apiString);
|
|
15
16
|
}
|
|
16
17
|
_apiString(api) {
|
|
17
|
-
let apiNameNormalized = Utils.
|
|
18
|
+
let apiNameNormalized = Utils.getNormalizedApiPathAngular(api.name);
|
|
18
19
|
let parametersString = this._parameters(api);
|
|
19
20
|
let queryParametersPreparation = this._queryParametersPreparation(api);
|
|
20
21
|
let requestPreparation = this._requestPreparation(api);
|
|
@@ -22,23 +23,25 @@ export class ApiAngularWriter {
|
|
|
22
23
|
let returnTypeString = this._returnType(api);
|
|
23
24
|
let haveRequest = api.haveRequest;
|
|
24
25
|
let method = api.method.toLowerCase();
|
|
25
|
-
let httpOptions = api.isMultiPart ? '
|
|
26
|
-
let apiString = `
|
|
27
|
-
public ${apiNameNormalized}(${parametersString}): Observable<${returnTypeString}> {
|
|
28
|
-
${queryParametersPreparation}${requestPreparation}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${api.url}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
|
|
29
|
-
.pipe(
|
|
30
|
-
map(x => this._handleResponse(x)),
|
|
31
|
-
catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
|
|
32
|
-
);
|
|
33
|
-
}
|
|
26
|
+
let httpOptions = api.isMultiPart ? 'httpOptionsMultipart' : 'httpOptions';
|
|
27
|
+
let apiString = `
|
|
28
|
+
public ${apiNameNormalized}(${parametersString}): Observable<${returnTypeString}> {
|
|
29
|
+
${queryParametersPreparation}${requestPreparation}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${api.url}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
|
|
30
|
+
.pipe(
|
|
31
|
+
map(x => this._handleResponse(x)),
|
|
32
|
+
catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
|
|
33
|
+
);
|
|
34
|
+
}
|
|
34
35
|
`;
|
|
35
36
|
return apiString;
|
|
36
37
|
}
|
|
37
38
|
_parameters(api) {
|
|
38
39
|
let parametersString = '';
|
|
39
40
|
api.parameters.forEach(parameter => {
|
|
41
|
+
const libraryDate = 'moment.Moment';
|
|
40
42
|
const prefixType = parameter.isEnum || !parameter.isNativeType ? 'Models.' : '';
|
|
41
|
-
|
|
43
|
+
const typeName = parameter.typeName === 'dateTime' ? libraryDate : `${prefixType}${Normalizator.mapTsTypeToAngular(parameter.typeName)}`;
|
|
44
|
+
parametersString += `${parameter.name}${parameter.nullable ? '?' : ''}: ${typeName}, `;
|
|
42
45
|
});
|
|
43
46
|
if (api.parameters.length > 0)
|
|
44
47
|
parametersString = parametersString.substring(0, parametersString.length - 2);
|
|
@@ -47,7 +50,7 @@ export class ApiAngularWriter {
|
|
|
47
50
|
_returnType(api) {
|
|
48
51
|
const prefixType = !api.returnType?.isNativeType ? 'Models.' : '';
|
|
49
52
|
const isArray = api.returnType?.isArray ? '[]' : '';
|
|
50
|
-
return api.returnType ? `${prefixType}${api.returnType.typeName}${isArray}` : 'any';
|
|
53
|
+
return api.returnType ? `${prefixType}${Normalizator.mapTsTypeToAngular(api.returnType.typeName)}${isArray}` : 'any';
|
|
51
54
|
}
|
|
52
55
|
_queryParametersPreparation(api) {
|
|
53
56
|
let queryParametersPreparation = '';
|
|
@@ -61,21 +64,21 @@ export class ApiAngularWriter {
|
|
|
61
64
|
_queryParametersPreparationStatement(parameter) {
|
|
62
65
|
if (parameter.nullable) {
|
|
63
66
|
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
64
|
-
return `let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined && ${parameter.name}.isValid() ? encodeURIComponent(this._momentToString(${parameter.name})) : '';
|
|
67
|
+
return `let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined && ${parameter.name}.isValid() ? encodeURIComponent(this._momentToString(${parameter.name})) : '';
|
|
65
68
|
`;
|
|
66
69
|
}
|
|
67
70
|
else {
|
|
68
|
-
return `let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined ? encodeURIComponent('' + ${parameter.name}) : '';
|
|
71
|
+
return `let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined ? encodeURIComponent('' + ${parameter.name}) : '';
|
|
69
72
|
`;
|
|
70
73
|
}
|
|
71
74
|
}
|
|
72
75
|
else {
|
|
73
76
|
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
74
|
-
return `let ${parameter.name}Param: string = encodeURIComponent(this._momentToString(${parameter.name}));
|
|
77
|
+
return `let ${parameter.name}Param: string = encodeURIComponent(this._momentToString(${parameter.name}));
|
|
75
78
|
`;
|
|
76
79
|
}
|
|
77
80
|
else {
|
|
78
|
-
return `let ${parameter.name}Param: string = encodeURIComponent('' + ${parameter.name});
|
|
81
|
+
return `let ${parameter.name}Param: string = encodeURIComponent('' + ${parameter.name});
|
|
79
82
|
`;
|
|
80
83
|
}
|
|
81
84
|
}
|
|
@@ -103,12 +106,12 @@ export class ApiAngularWriter {
|
|
|
103
106
|
if (!api.haveRequest) {
|
|
104
107
|
return '';
|
|
105
108
|
}
|
|
106
|
-
return `let wrappedRequest = this._handleRequest(request);
|
|
109
|
+
return `let wrappedRequest = this._handleRequest(request);
|
|
107
110
|
`;
|
|
108
111
|
}
|
|
109
112
|
_writeFile(apis) {
|
|
110
|
-
fs.writeFileSync(this._outputDirectory + "/api.autogenerated.ts", `${API_PRE}
|
|
111
|
-
${apis}
|
|
113
|
+
fs.writeFileSync(this._outputDirectory + "/api.autogenerated.ts", `${API_PRE}
|
|
114
|
+
${apis}
|
|
112
115
|
${API_POST}`, { flag: 'w' });
|
|
113
116
|
}
|
|
114
117
|
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const API_PRE = "import { HttpClient } from '@angular/common/http';\nimport { Observable, catchError, map } from 'rxjs';\nimport * as Models from './model.autogenerated';\nimport { HttpHeaders } from \"@angular/common/http\";\n\nexport const httpOptions = {\n headers: new HttpHeaders({ 'Content-Type': 'application/json' }),\n};\n\nexport const httpOptionsMultipart = {};\n\nexport abstract class ApiAutogeneratedService {\n constructor(\n public _http: HttpClient,\n public _baseUrl: string,\n ) { }\n\n protected abstract _momentToString(moment: moment.Moment): string;\n protected abstract _handleRequest<T>(request: T): T;\n protected abstract _handleMultipart<T>(request: T): FormData;\n protected abstract _handleResponse<T>(response: T): T;\n protected abstract _handleError(error: any, obs: any): Observable<never>;\n";
|
|
2
|
+
export declare const API_POST = "}";
|
|
3
|
+
export declare const MODEL_PRE = "import * as moment from 'moment';\n ";
|
|
4
|
+
export declare const MODEL_POST = "\n";
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
export const API_PRE = `import { HttpClient } from '@angular/common/http';
|
|
2
|
-
import { Observable, catchError, map } from 'rxjs';
|
|
3
|
-
import * as Models from './model.autogenerated';
|
|
4
|
-
import { HttpHeaders } from "@angular/common/http";
|
|
5
|
-
|
|
6
|
-
export const httpOptions = {
|
|
7
|
-
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export const httpOptionsMultipart = {};
|
|
11
|
-
|
|
12
|
-
export abstract class ApiAutogeneratedService {
|
|
13
|
-
constructor(
|
|
14
|
-
public _http: HttpClient,
|
|
15
|
-
public _baseUrl: string,
|
|
16
|
-
) { }
|
|
17
|
-
|
|
18
|
-
protected abstract _momentToString(moment: moment.Moment): string;
|
|
19
|
-
protected abstract _handleRequest<T>(request: T): T;
|
|
20
|
-
protected abstract _handleMultipart<T>(request: T): FormData;
|
|
21
|
-
protected abstract _handleResponse<T>(response: T): T;
|
|
22
|
-
protected abstract _handleError(error: any, obs: any): Observable<never>;
|
|
1
|
+
export const API_PRE = `import { HttpClient } from '@angular/common/http';
|
|
2
|
+
import { Observable, catchError, map } from 'rxjs';
|
|
3
|
+
import * as Models from './model.autogenerated';
|
|
4
|
+
import { HttpHeaders } from "@angular/common/http";
|
|
5
|
+
|
|
6
|
+
export const httpOptions = {
|
|
7
|
+
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const httpOptionsMultipart = {};
|
|
11
|
+
|
|
12
|
+
export abstract class ApiAutogeneratedService {
|
|
13
|
+
constructor(
|
|
14
|
+
public _http: HttpClient,
|
|
15
|
+
public _baseUrl: string,
|
|
16
|
+
) { }
|
|
17
|
+
|
|
18
|
+
protected abstract _momentToString(moment: moment.Moment): string;
|
|
19
|
+
protected abstract _handleRequest<T>(request: T): T;
|
|
20
|
+
protected abstract _handleMultipart<T>(request: T): FormData;
|
|
21
|
+
protected abstract _handleResponse<T>(response: T): T;
|
|
22
|
+
protected abstract _handleError(error: any, obs: any): Observable<never>;
|
|
23
23
|
`;
|
|
24
24
|
export const API_POST = `}`;
|
|
25
|
-
export const MODEL_PRE = `import * as moment from 'moment';
|
|
25
|
+
export const MODEL_PRE = `import * as moment from 'moment';
|
|
26
26
|
`;
|
|
27
|
-
export const MODEL_POST = `
|
|
27
|
+
export const MODEL_POST = `
|
|
28
28
|
`;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ModelDto } from '@src/models/model-dto.js';
|
|
2
|
+
export declare class ModelAngularWriter {
|
|
3
|
+
private _outputDirectory;
|
|
4
|
+
constructor(_outputDirectory: string);
|
|
5
|
+
write(models: ModelDto[]): void;
|
|
6
|
+
private _modelString;
|
|
7
|
+
private _properties;
|
|
8
|
+
private _enumValues;
|
|
9
|
+
private _writeFile;
|
|
10
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import { MODEL_POST, MODEL_PRE } from './constants.js';
|
|
3
|
+
import { Normalizator } from './normalizator.js';
|
|
4
|
+
import { Utils } from '../utils.js';
|
|
3
5
|
export class ModelAngularWriter {
|
|
4
6
|
_outputDirectory;
|
|
5
7
|
constructor(_outputDirectory) {
|
|
@@ -13,10 +15,10 @@ export class ModelAngularWriter {
|
|
|
13
15
|
this._writeFile(modelString);
|
|
14
16
|
}
|
|
15
17
|
_modelString(model) {
|
|
16
|
-
let modelString = `
|
|
17
|
-
export ${model.modelType} ${model.name} {
|
|
18
|
-
${this._properties(model)}${this._enumValues(model)}
|
|
19
|
-
}
|
|
18
|
+
let modelString = `
|
|
19
|
+
export ${model.modelType} ${Utils.toPascalCase(model.name)} {
|
|
20
|
+
${this._properties(model)}${this._enumValues(model)}
|
|
21
|
+
}
|
|
20
22
|
`;
|
|
21
23
|
return modelString;
|
|
22
24
|
}
|
|
@@ -25,8 +27,9 @@ ${this._properties(model)}${this._enumValues(model)}
|
|
|
25
27
|
model.properties.forEach(property => {
|
|
26
28
|
//const libraryDate = this._commandLineArgs.dateTimeLibrary == DateTimeLibrary.Moment ? 'moment.Moment' : 'Date'
|
|
27
29
|
const libraryDate = 'moment.Moment';
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
+
const isArray = property.isArray ? '[]' : '';
|
|
31
|
+
const typeName = property.typeName === 'dateTime' ? libraryDate : Normalizator.mapTsTypeToAngular(property.typeName);
|
|
32
|
+
propertiesString += ` ${property.name}${property.nullable ? '?' : ''}: ${typeName}${isArray};\n`;
|
|
30
33
|
});
|
|
31
34
|
return propertiesString.trimEnd();
|
|
32
35
|
}
|
|
@@ -38,8 +41,8 @@ ${this._properties(model)}${this._enumValues(model)}
|
|
|
38
41
|
return enumValuesString.trimEnd();
|
|
39
42
|
}
|
|
40
43
|
_writeFile(models) {
|
|
41
|
-
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts", `${MODEL_PRE}
|
|
42
|
-
${models}
|
|
44
|
+
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts", `${MODEL_PRE}
|
|
45
|
+
${models}
|
|
43
46
|
${MODEL_POST}`, { flag: 'w' });
|
|
44
47
|
}
|
|
45
48
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Utils } from "../utils.js";
|
|
2
|
+
export class Normalizator {
|
|
3
|
+
static mapTsTypeToAngular(type) {
|
|
4
|
+
const normalized = type.trim().toLowerCase();
|
|
5
|
+
if (normalized.endsWith("[]")) {
|
|
6
|
+
const inner = normalized.slice(0, -2).trim();
|
|
7
|
+
return `${Normalizator.mapTsTypeToAngular(inner)}[]`;
|
|
8
|
+
}
|
|
9
|
+
switch (normalized) {
|
|
10
|
+
case "string":
|
|
11
|
+
case "uuid":
|
|
12
|
+
return "string";
|
|
13
|
+
case "date":
|
|
14
|
+
case "datetime":
|
|
15
|
+
return "datetime";
|
|
16
|
+
case "number":
|
|
17
|
+
case "float":
|
|
18
|
+
case "double":
|
|
19
|
+
case "integer":
|
|
20
|
+
case "int":
|
|
21
|
+
case "long":
|
|
22
|
+
return "number";
|
|
23
|
+
case "boolean":
|
|
24
|
+
case "bool":
|
|
25
|
+
return "boolean";
|
|
26
|
+
case "any":
|
|
27
|
+
case "object":
|
|
28
|
+
return "any";
|
|
29
|
+
case "null":
|
|
30
|
+
return "null";
|
|
31
|
+
case "undefined":
|
|
32
|
+
return "undefined";
|
|
33
|
+
default:
|
|
34
|
+
return Utils.toPascalCase(type);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ApiDto } from '@src/models/api-dto.js';
|
|
2
|
+
import { CommandLineArgs } from '@src/index.js';
|
|
3
|
+
import { ModelDto } from '@src/models/model-dto.js';
|
|
4
|
+
export declare class ApiDartWriter {
|
|
5
|
+
private _commandLineArgs;
|
|
6
|
+
constructor(commandLineArgs: CommandLineArgs);
|
|
7
|
+
write(apis: ApiDto[], models: ModelDto[]): void;
|
|
8
|
+
private _toPascalCase;
|
|
9
|
+
private _normalizeApiClassName;
|
|
10
|
+
private _groupByTag;
|
|
11
|
+
}
|
|
@@ -49,7 +49,7 @@ export class ApiDartWriter {
|
|
|
49
49
|
else {
|
|
50
50
|
responseType = Utils.toDartClassName(responseType) ?? responseType;
|
|
51
51
|
}
|
|
52
|
-
var methodName = Utils.
|
|
52
|
+
var methodName = Utils.getNormalizedApiPathDart(api.name);
|
|
53
53
|
if (methodName.startsWith(Utils.toFirstLetterLowercase(tag))) {
|
|
54
54
|
methodName = methodName.slice(tag.length);
|
|
55
55
|
methodName = Utils.toFirstLetterLowercase(methodName);
|
|
@@ -107,7 +107,7 @@ export class ApiDartWriter {
|
|
|
107
107
|
apiDefinition.imports = imports.map(i => i.import);
|
|
108
108
|
let destinationPath = `${this._commandLineArgs.outputDirectory}/${subPath}/api`;
|
|
109
109
|
Utils.ensureDirectorySync(`${destinationPath}`);
|
|
110
|
-
var result = Mustache.
|
|
110
|
+
var result = Mustache.render(template, apiDefinition);
|
|
111
111
|
writeFileSync(`${destinationPath}/${apiDefinition.filename}.dart`, result, 'utf-8');
|
|
112
112
|
}
|
|
113
113
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ModelDto } from '@src/models/model-dto.js';
|
|
2
|
+
import { CommandLineArgs } from '@src/index.js';
|
|
3
|
+
export declare class ModelDartWriter {
|
|
4
|
+
private _commandLineArgs;
|
|
5
|
+
constructor(commandLineArgs: CommandLineArgs);
|
|
6
|
+
write(models: ModelDto[]): void;
|
|
7
|
+
writeClasses(models: ModelDto[]): void;
|
|
8
|
+
writeEnums(models: ModelDto[]): void;
|
|
9
|
+
}
|
|
@@ -60,7 +60,7 @@ export class ModelDartWriter {
|
|
|
60
60
|
dartModel.imports = imports.map(i => i.import);
|
|
61
61
|
let destinationPath = `${this._commandLineArgs.outputDirectory}/${normalizedInfo.subPath}`;
|
|
62
62
|
Utils.ensureDirectorySync(`${destinationPath}`);
|
|
63
|
-
let result = Mustache.
|
|
63
|
+
let result = Mustache.render(template, dartModel);
|
|
64
64
|
writeFileSync(`${destinationPath}/${normalizedInfo.filename}.dart`, result, 'utf-8');
|
|
65
65
|
}
|
|
66
66
|
});
|
|
@@ -92,7 +92,7 @@ export class ModelDartWriter {
|
|
|
92
92
|
dartModel.fields[lastIndex].isLast = true;
|
|
93
93
|
let destinationPath = `${this._commandLineArgs.outputDirectory}/${normalizedInfo.subPath}`;
|
|
94
94
|
Utils.ensureDirectorySync(`${destinationPath}`);
|
|
95
|
-
let result = Mustache.
|
|
95
|
+
let result = Mustache.render(template, dartModel);
|
|
96
96
|
writeFileSync(`${destinationPath}/${normalizedInfo.filename}.dart`, result, 'utf-8');
|
|
97
97
|
});
|
|
98
98
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ModelDto } from "@src/models/model-dto.js";
|
|
2
|
+
export declare class Normalizator {
|
|
3
|
+
static getNormalizedInfo(model: ModelDto): {
|
|
4
|
+
filename: string;
|
|
5
|
+
subPath: string;
|
|
6
|
+
modelName: string;
|
|
7
|
+
};
|
|
8
|
+
static getNormalizedTypeName(typeName: string): string;
|
|
9
|
+
static mapTsTypeToDart(type: string): string;
|
|
10
|
+
}
|
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
import 'package:{{package}}/core/di/injector.dart';
|
|
2
|
-
import 'package:dio/dio.dart';
|
|
3
|
-
{{#imports}}
|
|
4
|
-
{{{.}}}
|
|
5
|
-
{{/imports}}
|
|
6
|
-
|
|
7
|
-
class {{apiClassName}} {
|
|
8
|
-
final Dio _dio;
|
|
9
|
-
|
|
10
|
-
{{apiClassName}}() : _dio = getIt<Dio>();
|
|
11
|
-
|
|
12
|
-
{{#endpoints}}
|
|
13
|
-
Future<{{responseType}}> {{methodName}}(
|
|
14
|
-
{{#haveRequest}}{{requestType}} request{{/haveRequest}}
|
|
15
|
-
{{#queryParams}}
|
|
16
|
-
{{type}}{{#nullable}}?{{/nullable}} {{name}},
|
|
17
|
-
{{/queryParams}}
|
|
18
|
-
) async {
|
|
19
|
-
final response = await _dio.{{httpMethod}}(
|
|
20
|
-
'{{{path}}}',
|
|
21
|
-
{{#haveRequest}}
|
|
22
|
-
data: request.toJson(),
|
|
23
|
-
{{/haveRequest}}
|
|
24
|
-
{{^haveRequest}}
|
|
25
|
-
queryParameters: {
|
|
26
|
-
{{#queryParams}}
|
|
27
|
-
{{#nullable}}if ({{name}} != null) {{/nullable}}'{{name}}': {{name}},
|
|
28
|
-
{{/queryParams}}
|
|
29
|
-
},
|
|
30
|
-
{{/haveRequest}}
|
|
31
|
-
);
|
|
32
|
-
{{#isResponseNativeType}}
|
|
33
|
-
return parseNative<{{responseType}}>(response.data);
|
|
34
|
-
{{/isResponseNativeType}}
|
|
35
|
-
{{^isResponseNativeType}}
|
|
36
|
-
return {{responseType}}.fromJson(response.data);
|
|
37
|
-
{{/isResponseNativeType}}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
{{/endpoints}}
|
|
41
|
-
|
|
42
|
-
T parseNative<T>(dynamic data) {
|
|
43
|
-
if (T == int) return int.parse(data.toString()) as T;
|
|
44
|
-
if (T == double) return double.parse(data.toString()) as T;
|
|
45
|
-
if (T == String) return data as T;
|
|
46
|
-
if (T == bool) return data as T;
|
|
47
|
-
throw UnsupportedError('Unsupported native type $T');
|
|
48
|
-
}
|
|
49
|
-
}
|
|
1
|
+
import 'package:{{package}}/core/di/injector.dart';
|
|
2
|
+
import 'package:dio/dio.dart';
|
|
3
|
+
{{#imports}}
|
|
4
|
+
{{{.}}}
|
|
5
|
+
{{/imports}}
|
|
6
|
+
|
|
7
|
+
class {{apiClassName}} {
|
|
8
|
+
final Dio _dio;
|
|
9
|
+
|
|
10
|
+
{{apiClassName}}() : _dio = getIt<Dio>();
|
|
11
|
+
|
|
12
|
+
{{#endpoints}}
|
|
13
|
+
Future<{{responseType}}> {{methodName}}(
|
|
14
|
+
{{#haveRequest}}{{requestType}} request{{/haveRequest}}
|
|
15
|
+
{{#queryParams}}
|
|
16
|
+
{{type}}{{#nullable}}?{{/nullable}} {{name}},
|
|
17
|
+
{{/queryParams}}
|
|
18
|
+
) async {
|
|
19
|
+
final response = await _dio.{{httpMethod}}(
|
|
20
|
+
'{{{path}}}',
|
|
21
|
+
{{#haveRequest}}
|
|
22
|
+
data: request.toJson(),
|
|
23
|
+
{{/haveRequest}}
|
|
24
|
+
{{^haveRequest}}
|
|
25
|
+
queryParameters: {
|
|
26
|
+
{{#queryParams}}
|
|
27
|
+
{{#nullable}}if ({{name}} != null) {{/nullable}}'{{name}}': {{name}},
|
|
28
|
+
{{/queryParams}}
|
|
29
|
+
},
|
|
30
|
+
{{/haveRequest}}
|
|
31
|
+
);
|
|
32
|
+
{{#isResponseNativeType}}
|
|
33
|
+
return parseNative<{{responseType}}>(response.data);
|
|
34
|
+
{{/isResponseNativeType}}
|
|
35
|
+
{{^isResponseNativeType}}
|
|
36
|
+
return {{responseType}}.fromJson(response.data);
|
|
37
|
+
{{/isResponseNativeType}}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
{{/endpoints}}
|
|
41
|
+
|
|
42
|
+
T parseNative<T>(dynamic data) {
|
|
43
|
+
if (T == int) return int.parse(data.toString()) as T;
|
|
44
|
+
if (T == double) return double.parse(data.toString()) as T;
|
|
45
|
+
if (T == String) return data as T;
|
|
46
|
+
if (T == bool) return data as T;
|
|
47
|
+
throw UnsupportedError('Unsupported native type $T');
|
|
48
|
+
}
|
|
49
|
+
}
|