@devlearning/swagger-generator 1.1.16 → 1.1.17
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-OLD.md +209 -209
- package/README.md +277 -277
- package/dist/api.constants.js +22 -22
- package/dist/generator.d.ts +4 -0
- package/dist/generator.js +118 -4
- package/dist/generators-writers/angular/api-angular-writer.js +38 -38
- package/dist/generators-writers/angular/constants.js +24 -24
- package/dist/generators-writers/angular/model-angular-writer.js +6 -6
- package/dist/generators-writers/dart/model-dart-writer.d.ts +1 -0
- package/dist/generators-writers/dart/model-dart-writer.js +15 -14
- package/dist/generators-writers/dart/templates/api.mustache +143 -143
- package/dist/generators-writers/dart/templates/enum.mustache +14 -14
- package/dist/generators-writers/dart/templates/model.mustache +20 -23
- package/dist/generators-writers/nextjs/api-nextjs-writer.js +12 -12
- package/dist/generators-writers/nextjs/constants.js +4 -4
- package/dist/generators-writers/nextjs/model-nextjs-writer.js +6 -6
- package/dist/models/swagger/swagger-component.d.ts +2 -2
- package/dist/models/swagger/swagger-schema.d.ts +1 -0
- package/package.json +49 -49
- package/src/api.constants.ts +26 -26
- package/src/generator-old.ts +449 -449
- package/src/generator.ts +752 -625
- package/src/generators-writers/angular/api-angular-writer.ts +187 -187
- package/src/generators-writers/angular/constants.ts +36 -36
- package/src/generators-writers/angular/model-angular-writer.ts +65 -65
- package/src/generators-writers/angular/normalizator.ts +41 -41
- package/src/generators-writers/dart/api-dart-writer.ts +303 -303
- package/src/generators-writers/dart/model-dart-writer.ts +212 -209
- 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 +143 -143
- package/src/generators-writers/dart/templates/enum.mustache +14 -14
- package/src/generators-writers/dart/templates/model.mustache +20 -23
- package/src/generators-writers/nextjs/api-nextjs-writer.ts +157 -157
- 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 +93 -93
- package/src/index.ts +103 -103
- 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 +21 -20
- package/src/models/swagger/swagger.ts +38 -38
- package/src/models/type-dto.ts +7 -7
- package/src/swagger-downloader.ts +46 -46
- package/src/utils/logger.ts +73 -73
- package/src/utils/swagger-validator.ts +89 -89
- package/tsconfig.json +33 -33
- package/dist/templates/api.mustache +0 -29
- package/dist/templates/model.mustache +0 -18
|
@@ -1,158 +1,158 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import { API_PRE } from './constants.js';
|
|
3
|
-
import { ApiDto } from '../../models/api-dto.js';
|
|
4
|
-
import { Utils } from '../utils.js';
|
|
5
|
-
import { ParameterDto } from '../../models/parameter-dto.js';
|
|
6
|
-
import { CommandLineArgs } from '../../index.js';
|
|
7
|
-
|
|
8
|
-
export class ApiNextJsWriter {
|
|
9
|
-
private _commandLineArgs: CommandLineArgs;
|
|
10
|
-
|
|
11
|
-
constructor(commandLineArgs: CommandLineArgs) {
|
|
12
|
-
this._commandLineArgs = commandLineArgs;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
write(apis: ApiDto[]) {
|
|
16
|
-
let apiString = '';
|
|
17
|
-
|
|
18
|
-
apis.forEach(api => {
|
|
19
|
-
apiString += this._apiString(api);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
this._writeFile(apiString);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
private _apiString(api: ApiDto) {
|
|
27
|
-
|
|
28
|
-
let apiNameNormalized = Utils.toCamelCase(Utils.getNormalizedApiPathDart(api.name));
|
|
29
|
-
let parametersString = this._parameters(api);
|
|
30
|
-
let queryParametersPreparation = this._queryParametersPreparation(api);
|
|
31
|
-
let requestPreparation = this._requestPreparation(api);
|
|
32
|
-
let queryParameters = this._queryParameters(api);
|
|
33
|
-
let returnTypeString = this._returnType(api);
|
|
34
|
-
let haveRequest = api.haveRequest;
|
|
35
|
-
let method = api.method.toLowerCase();
|
|
36
|
-
let httpOptions = api.isMultiPart ? 'httpOptionsMultiPart' : 'httpOptions';
|
|
37
|
-
let preparation = `${queryParametersPreparation}
|
|
38
|
-
${requestPreparation}`.trim();
|
|
39
|
-
preparation = preparation.length > 0 ? ` ${preparation}\n ` : '';
|
|
40
|
-
|
|
41
|
-
//\`\${API_BASE_URL}
|
|
42
|
-
let apiString = `
|
|
43
|
-
export const ${apiNameNormalized} = async (${parametersString}): Promise<${returnTypeString}> => {
|
|
44
|
-
${preparation}const response = await axios.${method}<${returnTypeString}>(\`${api.url}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''});
|
|
45
|
-
return response.data;
|
|
46
|
-
}
|
|
47
|
-
`;
|
|
48
|
-
|
|
49
|
-
return apiString;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
private _parameters(api: ApiDto) {
|
|
53
|
-
let parametersString = '';
|
|
54
|
-
|
|
55
|
-
api.parameters.forEach(parameter => {
|
|
56
|
-
parametersString += `${parameter.name}${parameter.nullable ? '?' : ''}: ${!parameter.isNativeType ? 'Models.' : ''}${parameter.typeName}, `;
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
if (api.parameters.length > 0)
|
|
60
|
-
parametersString = parametersString.substring(0, parametersString.length - 2);
|
|
61
|
-
|
|
62
|
-
return parametersString;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private _returnType(api: ApiDto) {
|
|
66
|
-
return api.returnType ? `${!api.returnType.isNativeType ? 'Models.' : ''}${api.returnType.typeName}` : 'any';
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
private _queryParametersPreparation(api: ApiDto) {
|
|
70
|
-
let queryParametersPreparation = '';
|
|
71
|
-
|
|
72
|
-
api.parameters.forEach(parameter => {
|
|
73
|
-
if (parameter.isQuery) {
|
|
74
|
-
queryParametersPreparation += this._queryParametersPreparationStatement(parameter);
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
return `${queryParametersPreparation}`;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private _queryParametersPreparationStatement(parameter: ParameterDto) {
|
|
82
|
-
if (parameter.nullable) {
|
|
83
|
-
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
84
|
-
return ` const ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} !== undefined && isValid(${parameter.name}) ? encodeURIComponent(dateToZulu(${parameter.name})) : '';
|
|
85
|
-
`;
|
|
86
|
-
} else {
|
|
87
|
-
return ` const ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} !== undefined ? encodeURIComponent('' + ${parameter.name}) : '';
|
|
88
|
-
`;
|
|
89
|
-
}
|
|
90
|
-
} else {
|
|
91
|
-
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
92
|
-
return ` const ${parameter.name}Param: string = encodeURIComponent(dateToZulu(${parameter.name}));
|
|
93
|
-
`;
|
|
94
|
-
} else {
|
|
95
|
-
return ` const ${parameter.name}Param: string = encodeURIComponent('' + ${parameter.name});
|
|
96
|
-
`;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
private _queryParameters(api: ApiDto) {
|
|
102
|
-
let queryParameters = '';
|
|
103
|
-
|
|
104
|
-
api.parameters.forEach(parameter => {
|
|
105
|
-
if (parameter.isQuery) {
|
|
106
|
-
queryParameters += this._queryParametersStatement(parameter);
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
if (queryParameters.length > 0) {
|
|
111
|
-
queryParameters = '?' + queryParameters.substring(0, queryParameters.length - 1);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return queryParameters;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private _queryParametersStatement(parameter: ParameterDto) {
|
|
118
|
-
if (parameter.swaggerParameter == null) return '';
|
|
119
|
-
|
|
120
|
-
if (!parameter.isQuery) return '';
|
|
121
|
-
|
|
122
|
-
return `${parameter.name}=\${${parameter.name}Param}&`;
|
|
123
|
-
// if (parameter.isEnum) {
|
|
124
|
-
// return `${parameter.name}=\${` + parameter.name + `Param}&`;
|
|
125
|
-
// } else {
|
|
126
|
-
// return `${parameter.name}=\${` + parameter.name + `}&`;
|
|
127
|
-
// }
|
|
128
|
-
// if (parameter.swaggerParameter.schema.$ref != null) {
|
|
129
|
-
// if (this.isEnum(parameter.schema.$ref) != null) {
|
|
130
|
-
// parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;;
|
|
131
|
-
// } else {
|
|
132
|
-
// throw new Error("retrieveQueryParameters unmanaged parameter.schema.$ref");
|
|
133
|
-
// }
|
|
134
|
-
// } else {
|
|
135
|
-
// parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;
|
|
136
|
-
// }
|
|
137
|
-
// let paramName = Utils.toFirstLetterLowercase(parameter.name);
|
|
138
|
-
// return `${parameter.swaggerParameter?.name}=\${${paramName}Param}&`;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
private _requestPreparation(api: ApiDto) {
|
|
142
|
-
if (!api.haveRequest) {
|
|
143
|
-
return ``;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return ` const wrappedRequest = handleRequest(request);
|
|
147
|
-
`;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
private _writeFile(apis: string) {
|
|
153
|
-
fs.writeFileSync(this._commandLineArgs.outputDirectory + "/api.autogenerated.ts",
|
|
154
|
-
`${API_PRE}
|
|
155
|
-
${apis}`,
|
|
156
|
-
{ flag: 'w' });
|
|
157
|
-
}
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { API_PRE } from './constants.js';
|
|
3
|
+
import { ApiDto } from '../../models/api-dto.js';
|
|
4
|
+
import { Utils } from '../utils.js';
|
|
5
|
+
import { ParameterDto } from '../../models/parameter-dto.js';
|
|
6
|
+
import { CommandLineArgs } from '../../index.js';
|
|
7
|
+
|
|
8
|
+
export class ApiNextJsWriter {
|
|
9
|
+
private _commandLineArgs: CommandLineArgs;
|
|
10
|
+
|
|
11
|
+
constructor(commandLineArgs: CommandLineArgs) {
|
|
12
|
+
this._commandLineArgs = commandLineArgs;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
write(apis: ApiDto[]) {
|
|
16
|
+
let apiString = '';
|
|
17
|
+
|
|
18
|
+
apis.forEach(api => {
|
|
19
|
+
apiString += this._apiString(api);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
this._writeFile(apiString);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private _apiString(api: ApiDto) {
|
|
27
|
+
|
|
28
|
+
let apiNameNormalized = Utils.toCamelCase(Utils.getNormalizedApiPathDart(api.name));
|
|
29
|
+
let parametersString = this._parameters(api);
|
|
30
|
+
let queryParametersPreparation = this._queryParametersPreparation(api);
|
|
31
|
+
let requestPreparation = this._requestPreparation(api);
|
|
32
|
+
let queryParameters = this._queryParameters(api);
|
|
33
|
+
let returnTypeString = this._returnType(api);
|
|
34
|
+
let haveRequest = api.haveRequest;
|
|
35
|
+
let method = api.method.toLowerCase();
|
|
36
|
+
let httpOptions = api.isMultiPart ? 'httpOptionsMultiPart' : 'httpOptions';
|
|
37
|
+
let preparation = `${queryParametersPreparation}
|
|
38
|
+
${requestPreparation}`.trim();
|
|
39
|
+
preparation = preparation.length > 0 ? ` ${preparation}\n ` : '';
|
|
40
|
+
|
|
41
|
+
//\`\${API_BASE_URL}
|
|
42
|
+
let apiString = `
|
|
43
|
+
export const ${apiNameNormalized} = async (${parametersString}): Promise<${returnTypeString}> => {
|
|
44
|
+
${preparation}const response = await axios.${method}<${returnTypeString}>(\`${api.url}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''});
|
|
45
|
+
return response.data;
|
|
46
|
+
}
|
|
47
|
+
`;
|
|
48
|
+
|
|
49
|
+
return apiString;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private _parameters(api: ApiDto) {
|
|
53
|
+
let parametersString = '';
|
|
54
|
+
|
|
55
|
+
api.parameters.forEach(parameter => {
|
|
56
|
+
parametersString += `${parameter.name}${parameter.nullable ? '?' : ''}: ${!parameter.isNativeType ? 'Models.' : ''}${parameter.typeName}, `;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (api.parameters.length > 0)
|
|
60
|
+
parametersString = parametersString.substring(0, parametersString.length - 2);
|
|
61
|
+
|
|
62
|
+
return parametersString;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private _returnType(api: ApiDto) {
|
|
66
|
+
return api.returnType ? `${!api.returnType.isNativeType ? 'Models.' : ''}${api.returnType.typeName}` : 'any';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private _queryParametersPreparation(api: ApiDto) {
|
|
70
|
+
let queryParametersPreparation = '';
|
|
71
|
+
|
|
72
|
+
api.parameters.forEach(parameter => {
|
|
73
|
+
if (parameter.isQuery) {
|
|
74
|
+
queryParametersPreparation += this._queryParametersPreparationStatement(parameter);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return `${queryParametersPreparation}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private _queryParametersPreparationStatement(parameter: ParameterDto) {
|
|
82
|
+
if (parameter.nullable) {
|
|
83
|
+
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
84
|
+
return ` const ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} !== undefined && isValid(${parameter.name}) ? encodeURIComponent(dateToZulu(${parameter.name})) : '';
|
|
85
|
+
`;
|
|
86
|
+
} else {
|
|
87
|
+
return ` const ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} !== undefined ? encodeURIComponent('' + ${parameter.name}) : '';
|
|
88
|
+
`;
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
92
|
+
return ` const ${parameter.name}Param: string = encodeURIComponent(dateToZulu(${parameter.name}));
|
|
93
|
+
`;
|
|
94
|
+
} else {
|
|
95
|
+
return ` const ${parameter.name}Param: string = encodeURIComponent('' + ${parameter.name});
|
|
96
|
+
`;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private _queryParameters(api: ApiDto) {
|
|
102
|
+
let queryParameters = '';
|
|
103
|
+
|
|
104
|
+
api.parameters.forEach(parameter => {
|
|
105
|
+
if (parameter.isQuery) {
|
|
106
|
+
queryParameters += this._queryParametersStatement(parameter);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
if (queryParameters.length > 0) {
|
|
111
|
+
queryParameters = '?' + queryParameters.substring(0, queryParameters.length - 1);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return queryParameters;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private _queryParametersStatement(parameter: ParameterDto) {
|
|
118
|
+
if (parameter.swaggerParameter == null) return '';
|
|
119
|
+
|
|
120
|
+
if (!parameter.isQuery) return '';
|
|
121
|
+
|
|
122
|
+
return `${parameter.name}=\${${parameter.name}Param}&`;
|
|
123
|
+
// if (parameter.isEnum) {
|
|
124
|
+
// return `${parameter.name}=\${` + parameter.name + `Param}&`;
|
|
125
|
+
// } else {
|
|
126
|
+
// return `${parameter.name}=\${` + parameter.name + `}&`;
|
|
127
|
+
// }
|
|
128
|
+
// if (parameter.swaggerParameter.schema.$ref != null) {
|
|
129
|
+
// if (this.isEnum(parameter.schema.$ref) != null) {
|
|
130
|
+
// parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;;
|
|
131
|
+
// } else {
|
|
132
|
+
// throw new Error("retrieveQueryParameters unmanaged parameter.schema.$ref");
|
|
133
|
+
// }
|
|
134
|
+
// } else {
|
|
135
|
+
// parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;
|
|
136
|
+
// }
|
|
137
|
+
// let paramName = Utils.toFirstLetterLowercase(parameter.name);
|
|
138
|
+
// return `${parameter.swaggerParameter?.name}=\${${paramName}Param}&`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private _requestPreparation(api: ApiDto) {
|
|
142
|
+
if (!api.haveRequest) {
|
|
143
|
+
return ``;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return ` const wrappedRequest = handleRequest(request);
|
|
147
|
+
`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
private _writeFile(apis: string) {
|
|
153
|
+
fs.writeFileSync(this._commandLineArgs.outputDirectory + "/api.autogenerated.ts",
|
|
154
|
+
`${API_PRE}
|
|
155
|
+
${apis}`,
|
|
156
|
+
{ flag: 'w' });
|
|
157
|
+
}
|
|
158
158
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export const API_PRE =
|
|
2
|
-
`import axios from 'axios';
|
|
3
|
-
import * as Models from './model.autogenerated';
|
|
4
|
-
import { handleRequest, dateToZulu } from './utils/axios'
|
|
5
|
-
import { isValid } from 'date-fns';
|
|
1
|
+
export const API_PRE =
|
|
2
|
+
`import axios from 'axios';
|
|
3
|
+
import * as Models from './model.autogenerated';
|
|
4
|
+
import { handleRequest, dateToZulu } from './utils/axios'
|
|
5
|
+
import { isValid } from 'date-fns';
|
|
6
6
|
`;
|
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import { ModelDto } from '../../models/model-dto.js';
|
|
3
|
-
|
|
4
|
-
export class ModelNextJsWriter {
|
|
5
|
-
private _outputDirectory: string;
|
|
6
|
-
|
|
7
|
-
constructor(_outputDirectory: string) {
|
|
8
|
-
this._outputDirectory = _outputDirectory;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
write(models: ModelDto[]) {
|
|
12
|
-
let modelString = '';
|
|
13
|
-
|
|
14
|
-
models.forEach(model => {
|
|
15
|
-
modelString += this._modelString(model);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this._writeFile(modelString);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
private _modelString(model: ModelDto) {
|
|
23
|
-
let modelString = `
|
|
24
|
-
export ${model.modelType} ${model.name} {
|
|
25
|
-
${this._properties(model)}${this._enumValues(model)}
|
|
26
|
-
}
|
|
27
|
-
`;
|
|
28
|
-
|
|
29
|
-
return modelString;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
private _properties(model: ModelDto) {
|
|
33
|
-
let propertiesString = '';
|
|
34
|
-
|
|
35
|
-
model.properties.forEach(property => {
|
|
36
|
-
//const libraryDate = this._commandLineArgs.dateTimeLibrary == DateTimeLibrary.Moment ? 'moment.Moment' : 'Date'
|
|
37
|
-
const libraryDate = 'Date';
|
|
38
|
-
const typeName = property.typeName === 'dateTime' ? libraryDate : property.typeName;
|
|
39
|
-
propertiesString += ` ${property.name}${property.nullable ? '?' : ''}: ${typeName};\n`;
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
return propertiesString.trimEnd();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private _enumValues(model: ModelDto) {
|
|
46
|
-
let enumValuesString = '';
|
|
47
|
-
|
|
48
|
-
model.enumValues.forEach(enumValue => {
|
|
49
|
-
enumValuesString += ` ${enumValue.name} = ${enumValue.value},\n`;
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
return enumValuesString.trimEnd();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
private _writeFile(models: string) {
|
|
56
|
-
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
|
|
57
|
-
`
|
|
58
|
-
${models}
|
|
59
|
-
`,
|
|
60
|
-
{ flag: 'w' });
|
|
61
|
-
}
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { ModelDto } from '../../models/model-dto.js';
|
|
3
|
+
|
|
4
|
+
export class ModelNextJsWriter {
|
|
5
|
+
private _outputDirectory: string;
|
|
6
|
+
|
|
7
|
+
constructor(_outputDirectory: string) {
|
|
8
|
+
this._outputDirectory = _outputDirectory;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
write(models: ModelDto[]) {
|
|
12
|
+
let modelString = '';
|
|
13
|
+
|
|
14
|
+
models.forEach(model => {
|
|
15
|
+
modelString += this._modelString(model);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
this._writeFile(modelString);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private _modelString(model: ModelDto) {
|
|
23
|
+
let modelString = `
|
|
24
|
+
export ${model.modelType} ${model.name} {
|
|
25
|
+
${this._properties(model)}${this._enumValues(model)}
|
|
26
|
+
}
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
return modelString;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private _properties(model: ModelDto) {
|
|
33
|
+
let propertiesString = '';
|
|
34
|
+
|
|
35
|
+
model.properties.forEach(property => {
|
|
36
|
+
//const libraryDate = this._commandLineArgs.dateTimeLibrary == DateTimeLibrary.Moment ? 'moment.Moment' : 'Date'
|
|
37
|
+
const libraryDate = 'Date';
|
|
38
|
+
const typeName = property.typeName === 'dateTime' ? libraryDate : property.typeName;
|
|
39
|
+
propertiesString += ` ${property.name}${property.nullable ? '?' : ''}: ${typeName};\n`;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return propertiesString.trimEnd();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private _enumValues(model: ModelDto) {
|
|
46
|
+
let enumValuesString = '';
|
|
47
|
+
|
|
48
|
+
model.enumValues.forEach(enumValue => {
|
|
49
|
+
enumValuesString += ` ${enumValue.name} = ${enumValue.value},\n`;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return enumValuesString.trimEnd();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private _writeFile(models: string) {
|
|
56
|
+
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
|
|
57
|
+
`
|
|
58
|
+
${models}
|
|
59
|
+
`,
|
|
60
|
+
{ flag: 'w' });
|
|
61
|
+
}
|
|
62
62
|
}
|
|
@@ -1,94 +1,94 @@
|
|
|
1
|
-
import { SwaggerSchema } from "../models/swagger/swagger-schema.js";
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
|
|
5
|
-
export class Utils {
|
|
6
|
-
|
|
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) {
|
|
20
|
-
// Remove path params like {id} from the method name.
|
|
21
|
-
apiName = apiName.replace(/\{[^}]+\}/g, '');
|
|
22
|
-
|
|
23
|
-
let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
24
|
-
|
|
25
|
-
if (normalizedApiName.charAt(0) == '_') {
|
|
26
|
-
normalizedApiName = normalizedApiName.slice(1);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
normalizedApiName = normalizedApiName.replace(/-([a-zA-Z])/g, (_, char) => char.toUpperCase());
|
|
30
|
-
|
|
31
|
-
normalizedApiName = this.toCamelCase(normalizedApiName);
|
|
32
|
-
|
|
33
|
-
normalizedApiName = this.toFirstLetterLowercase(normalizedApiName);
|
|
34
|
-
|
|
35
|
-
normalizedApiName = normalizedApiName.replaceAll('_', '');
|
|
36
|
-
|
|
37
|
-
return normalizedApiName;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public static toFirstLetterLowercase(value: string) {
|
|
41
|
-
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public static toCamelCase(input: string): string {
|
|
45
|
-
return input.replace(/_([a-zA-Z])/g, (_, letter) => letter.toUpperCase());
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public static toPascalCase(input: string): string {
|
|
49
|
-
return input
|
|
50
|
-
.replace(/[_\-\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : ""))
|
|
51
|
-
.replace(/^(.)/, (_, c) => c.toUpperCase());
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public static isDate(schema?: SwaggerSchema) {
|
|
55
|
-
if (!schema) return false;
|
|
56
|
-
|
|
57
|
-
return schema.type == 'string' && schema.format == 'date-time';
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public static toDartFileName(name: string): string {
|
|
61
|
-
return name
|
|
62
|
-
.replace(/\./g, '_') // sostituisce i punti con underscore
|
|
63
|
-
.replace(/([a-z0-9])([A-Z])/g, '$1_$2') // aggiunge _ tra camelCase
|
|
64
|
-
.toLowerCase();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
public static toDartClassName(name: string): string | undefined {
|
|
68
|
-
if (!name) return undefined;
|
|
69
|
-
|
|
70
|
-
return name.replace(/\./g, ''); // rimuove i punti per ottenere PascalCase
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
public static ensureDirectorySync(dirPath: string) {
|
|
74
|
-
if (!fs.existsSync(dirPath)) {
|
|
75
|
-
fs.mkdirSync(dirPath, { recursive: true });
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public static clearDirectory(dirPath: string) {
|
|
80
|
-
if (!fs.existsSync(dirPath)) return;
|
|
81
|
-
|
|
82
|
-
for (const file of fs.readdirSync(dirPath)) {
|
|
83
|
-
const fullPath = path.join(dirPath, file);
|
|
84
|
-
const stat = fs.statSync(fullPath);
|
|
85
|
-
|
|
86
|
-
if (stat.isDirectory()) {
|
|
87
|
-
fs.rmSync(fullPath, { recursive: true, force: true });
|
|
88
|
-
} else {
|
|
89
|
-
fs.unlinkSync(fullPath);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
1
|
+
import { SwaggerSchema } from "../models/swagger/swagger-schema.js";
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
export class Utils {
|
|
6
|
+
|
|
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) {
|
|
20
|
+
// Remove path params like {id} from the method name.
|
|
21
|
+
apiName = apiName.replace(/\{[^}]+\}/g, '');
|
|
22
|
+
|
|
23
|
+
let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
24
|
+
|
|
25
|
+
if (normalizedApiName.charAt(0) == '_') {
|
|
26
|
+
normalizedApiName = normalizedApiName.slice(1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
normalizedApiName = normalizedApiName.replace(/-([a-zA-Z])/g, (_, char) => char.toUpperCase());
|
|
30
|
+
|
|
31
|
+
normalizedApiName = this.toCamelCase(normalizedApiName);
|
|
32
|
+
|
|
33
|
+
normalizedApiName = this.toFirstLetterLowercase(normalizedApiName);
|
|
34
|
+
|
|
35
|
+
normalizedApiName = normalizedApiName.replaceAll('_', '');
|
|
36
|
+
|
|
37
|
+
return normalizedApiName;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public static toFirstLetterLowercase(value: string) {
|
|
41
|
+
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public static toCamelCase(input: string): string {
|
|
45
|
+
return input.replace(/_([a-zA-Z])/g, (_, letter) => letter.toUpperCase());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public static toPascalCase(input: string): string {
|
|
49
|
+
return input
|
|
50
|
+
.replace(/[_\-\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : ""))
|
|
51
|
+
.replace(/^(.)/, (_, c) => c.toUpperCase());
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public static isDate(schema?: SwaggerSchema) {
|
|
55
|
+
if (!schema) return false;
|
|
56
|
+
|
|
57
|
+
return schema.type == 'string' && schema.format == 'date-time';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public static toDartFileName(name: string): string {
|
|
61
|
+
return name
|
|
62
|
+
.replace(/\./g, '_') // sostituisce i punti con underscore
|
|
63
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1_$2') // aggiunge _ tra camelCase
|
|
64
|
+
.toLowerCase();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public static toDartClassName(name: string): string | undefined {
|
|
68
|
+
if (!name) return undefined;
|
|
69
|
+
|
|
70
|
+
return name.replace(/\./g, ''); // rimuove i punti per ottenere PascalCase
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public static ensureDirectorySync(dirPath: string) {
|
|
74
|
+
if (!fs.existsSync(dirPath)) {
|
|
75
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public static clearDirectory(dirPath: string) {
|
|
80
|
+
if (!fs.existsSync(dirPath)) return;
|
|
81
|
+
|
|
82
|
+
for (const file of fs.readdirSync(dirPath)) {
|
|
83
|
+
const fullPath = path.join(dirPath, file);
|
|
84
|
+
const stat = fs.statSync(fullPath);
|
|
85
|
+
|
|
86
|
+
if (stat.isDirectory()) {
|
|
87
|
+
fs.rmSync(fullPath, { recursive: true, force: true });
|
|
88
|
+
} else {
|
|
89
|
+
fs.unlinkSync(fullPath);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
94
|
}
|