@devlearning/swagger-generator 1.1.2 → 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/dist/generator.d.ts +1 -0
- package/dist/generator.js +61 -45
- package/dist/generators-writers/angular/api-angular-writer.js +7 -4
- package/dist/generators-writers/angular/model-angular-writer.js +6 -3
- 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.js +1 -1
- package/dist/generators-writers/nextjs/api-nextjs-writer.js +1 -1
- package/dist/generators-writers/utils.d.ts +2 -1
- package/dist/generators-writers/utils.js +9 -1
- package/package.json +2 -2
- package/src/generator.ts +63 -47
- package/src/generators-writers/angular/api-angular-writer.ts +9 -4
- package/src/generators-writers/angular/model-angular-writer.ts +6 -3
- package/src/generators-writers/angular/normalizator.ts +42 -0
- package/src/generators-writers/dart/api-dart-writer.ts +1 -1
- package/src/generators-writers/nextjs/api-nextjs-writer.ts +1 -1
- package/src/generators-writers/utils.ts +13 -1
package/dist/generator.d.ts
CHANGED
|
@@ -31,6 +31,7 @@ export declare class Generator {
|
|
|
31
31
|
isDate(schema: SwaggerSchema): boolean;
|
|
32
32
|
getObjectName(ref: string): string;
|
|
33
33
|
retrieveComponentProperties(swaggerComponent: SwaggerComponent): PropertyDto[];
|
|
34
|
+
retrieveSchemaProperties(schema: SwaggerSchema): PropertyDto[];
|
|
34
35
|
retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent): EnumValueDto[];
|
|
35
36
|
retrieveNestedObjects(usedTypes: TypeDto[]): void;
|
|
36
37
|
retrieveNestedObjectsRecursive(swaggerComponent: SwaggerComponent, usedTypes: TypeDto[]): void;
|
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) {
|
|
@@ -515,7 +531,7 @@ export class Generator {
|
|
|
515
531
|
if (normalizedApiName.charAt(0) == '_') {
|
|
516
532
|
normalizedApiName = normalizedApiName.slice(1);
|
|
517
533
|
}
|
|
518
|
-
return
|
|
534
|
+
return normalizedApiName;
|
|
519
535
|
}
|
|
520
536
|
toFirstLetterLowercase(value) {
|
|
521
537
|
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
@@ -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,7 +23,7 @@ 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 httpOptions = api.isMultiPart ? 'httpOptionsMultipart' : 'httpOptions';
|
|
26
27
|
let apiString = `
|
|
27
28
|
public ${apiNameNormalized}(${parametersString}): Observable<${returnTypeString}> {
|
|
28
29
|
${queryParametersPreparation}${requestPreparation}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${api.url}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
|
|
@@ -37,8 +38,10 @@ export class ApiAngularWriter {
|
|
|
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 = '';
|
|
@@ -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) {
|
|
@@ -14,7 +16,7 @@ export class ModelAngularWriter {
|
|
|
14
16
|
}
|
|
15
17
|
_modelString(model) {
|
|
16
18
|
let modelString = `
|
|
17
|
-
export ${model.modelType} ${model.name} {
|
|
19
|
+
export ${model.modelType} ${Utils.toPascalCase(model.name)} {
|
|
18
20
|
${this._properties(model)}${this._enumValues(model)}
|
|
19
21
|
}
|
|
20
22
|
`;
|
|
@@ -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
|
}
|
|
@@ -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
|
+
}
|
|
@@ -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);
|
|
@@ -14,7 +14,7 @@ export class ApiNextJsWriter {
|
|
|
14
14
|
this._writeFile(apiString);
|
|
15
15
|
}
|
|
16
16
|
_apiString(api) {
|
|
17
|
-
let apiNameNormalized = Utils.toCamelCase(Utils.
|
|
17
|
+
let apiNameNormalized = Utils.toCamelCase(Utils.getNormalizedApiPathDart(api.name));
|
|
18
18
|
let parametersString = this._parameters(api);
|
|
19
19
|
let queryParametersPreparation = this._queryParametersPreparation(api);
|
|
20
20
|
let requestPreparation = this._requestPreparation(api);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { SwaggerSchema } from "@src/models/swagger/swagger-schema.js";
|
|
2
2
|
export declare class Utils {
|
|
3
|
-
static
|
|
3
|
+
static getNormalizedApiPathAngular(apiName: string): string;
|
|
4
|
+
static getNormalizedApiPathDart(apiName: string): string;
|
|
4
5
|
static toFirstLetterLowercase(value: string): string;
|
|
5
6
|
static toCamelCase(input: string): string;
|
|
6
7
|
static toPascalCase(input: string): string;
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
export class Utils {
|
|
4
|
-
static
|
|
4
|
+
static getNormalizedApiPathAngular(apiName) {
|
|
5
|
+
let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
6
|
+
if (normalizedApiName.charAt(0) == '_') {
|
|
7
|
+
normalizedApiName = normalizedApiName.slice(1);
|
|
8
|
+
}
|
|
9
|
+
normalizedApiName = normalizedApiName.replaceAll('/', '_');
|
|
10
|
+
return normalizedApiName;
|
|
11
|
+
}
|
|
12
|
+
static getNormalizedApiPathDart(apiName) {
|
|
5
13
|
let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
6
14
|
if (normalizedApiName.charAt(0) == '_') {
|
|
7
15
|
normalizedApiName = normalizedApiName.slice(1);
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devlearning/swagger-generator",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "Swagger generator apis and models for Angular and NextJS",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"debug-angular": "npx tsx src/index.ts --url http://localhost:
|
|
8
|
+
"debug-angular": "npx tsx src/index.ts --url http://localhost:5208/swagger/v1/swagger.json --output autogeneration/output --target angular --dateTimeLibrary moment",
|
|
9
9
|
"debug-nextjs": "npx tsx src/index.ts --url http://localhost:7550/swagger/ApiGateway/swagger.json --output autogeneration/output --target next --dateTimeLibrary date-fns",
|
|
10
10
|
"debug-flutter": "npx tsx src/index.ts --url http://localhost:7550/swagger/ApiGateway/swagger.json --output autogen --target flutter --package coqudo_app",
|
|
11
11
|
"copy-templates": "copyfiles -u 3 \"src/generators-writers/dart/templates/**/*\" dist/generators-writers/dart",
|
package/src/generator.ts
CHANGED
|
@@ -96,37 +96,47 @@ export class Generator {
|
|
|
96
96
|
let usedTypes: TypeDto[] = [];
|
|
97
97
|
let usedMultiPart: string[] = [];
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
99
|
+
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
100
|
+
const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
101
|
+
const apiSwaggerMethodKey = this._swagger.paths[apiName];
|
|
102
|
+
const apiMethod = Object.getOwnPropertyNames(apiSwaggerMethodKey)[0];
|
|
103
|
+
const apiSwaggerMethod = apiSwaggerMethodKey[apiMethod];
|
|
104
|
+
|
|
105
|
+
// const parametersRefType = apiSwaggerMethod.parameters?.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''));
|
|
106
|
+
// if (parametersRefType) {
|
|
107
|
+
// usedTypes = usedTypes.concat(parametersRefType);
|
|
108
|
+
// }
|
|
109
|
+
|
|
110
|
+
// const responseRefType = this.computeRequestBodyType(apiSwaggerMethod!);
|
|
111
|
+
// if (responseRefType && !responseRefType.isVoid && !responseRefType.isNativeType) {
|
|
112
|
+
// usedTypes = usedTypes.concat(responseRefType.typeName);
|
|
113
|
+
// }
|
|
114
|
+
|
|
115
|
+
// const contentRefType = this.computeResponseType(apiSwaggerMethod!);
|
|
116
|
+
// if (contentRefType && !contentRefType.isVoid && !contentRefType.isNativeType) {
|
|
117
|
+
// usedTypes = usedTypes.concat(contentRefType.typeName);
|
|
118
|
+
// }
|
|
119
|
+
|
|
120
|
+
if (apiSwaggerMethod.requestBody?.content[contentTypeMultipartFormData]?.schema != null) {
|
|
121
|
+
usedMultiPart.push(apiName);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
120
124
|
|
|
121
125
|
for (let index = 0; index < this._apis.length; index++) {
|
|
122
126
|
if (this._apis[index].returnType && this._apis[index].returnType!.isTypeReference) {
|
|
123
127
|
usedTypes.push(this._apis[index].returnType!);
|
|
128
|
+
// if (this._apis[index].returnType!.typeName.startsWith("hotac_")) {
|
|
129
|
+
// debugger
|
|
130
|
+
// }
|
|
124
131
|
}
|
|
125
132
|
|
|
126
133
|
if (this._apis[index].parameters) {
|
|
127
134
|
this._apis[index].parameters.forEach(parameter => {
|
|
128
135
|
if (parameter.isTypeReference) {
|
|
129
136
|
usedTypes.push(parameter);
|
|
137
|
+
// if (parameter.typeName.startsWith("hotac_")) {
|
|
138
|
+
// debugger
|
|
139
|
+
// }
|
|
130
140
|
}
|
|
131
141
|
});
|
|
132
142
|
}
|
|
@@ -162,15 +172,15 @@ export class Generator {
|
|
|
162
172
|
const swaggerMethod = this._swagger.paths[apiName];
|
|
163
173
|
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
164
174
|
const swaggerMethodInfo = swaggerMethod[method];
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
175
|
+
const schema = swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData].schema;
|
|
176
|
+
|
|
177
|
+
this._models.push({
|
|
178
|
+
typeName: this.getApiNameNormalized(apiName),
|
|
179
|
+
modelType: 'class',
|
|
180
|
+
name: this.getApiNameNormalized(apiName),
|
|
181
|
+
properties: this.retrieveSchemaProperties(schema),
|
|
182
|
+
enumValues: [],
|
|
183
|
+
});
|
|
174
184
|
});
|
|
175
185
|
}
|
|
176
186
|
|
|
@@ -462,19 +472,24 @@ export class Generator {
|
|
|
462
472
|
return properties;
|
|
463
473
|
}
|
|
464
474
|
|
|
465
|
-
|
|
466
|
-
|
|
475
|
+
retrieveSchemaProperties(schema: SwaggerSchema) {
|
|
476
|
+
if (schema.properties == null) return [];
|
|
467
477
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
478
|
+
let properties: PropertyDto[] = [];
|
|
479
|
+
|
|
480
|
+
for (let index = 0; index < Object.getOwnPropertyNames(schema.properties).length; index++) {
|
|
481
|
+
const propertyName = Object.getOwnPropertyNames(schema.properties)[index];
|
|
482
|
+
const property = schema.properties[propertyName];
|
|
483
|
+
const type = this.retrieveType(property);
|
|
484
|
+
properties.push({
|
|
485
|
+
...type,
|
|
486
|
+
name: propertyName,
|
|
487
|
+
nullable: type.nullable,
|
|
488
|
+
});
|
|
489
|
+
}
|
|
475
490
|
|
|
476
|
-
|
|
477
|
-
|
|
491
|
+
return properties;
|
|
492
|
+
}
|
|
478
493
|
|
|
479
494
|
retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
|
|
480
495
|
if (swaggerCopmponent.enum == null) return [];
|
|
@@ -498,15 +513,16 @@ export class Generator {
|
|
|
498
513
|
retrieveNestedObjects(usedTypes: TypeDto[]) {
|
|
499
514
|
for (let i = 0; i < usedTypes.length; i++) {
|
|
500
515
|
const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i].typeName];
|
|
516
|
+
if (!swaggerCopmponent) continue;
|
|
501
517
|
this.retrieveNestedObjectsRecursive(swaggerCopmponent, usedTypes);
|
|
502
518
|
}
|
|
503
519
|
}
|
|
504
520
|
|
|
505
521
|
retrieveNestedObjectsRecursive(swaggerComponent: SwaggerComponent, usedTypes: TypeDto[]) {
|
|
506
|
-
|
|
507
|
-
if (!swaggerComponent.properties) return;
|
|
522
|
+
if (!swaggerComponent.properties) return;
|
|
508
523
|
|
|
509
|
-
|
|
524
|
+
for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
|
|
525
|
+
try {
|
|
510
526
|
const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
|
|
511
527
|
|
|
512
528
|
let nestedUsedType = '';
|
|
@@ -535,9 +551,9 @@ export class Generator {
|
|
|
535
551
|
});
|
|
536
552
|
this.retrieveNestedObjectsRecursive(nested, usedTypes);
|
|
537
553
|
}
|
|
554
|
+
} catch (error) {
|
|
555
|
+
debugger
|
|
538
556
|
}
|
|
539
|
-
} catch (error) {
|
|
540
|
-
debugger
|
|
541
557
|
}
|
|
542
558
|
}
|
|
543
559
|
|
|
@@ -575,7 +591,7 @@ export class Generator {
|
|
|
575
591
|
if (normalizedApiName.charAt(0) == '_') {
|
|
576
592
|
normalizedApiName = normalizedApiName.slice(1);
|
|
577
593
|
}
|
|
578
|
-
return
|
|
594
|
+
return normalizedApiName;
|
|
579
595
|
}
|
|
580
596
|
|
|
581
597
|
toFirstLetterLowercase(value: string) {
|
|
@@ -3,6 +3,7 @@ import { API_POST, API_PRE } from './constants.js';
|
|
|
3
3
|
import { ApiDto } from '@src/models/api-dto.js';
|
|
4
4
|
import { Utils } from '../utils.js';
|
|
5
5
|
import { ParameterDto } from '@src/models/parameter-dto.js';
|
|
6
|
+
import { Normalizator } from './normalizator.js';
|
|
6
7
|
|
|
7
8
|
export class ApiAngularWriter {
|
|
8
9
|
private _outputDirectory: string;
|
|
@@ -24,7 +25,8 @@ export class ApiAngularWriter {
|
|
|
24
25
|
|
|
25
26
|
private _apiString(api: ApiDto) {
|
|
26
27
|
|
|
27
|
-
let apiNameNormalized = Utils.
|
|
28
|
+
let apiNameNormalized = Utils.getNormalizedApiPathAngular(api.name);
|
|
29
|
+
|
|
28
30
|
let parametersString = this._parameters(api);
|
|
29
31
|
let queryParametersPreparation = this._queryParametersPreparation(api);
|
|
30
32
|
let requestPreparation = this._requestPreparation(api);
|
|
@@ -32,7 +34,7 @@ export class ApiAngularWriter {
|
|
|
32
34
|
let returnTypeString = this._returnType(api);
|
|
33
35
|
let haveRequest = api.haveRequest;
|
|
34
36
|
let method = api.method.toLowerCase();
|
|
35
|
-
let httpOptions = api.isMultiPart ? '
|
|
37
|
+
let httpOptions = api.isMultiPart ? 'httpOptionsMultipart' : 'httpOptions';
|
|
36
38
|
|
|
37
39
|
let apiString = `
|
|
38
40
|
public ${apiNameNormalized}(${parametersString}): Observable<${returnTypeString}> {
|
|
@@ -51,8 +53,11 @@ export class ApiAngularWriter {
|
|
|
51
53
|
let parametersString = '';
|
|
52
54
|
|
|
53
55
|
api.parameters.forEach(parameter => {
|
|
56
|
+
const libraryDate = 'moment.Moment';
|
|
54
57
|
const prefixType = parameter.isEnum || !parameter.isNativeType ? 'Models.' : '';
|
|
55
|
-
|
|
58
|
+
const typeName = parameter.typeName === 'dateTime' ? libraryDate : `${prefixType}${Normalizator.mapTsTypeToAngular(parameter.typeName)}`;
|
|
59
|
+
|
|
60
|
+
parametersString += `${parameter.name}${parameter.nullable ? '?' : ''}: ${typeName}, `;
|
|
56
61
|
});
|
|
57
62
|
|
|
58
63
|
if (api.parameters.length > 0)
|
|
@@ -64,7 +69,7 @@ export class ApiAngularWriter {
|
|
|
64
69
|
private _returnType(api: ApiDto) {
|
|
65
70
|
const prefixType = !api.returnType?.isNativeType ? 'Models.' : '';
|
|
66
71
|
const isArray = api.returnType?.isArray ? '[]' : '';
|
|
67
|
-
return api.returnType ? `${prefixType}${api.returnType.typeName}${isArray}` : 'any';
|
|
72
|
+
return api.returnType ? `${prefixType}${Normalizator.mapTsTypeToAngular(api.returnType.typeName)}${isArray}` : 'any';
|
|
68
73
|
}
|
|
69
74
|
|
|
70
75
|
private _queryParametersPreparation(api: ApiDto) {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import { MODEL_POST, MODEL_PRE } from './constants.js';
|
|
3
3
|
import { ModelDto } from '@src/models/model-dto.js';
|
|
4
|
+
import { Normalizator } from './normalizator.js';
|
|
5
|
+
import { Utils } from '../utils.js';
|
|
4
6
|
|
|
5
7
|
export class ModelAngularWriter {
|
|
6
8
|
private _outputDirectory: string;
|
|
@@ -22,7 +24,7 @@ export class ModelAngularWriter {
|
|
|
22
24
|
|
|
23
25
|
private _modelString(model: ModelDto) {
|
|
24
26
|
let modelString = `
|
|
25
|
-
export ${model.modelType} ${model.name} {
|
|
27
|
+
export ${model.modelType} ${Utils.toPascalCase(model.name)} {
|
|
26
28
|
${this._properties(model)}${this._enumValues(model)}
|
|
27
29
|
}
|
|
28
30
|
`;
|
|
@@ -36,8 +38,9 @@ ${this._properties(model)}${this._enumValues(model)}
|
|
|
36
38
|
model.properties.forEach(property => {
|
|
37
39
|
//const libraryDate = this._commandLineArgs.dateTimeLibrary == DateTimeLibrary.Moment ? 'moment.Moment' : 'Date'
|
|
38
40
|
const libraryDate = 'moment.Moment';
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
+
const isArray = property.isArray ? '[]' : '';
|
|
42
|
+
const typeName = property.typeName === 'dateTime' ? libraryDate : Normalizator.mapTsTypeToAngular(property.typeName);
|
|
43
|
+
propertiesString += ` ${property.name}${property.nullable ? '?' : ''}: ${typeName}${isArray};\n`;
|
|
41
44
|
});
|
|
42
45
|
|
|
43
46
|
return propertiesString.trimEnd();
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Utils } from "../utils.js";
|
|
2
|
+
|
|
3
|
+
export class Normalizator {
|
|
4
|
+
|
|
5
|
+
public static mapTsTypeToAngular(type: string): string {
|
|
6
|
+
const normalized: string = type.trim().toLowerCase();
|
|
7
|
+
|
|
8
|
+
if (normalized.endsWith("[]")) {
|
|
9
|
+
const inner: string = normalized.slice(0, -2).trim();
|
|
10
|
+
return `${Normalizator.mapTsTypeToAngular(inner)}[]`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
switch (normalized) {
|
|
14
|
+
case "string":
|
|
15
|
+
case "uuid":
|
|
16
|
+
return "string";
|
|
17
|
+
case "date":
|
|
18
|
+
case "datetime":
|
|
19
|
+
return "datetime";
|
|
20
|
+
case "number":
|
|
21
|
+
case "float":
|
|
22
|
+
case "double":
|
|
23
|
+
case "integer":
|
|
24
|
+
case "int":
|
|
25
|
+
case "long":
|
|
26
|
+
return "number";
|
|
27
|
+
case "boolean":
|
|
28
|
+
case "bool":
|
|
29
|
+
return "boolean";
|
|
30
|
+
case "any":
|
|
31
|
+
case "object":
|
|
32
|
+
return "any";
|
|
33
|
+
case "null":
|
|
34
|
+
return "null";
|
|
35
|
+
case "undefined":
|
|
36
|
+
return "undefined";
|
|
37
|
+
default:
|
|
38
|
+
return Utils.toPascalCase(type);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
}
|
|
@@ -97,7 +97,7 @@ export class ApiDartWriter {
|
|
|
97
97
|
responseType = Utils.toDartClassName(responseType) ?? responseType;
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
var methodName = Utils.
|
|
100
|
+
var methodName = Utils.getNormalizedApiPathDart(api.name);
|
|
101
101
|
if (methodName.startsWith(Utils.toFirstLetterLowercase(tag))) {
|
|
102
102
|
methodName = methodName.slice(tag.length);
|
|
103
103
|
methodName = Utils.toFirstLetterLowercase(methodName);
|
|
@@ -24,7 +24,7 @@ export class ApiNextJsWriter {
|
|
|
24
24
|
|
|
25
25
|
private _apiString(api: ApiDto) {
|
|
26
26
|
|
|
27
|
-
let apiNameNormalized = Utils.toCamelCase(Utils.
|
|
27
|
+
let apiNameNormalized = Utils.toCamelCase(Utils.getNormalizedApiPathDart(api.name));
|
|
28
28
|
let parametersString = this._parameters(api);
|
|
29
29
|
let queryParametersPreparation = this._queryParametersPreparation(api);
|
|
30
30
|
let requestPreparation = this._requestPreparation(api);
|
|
@@ -4,7 +4,19 @@ import path from 'path';
|
|
|
4
4
|
|
|
5
5
|
export class Utils {
|
|
6
6
|
|
|
7
|
-
public static
|
|
7
|
+
public static getNormalizedApiPathAngular(apiName: string) {
|
|
8
|
+
let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
9
|
+
|
|
10
|
+
if (normalizedApiName.charAt(0) == '_') {
|
|
11
|
+
normalizedApiName = normalizedApiName.slice(1);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
normalizedApiName = normalizedApiName.replaceAll('/', '_');
|
|
15
|
+
|
|
16
|
+
return normalizedApiName;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public static getNormalizedApiPathDart(apiName: string) {
|
|
8
20
|
let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
9
21
|
|
|
10
22
|
if (normalizedApiName.charAt(0) == '_') {
|