@devlearning/swagger-generator 1.0.14 → 1.0.16
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/generators-writers/dart/api-dart-writer.js +3 -1
- package/dist/generators-writers/dart/model-dart-writer.js +2 -0
- package/dist/generators-writers/dart/templates/api.mustache +29 -0
- package/dist/generators-writers/dart/templates/model.mustache +18 -0
- package/dist/templates/api.mustache +29 -0
- package/dist/templates/model.mustache +18 -0
- package/package.json +4 -2
- package/src/generators-writers/dart/api-dart-writer.ts +3 -2
- package/src/generators-writers/dart/model-dart-writer.ts +6 -4
- package/dist/package.json +0 -37
- package/dist/src/api.constants.js +0 -24
- package/dist/src/generator-old.js +0 -369
- package/dist/src/generator.js +0 -425
- package/dist/src/generators-writers/angular/api-angular-writer.js +0 -113
- package/dist/src/generators-writers/angular/constants.js +0 -28
- package/dist/src/generators-writers/angular/model-angular-writer.js +0 -42
- package/dist/src/generators-writers/nextjs/api-nextjs-writer.js +0 -127
- package/dist/src/generators-writers/nextjs/constants.js +0 -5
- package/dist/src/generators-writers/nextjs/model-nextjs-writer.js +0 -41
- package/dist/src/generators-writers/utils.js +0 -20
- package/dist/src/index.js +0 -24
- package/dist/src/model.constants.js +0 -1
- package/dist/src/models/api-dto.js +0 -1
- package/dist/src/models/enum-value-dto.js +0 -1
- package/dist/src/models/model-dto.js +0 -1
- package/dist/src/models/parameter-dto.js +0 -1
- package/dist/src/models/property-dto.js +0 -1
- package/dist/src/models/swagger/swagger-component-property.js +0 -1
- package/dist/src/models/swagger/swagger-component.js +0 -1
- package/dist/src/models/swagger/swagger-content.js +0 -1
- package/dist/src/models/swagger/swagger-info.js +0 -1
- package/dist/src/models/swagger/swagger-method.js +0 -1
- package/dist/src/models/swagger/swagger-schema.js +0 -1
- package/dist/src/models/swagger/swagger.js +0 -1
- package/dist/src/models/type-dto.js +0 -1
- package/dist/src/swagger-downloader.js +0 -9
|
@@ -2,13 +2,15 @@ import fs, { writeFileSync } from 'fs';
|
|
|
2
2
|
import { Utils } from '../utils.js';
|
|
3
3
|
import * as Mustache from 'mustache';
|
|
4
4
|
import { Normalizator } from './normalizator.js';
|
|
5
|
+
import path from 'path';
|
|
5
6
|
export class ApiDartWriter {
|
|
6
7
|
_commandLineArgs;
|
|
7
8
|
constructor(commandLineArgs) {
|
|
8
9
|
this._commandLineArgs = commandLineArgs;
|
|
9
10
|
}
|
|
10
11
|
write(apis, models) {
|
|
11
|
-
const
|
|
12
|
+
const templatePath = path.join(__dirname, 'generators-writers', 'dart', 'templates', 'api.mustache');
|
|
13
|
+
const template = fs.readFileSync(templatePath, 'utf-8');
|
|
12
14
|
const grouped = this._groupByTag(apis);
|
|
13
15
|
for (const [tag, apis] of Object.entries(grouped)) {
|
|
14
16
|
console.log(`Api: ${tag}`);
|
|
@@ -2,12 +2,14 @@ import { readFileSync, writeFileSync } from 'fs';
|
|
|
2
2
|
import * as Mustache from 'mustache';
|
|
3
3
|
import { Utils } from '../utils.js';
|
|
4
4
|
import { Normalizator } from './normalizator.js';
|
|
5
|
+
import path from 'path';
|
|
5
6
|
export class ModelDartWriter {
|
|
6
7
|
_commandLineArgs;
|
|
7
8
|
constructor(commandLineArgs) {
|
|
8
9
|
this._commandLineArgs = commandLineArgs;
|
|
9
10
|
}
|
|
10
11
|
write(models) {
|
|
12
|
+
const templatePath = path.join(__dirname, 'generators-writers', 'dart', 'templates', 'model.mustache');
|
|
11
13
|
const template = readFileSync('src/generators-writers/dart/templates/model.mustache', 'utf-8');
|
|
12
14
|
models.forEach(model => {
|
|
13
15
|
const normalizedInfo = Normalizator.getNormalizedInfo(model);
|
|
@@ -0,0 +1,29 @@
|
|
|
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}}({{#haveRequest}}{{requestType}} request{{/haveRequest}}) async {
|
|
14
|
+
final response = await _dio.{{httpMethod}}(
|
|
15
|
+
'{{{path}}}',
|
|
16
|
+
{{#haveRequest}}
|
|
17
|
+
{{#isGet}}
|
|
18
|
+
queryParameters: request.toJson(),
|
|
19
|
+
{{/isGet}}
|
|
20
|
+
{{^isGet}}
|
|
21
|
+
data: request.toJson(),
|
|
22
|
+
{{/isGet}}
|
|
23
|
+
{{/haveRequest}}
|
|
24
|
+
);
|
|
25
|
+
return {{responseType}}.fromJson(response.data);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
{{/endpoints}}
|
|
29
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
2
|
+
{{#imports}}
|
|
3
|
+
{{{.}}}
|
|
4
|
+
{{/imports}}
|
|
5
|
+
|
|
6
|
+
part '{{filename}}.freezed.dart';
|
|
7
|
+
part '{{filename}}.g.dart';
|
|
8
|
+
|
|
9
|
+
@freezed
|
|
10
|
+
abstract class {{modelName}} with _${{modelName}} {
|
|
11
|
+
const factory {{modelName}}({
|
|
12
|
+
{{#fields}}
|
|
13
|
+
{{required}}{{type}}{{nullable}} {{name}},
|
|
14
|
+
{{/fields}}
|
|
15
|
+
}) = _{{modelName}};
|
|
16
|
+
|
|
17
|
+
factory {{modelName}}.fromJson(Map<String, dynamic> json) => _${{modelName}}FromJson(json);
|
|
18
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
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}}({{#haveRequest}}{{requestType}} request{{/haveRequest}}) async {
|
|
14
|
+
final response = await _dio.{{httpMethod}}(
|
|
15
|
+
'{{{path}}}',
|
|
16
|
+
{{#haveRequest}}
|
|
17
|
+
{{#isGet}}
|
|
18
|
+
queryParameters: request.toJson(),
|
|
19
|
+
{{/isGet}}
|
|
20
|
+
{{^isGet}}
|
|
21
|
+
data: request.toJson(),
|
|
22
|
+
{{/isGet}}
|
|
23
|
+
{{/haveRequest}}
|
|
24
|
+
);
|
|
25
|
+
return {{responseType}}.fromJson(response.data);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
{{/endpoints}}
|
|
29
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
2
|
+
{{#imports}}
|
|
3
|
+
{{{.}}}
|
|
4
|
+
{{/imports}}
|
|
5
|
+
|
|
6
|
+
part '{{filename}}.freezed.dart';
|
|
7
|
+
part '{{filename}}.g.dart';
|
|
8
|
+
|
|
9
|
+
@freezed
|
|
10
|
+
abstract class {{modelName}} with _${{modelName}} {
|
|
11
|
+
const factory {{modelName}}({
|
|
12
|
+
{{#fields}}
|
|
13
|
+
{{required}}{{type}}{{nullable}} {{name}},
|
|
14
|
+
{{/fields}}
|
|
15
|
+
}) = _{{modelName}};
|
|
16
|
+
|
|
17
|
+
factory {{modelName}}.fromJson(Map<String, dynamic> json) => _${{modelName}}FromJson(json);
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devlearning/swagger-generator",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"description": "Swagger generator apis and models for Angular and NextJS",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"debug-angular": "npx tsx src/index.ts --url http://localhost:7550/swagger/ApiGateway/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",
|
|
12
|
+
"deploy": "npx tsc & copyfiles & npm publish"
|
|
12
13
|
},
|
|
13
14
|
"bin": {
|
|
14
15
|
"swgen": "./dist/index.js"
|
|
@@ -36,6 +37,7 @@
|
|
|
36
37
|
"@types/mustache": "^4.2.6",
|
|
37
38
|
"@types/node": "^18.15.0",
|
|
38
39
|
"@types/yargs": "^17.0.33",
|
|
40
|
+
"copyfiles": "^2.4.1",
|
|
39
41
|
"ts-node": "^10.9.1",
|
|
40
42
|
"tsconfig-paths": "^4.2.0",
|
|
41
43
|
"typescript": "^5.1.6"
|
|
@@ -8,6 +8,7 @@ import { ModelDto } from '@src/models/model-dto.js';
|
|
|
8
8
|
import { TypeDto } from '@src/models/type-dto.js';
|
|
9
9
|
import { ImportDefinitionDart } from './models/import-definition-dart.js';
|
|
10
10
|
import { Normalizator } from './normalizator.js';
|
|
11
|
+
import path from 'path';
|
|
11
12
|
|
|
12
13
|
interface ApiDefinitionDart {
|
|
13
14
|
package: string;
|
|
@@ -39,8 +40,8 @@ export class ApiDartWriter {
|
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
write(apis: ApiDto[], models: ModelDto[]) {
|
|
42
|
-
|
|
43
|
-
const template = fs.readFileSync(
|
|
43
|
+
const templatePath = path.join(__dirname, 'generators-writers', 'dart', 'templates', 'api.mustache');
|
|
44
|
+
const template = fs.readFileSync(templatePath, 'utf-8');
|
|
44
45
|
|
|
45
46
|
const grouped = this._groupByTag(apis);
|
|
46
47
|
|
|
@@ -5,6 +5,7 @@ import { Utils } from '../utils.js';
|
|
|
5
5
|
import { CommandLineArgs } from '@src/index.js';
|
|
6
6
|
import { ImportDefinitionDart } from './models/import-definition-dart.js';
|
|
7
7
|
import { Normalizator } from './normalizator.js';
|
|
8
|
+
import path from 'path';
|
|
8
9
|
|
|
9
10
|
interface ModelDefinitionDart {
|
|
10
11
|
modelName?: string;
|
|
@@ -36,6 +37,7 @@ export class ModelDartWriter {
|
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
write(models: ModelDto[]) {
|
|
40
|
+
const templatePath = path.join(__dirname, 'generators-writers', 'dart', 'templates', 'model.mustache');
|
|
39
41
|
const template = readFileSync('src/generators-writers/dart/templates/model.mustache', 'utf-8');
|
|
40
42
|
|
|
41
43
|
models.forEach(model => {
|
|
@@ -47,15 +49,15 @@ export class ModelDartWriter {
|
|
|
47
49
|
fields: [],
|
|
48
50
|
imports: [],
|
|
49
51
|
};
|
|
50
|
-
|
|
52
|
+
|
|
51
53
|
var imports = <ImportDefinitionDart[]>[];
|
|
52
54
|
|
|
53
55
|
model.properties.forEach(property => {
|
|
54
56
|
var fieldTypeName = this._mapTsTypeToDart(property.typeName);
|
|
55
57
|
|
|
56
|
-
// if (fieldTypeName.endsWith('Exception')) {
|
|
57
|
-
// debugger
|
|
58
|
-
// }
|
|
58
|
+
// if (fieldTypeName.endsWith('Exception')) {
|
|
59
|
+
// debugger
|
|
60
|
+
// }
|
|
59
61
|
|
|
60
62
|
fieldTypeName = Normalizator.getNormalizedTypeName(fieldTypeName);
|
|
61
63
|
|
package/dist/package.json
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@devlearning/swagger-generator",
|
|
3
|
-
"version": "1.0.9",
|
|
4
|
-
"description": "Swagger generator apis and models for Angular and NextJS",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"type": "module",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"dev": "ts-node --esm ./src/index.ts http://localhost:7550/swagger/ApiGateway/swagger.json autogeneration/output",
|
|
9
|
-
"debug-angular": "npx tsx src/index.ts http://localhost:7550/swagger/ApiGateway/swagger.json autogeneration/output angular moment",
|
|
10
|
-
"debug-nextjs": "npx tsx src/index.ts http://localhost:7550/swagger/ApiGateway/swagger.json autogeneration/output next date-fns",
|
|
11
|
-
"deploy": "npx tsc && npm publish"
|
|
12
|
-
},
|
|
13
|
-
"bin": {
|
|
14
|
-
"swgen": "./dist/index.js"
|
|
15
|
-
},
|
|
16
|
-
"keywords": [
|
|
17
|
-
"swagger",
|
|
18
|
-
"angular",
|
|
19
|
-
"nextjs",
|
|
20
|
-
"api",
|
|
21
|
-
"generator"
|
|
22
|
-
],
|
|
23
|
-
"author": "",
|
|
24
|
-
"license": "ISC",
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"cli-progress": "^3.12.0",
|
|
27
|
-
"node-fetch": "^3.3.2"
|
|
28
|
-
},
|
|
29
|
-
"node-fetch": "^3.3.2",
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"@types/cli-progress": "^3.11.6",
|
|
32
|
-
"@types/node": "^18.15.0",
|
|
33
|
-
"ts-node": "^10.9.1",
|
|
34
|
-
"tsconfig-paths": "^4.2.0",
|
|
35
|
-
"typescript": "^5.1.6"
|
|
36
|
-
}
|
|
37
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
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
|
-
`;
|
|
24
|
-
export const API_POST = `}`;
|
|
@@ -1,369 +0,0 @@
|
|
|
1
|
-
// import fs from 'fs';
|
|
2
|
-
// import { SwaggerComponent } from './models/swagger/swagger-component.js';
|
|
3
|
-
// import { Swagger, SwaggerParameter } from './models/swagger/swagger.js';
|
|
4
|
-
// import { SwaggerMethod } from './models/swagger/swagger-method.js';
|
|
5
|
-
// import { SwaggerComponentProperty } from './models/swagger/swagger-component-property.js';
|
|
6
|
-
// import { SwaggerSchema } from './models/swagger/swagger-schema.js';
|
|
7
|
-
// import { API_PRE, API_POST } from './api.constants.js';
|
|
8
|
-
// import { MODEL_POST, MODEL_PRE } from './model.constants.js';
|
|
9
|
-
export {};
|
|
10
|
-
// const contentTypeApplicationJson = 'application/json';
|
|
11
|
-
// const contentTypeMultipartFormData = 'multipart/form-data';
|
|
12
|
-
// export class GeneratorOld {
|
|
13
|
-
// private _swagger: Swagger;
|
|
14
|
-
// private _outputDirectory: string;
|
|
15
|
-
// private _outputFormat: 'angular' | 'next';
|
|
16
|
-
// constructor(swagger: Swagger, outputDirectory: string, outputFormat: 'angular' | 'next') {
|
|
17
|
-
// this._swagger = swagger;
|
|
18
|
-
// this._outputDirectory = outputDirectory;
|
|
19
|
-
// this._outputFormat = outputFormat
|
|
20
|
-
// }
|
|
21
|
-
// generateApi() {
|
|
22
|
-
// console.debug(`Start autogeneration Apis`);
|
|
23
|
-
// let apiMethods = ``;
|
|
24
|
-
// for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
25
|
-
// const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
26
|
-
// const swaggerMethod = this._swagger.paths[apiName];
|
|
27
|
-
// const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
28
|
-
// const swaggerMethodInfo = swaggerMethod[method];
|
|
29
|
-
// console.debug(`\tAPI - ${apiName} - ${method}`);
|
|
30
|
-
// let parametersString = this.retrieveParameters(swaggerMethodInfo, apiName);
|
|
31
|
-
// let queryParametersDeclaration = this.retrieveQueryParametersDeclaration(swaggerMethodInfo);
|
|
32
|
-
// let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
|
|
33
|
-
// let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
|
|
34
|
-
// if (returnTypeString == null) returnTypeString = 'void';
|
|
35
|
-
// let prepareRequestString = ``; //request = this._handleRequest(request);
|
|
36
|
-
// let haveRequest = swaggerMethodInfo.requestBody != null;
|
|
37
|
-
// let isMultiPart = haveRequest && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null;
|
|
38
|
-
// let httpOptions = 'httpOptions';
|
|
39
|
-
// if (haveRequest && !isMultiPart) {
|
|
40
|
-
// prepareRequestString = `const wrappedRequest = this._handleRequest(request);
|
|
41
|
-
// `;
|
|
42
|
-
// } else if (isMultiPart) {
|
|
43
|
-
// prepareRequestString = `const wrappedRequest = this._handleMultipart(request);
|
|
44
|
-
// `;
|
|
45
|
-
// httpOptions = `httpOptionsMultipart`;
|
|
46
|
-
// }
|
|
47
|
-
// apiMethods +=
|
|
48
|
-
// `
|
|
49
|
-
// public ${this.getApiNameNormalized(apiName)}(${parametersString}): Observable<${returnTypeString}> {
|
|
50
|
-
// ${queryParametersDeclaration}${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
|
|
51
|
-
// .pipe(
|
|
52
|
-
// map(x => this._handleResponse(x)),
|
|
53
|
-
// catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
|
|
54
|
-
// );
|
|
55
|
-
// }
|
|
56
|
-
// `;
|
|
57
|
-
// }
|
|
58
|
-
// fs.writeFileSync(this._outputDirectory + "/api.autogenerated.ts",
|
|
59
|
-
// `${API_PRE}
|
|
60
|
-
// ${apiMethods}
|
|
61
|
-
// ${API_POST}`,
|
|
62
|
-
// { flag: 'w' });
|
|
63
|
-
// }
|
|
64
|
-
// generateModel() {
|
|
65
|
-
// let usedTypes: string[] = [];
|
|
66
|
-
// let usedMultiPart: string[] = [];
|
|
67
|
-
// for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
|
|
68
|
-
// const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
|
|
69
|
-
// const swaggerMethod = this._swagger.paths[apiName];
|
|
70
|
-
// const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
71
|
-
// const swaggerMethodInfo = swaggerMethod[method];
|
|
72
|
-
// if (apiName == "/api/v{version}/TicketFile/Create") {
|
|
73
|
-
// debugger
|
|
74
|
-
// }
|
|
75
|
-
// let parametersRefType = swaggerMethodInfo.parameters?.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
|
|
76
|
-
// if (parametersRefType) {
|
|
77
|
-
// usedTypes = usedTypes.concat(parametersRefType);
|
|
78
|
-
// if (swaggerMethodInfo.responses[200].content[contentTypeApplicationJson]?.schema.$ref != null) {
|
|
79
|
-
// usedTypes.push(swaggerMethodInfo.responses[200].content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''));
|
|
80
|
-
// }
|
|
81
|
-
// if (swaggerMethodInfo.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref) {
|
|
82
|
-
// usedTypes.push(swaggerMethodInfo.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref.replace('#/components/schemas/', ''));
|
|
83
|
-
// }
|
|
84
|
-
// if (swaggerMethodInfo.requestBody?.content[contentTypeMultipartFormData]?.schema != null) {
|
|
85
|
-
// usedMultiPart.push(apiName);
|
|
86
|
-
// }
|
|
87
|
-
// }
|
|
88
|
-
// }
|
|
89
|
-
// this.retrieveNestedObjects(usedTypes);
|
|
90
|
-
// usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
|
|
91
|
-
// // usedTypes = usedTypes.filter((value, index, array) => {
|
|
92
|
-
// // array.indexOf(value) === index;
|
|
93
|
-
// // });
|
|
94
|
-
// // usedTypes.forEach(element => {
|
|
95
|
-
// // console.debug(element);
|
|
96
|
-
// // });
|
|
97
|
-
// console.debug(`Start autogeneration Models`);
|
|
98
|
-
// let models = ``;
|
|
99
|
-
// if (this._swagger.components != null
|
|
100
|
-
// && this._swagger.components != undefined
|
|
101
|
-
// && this._swagger.components.schemas != null
|
|
102
|
-
// && this._swagger.components.schemas != undefined) {
|
|
103
|
-
// for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
|
|
104
|
-
// const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
|
|
105
|
-
// if (modelName == 'ActivatedVoucherSnackListResponse') {
|
|
106
|
-
// console.debug("ActivatedVoucherSnackListResponse");
|
|
107
|
-
// }
|
|
108
|
-
// if (usedTypes.indexOf(modelName) < 0) {
|
|
109
|
-
// console.debug(`\tModel SKIP - ${modelName}`);
|
|
110
|
-
// continue
|
|
111
|
-
// };
|
|
112
|
-
// const swaggerCopmponent = this._swagger.components.schemas[modelName];
|
|
113
|
-
// console.debug(`\tModel - ${modelName}`);
|
|
114
|
-
// let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
|
|
115
|
-
// let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
|
|
116
|
-
// models +=
|
|
117
|
-
// `
|
|
118
|
-
// export ${type} ${modelName} {${content}
|
|
119
|
-
// }
|
|
120
|
-
// `;
|
|
121
|
-
// }
|
|
122
|
-
// }
|
|
123
|
-
// usedMultiPart.forEach(apiName => {
|
|
124
|
-
// const swaggerMethod = this._swagger.paths[apiName];
|
|
125
|
-
// const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
126
|
-
// const swaggerMethodInfo = swaggerMethod[method];
|
|
127
|
-
// let type = 'class';
|
|
128
|
-
// let modelName = this.getApiNameNormalized(apiName);
|
|
129
|
-
// let content = this.retrieveSchemaProperties(swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData].schema);
|
|
130
|
-
// models +=
|
|
131
|
-
// `
|
|
132
|
-
// export ${type} ${modelName} {${content}
|
|
133
|
-
// }
|
|
134
|
-
// `;
|
|
135
|
-
// });
|
|
136
|
-
// fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
|
|
137
|
-
// `${MODEL_PRE}
|
|
138
|
-
// ${models}
|
|
139
|
-
// ${MODEL_POST}`,
|
|
140
|
-
// { flag: 'w' });
|
|
141
|
-
// }
|
|
142
|
-
// retrieveParameters(swaggerMethodInfo: SwaggerMethod, apiName: string) {
|
|
143
|
-
// if (swaggerMethodInfo.requestBody != null && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null) {
|
|
144
|
-
// var modelName = this.getApiNameNormalized(apiName);
|
|
145
|
-
// return `request: Models.${modelName}`;
|
|
146
|
-
// } else {
|
|
147
|
-
// if (swaggerMethodInfo.requestBody != null) {
|
|
148
|
-
// return `request: Models.${swaggerMethodInfo.requestBody.content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', '')}`
|
|
149
|
-
// }
|
|
150
|
-
// let parameters = ``;
|
|
151
|
-
// swaggerMethodInfo.parameters?.filter(x => x.in == 'query').forEach(parameter => {
|
|
152
|
-
// if (parameter.schema.$ref != null) {
|
|
153
|
-
// parameters += `${this.toFirstLetterLowercase(parameter.name)}: Models.${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
|
|
154
|
-
// } else {
|
|
155
|
-
// const nativeType = this.getNativeType(parameter.schema);
|
|
156
|
-
// const nullable = !parameter.required ? '?' : '';
|
|
157
|
-
// parameters += `${this.toFirstLetterLowercase(parameter.name)}${nullable}: ${nativeType}, `;
|
|
158
|
-
// }
|
|
159
|
-
// });
|
|
160
|
-
// if (parameters.length > 2)
|
|
161
|
-
// parameters = parameters.substring(0, parameters.length - 2);
|
|
162
|
-
// return parameters;
|
|
163
|
-
// }
|
|
164
|
-
// }
|
|
165
|
-
// retrieveQueryParametersDeclaration(swaggerMethodInfo: SwaggerMethod) {
|
|
166
|
-
// if (swaggerMethodInfo.requestBody != null) return ``;
|
|
167
|
-
// let filteredParameters = swaggerMethodInfo.parameters?.filter(x => x.in == 'query');
|
|
168
|
-
// if (filteredParameters == null || filteredParameters == undefined || filteredParameters.length == 0) return ``;
|
|
169
|
-
// let parameters = ``;
|
|
170
|
-
// filteredParameters.forEach(parameter => {
|
|
171
|
-
// if (parameter.schema.$ref != null) {
|
|
172
|
-
// if (this.isEnum(parameter.schema.$ref) != null) {
|
|
173
|
-
// parameters += this.retrieveQueryParametersDeclarationStatement(parameter);
|
|
174
|
-
// } else {
|
|
175
|
-
// throw new Error("retrieveQueryParameters unmanaged parameter.schema.$ref");
|
|
176
|
-
// }
|
|
177
|
-
// } else {
|
|
178
|
-
// parameters += this.retrieveQueryParametersDeclarationStatement(parameter);
|
|
179
|
-
// }
|
|
180
|
-
// });
|
|
181
|
-
// // if (parameters.length > 2)
|
|
182
|
-
// // parameters = parameters.substring(0, parameters.length - 1);
|
|
183
|
-
// return parameters;
|
|
184
|
-
// }
|
|
185
|
-
// retrieveQueryParametersDeclarationStatement(parameter: SwaggerParameter) {
|
|
186
|
-
// let paramName = this.toFirstLetterLowercase(parameter.name);
|
|
187
|
-
// if (!parameter.required) {
|
|
188
|
-
// if (this.isDate(parameter.schema)) {
|
|
189
|
-
// return `let ${paramName}Param: string = ${paramName} != null && ${paramName} != undefined && ${paramName}.isValid() ? encodeURIComponent(this._momentToString(${paramName})) : '';
|
|
190
|
-
// `;
|
|
191
|
-
// } else {
|
|
192
|
-
// return `let ${paramName}Param: string = ${paramName} != null && ${paramName} != undefined ? encodeURIComponent('' + ${paramName}) : '';
|
|
193
|
-
// `;
|
|
194
|
-
// }
|
|
195
|
-
// } else {
|
|
196
|
-
// if (this.isDate(parameter.schema)) {
|
|
197
|
-
// return `let ${paramName}Param: string = encodeURIComponent(this._momentToString(${paramName}));
|
|
198
|
-
// `;
|
|
199
|
-
// } else {
|
|
200
|
-
// return `let ${paramName}Param: string = encodeURIComponent('' + ${paramName});
|
|
201
|
-
// `;
|
|
202
|
-
// }
|
|
203
|
-
// }
|
|
204
|
-
// }
|
|
205
|
-
// retrieveQueryParameters(swaggerMethodInfo: SwaggerMethod) {
|
|
206
|
-
// if (swaggerMethodInfo.requestBody != null) return ``;
|
|
207
|
-
// let filteredParameters = swaggerMethodInfo.parameters?.filter(x => x.in == 'query');
|
|
208
|
-
// if (filteredParameters == null || filteredParameters == undefined || filteredParameters.length == 0) return ``;
|
|
209
|
-
// let parameters = `?`;
|
|
210
|
-
// filteredParameters.forEach(parameter => {
|
|
211
|
-
// if (parameter.schema.$ref != null) {
|
|
212
|
-
// if (this.isEnum(parameter.schema.$ref) != null) {
|
|
213
|
-
// parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;;
|
|
214
|
-
// } else {
|
|
215
|
-
// throw new Error("retrieveQueryParameters unmanaged parameter.schema.$ref");
|
|
216
|
-
// }
|
|
217
|
-
// } else {
|
|
218
|
-
// parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;
|
|
219
|
-
// }
|
|
220
|
-
// });
|
|
221
|
-
// if (parameters.length > 1)
|
|
222
|
-
// parameters = parameters.substring(0, parameters.length - 1);
|
|
223
|
-
// return parameters;
|
|
224
|
-
// }
|
|
225
|
-
// retrieveReturnType(swaggerMethodInfo: SwaggerMethod) {
|
|
226
|
-
// if (swaggerMethodInfo.responses[200] == null)
|
|
227
|
-
// return 'void';
|
|
228
|
-
// try {
|
|
229
|
-
// if (swaggerMethodInfo.responses[200].content[contentTypeApplicationJson].schema.$ref != null)
|
|
230
|
-
// return `Models.${this.getObjectName(swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema.$ref)}`;
|
|
231
|
-
// } catch (error) {
|
|
232
|
-
// const errorMessage = "\t\tAttenzione forse hai dimenticato IActionResult e non hai tipizzato il tipo restituito dal servizio";
|
|
233
|
-
// console.error(`%c${errorMessage}`, 'color: red');
|
|
234
|
-
// throw new Error(errorMessage);
|
|
235
|
-
// }
|
|
236
|
-
// if (swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema.type != null)
|
|
237
|
-
// return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentTypeApplicationJson].schema);
|
|
238
|
-
// console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
|
|
239
|
-
// throw new Error("unmanaged swaggerMethodInfo");
|
|
240
|
-
// }
|
|
241
|
-
// retrieveType(swaggerComponentProperty: SwaggerComponentProperty) {
|
|
242
|
-
// if (swaggerComponentProperty.$ref != null)
|
|
243
|
-
// return swaggerComponentProperty.$ref.replace('#/components/schemas/', '');
|
|
244
|
-
// if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
|
|
245
|
-
// if (swaggerComponentProperty.items.$ref != null)
|
|
246
|
-
// return `${this.getObjectName(swaggerComponentProperty.items.$ref)}[]`;
|
|
247
|
-
// else
|
|
248
|
-
// return this.getNativeType(swaggerComponentProperty);
|
|
249
|
-
// if (swaggerComponentProperty.type != null)
|
|
250
|
-
// return this.getNativeType(swaggerComponentProperty);
|
|
251
|
-
// if (swaggerComponentProperty.type == null)
|
|
252
|
-
// return '';
|
|
253
|
-
// console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
|
|
254
|
-
// throw new Error("unmanaged swaggerMethodInfo");
|
|
255
|
-
// }
|
|
256
|
-
// parametrizeObject(objectName: string) {
|
|
257
|
-
// let component = this._swagger.components.schemas[this.getObjectName(objectName)];
|
|
258
|
-
// if (component == null || component.properties == null) return ``;
|
|
259
|
-
// console.debug(component.properties);
|
|
260
|
-
// return ``;
|
|
261
|
-
// }
|
|
262
|
-
// isEnum(objectName: string) {
|
|
263
|
-
// let component = this._swagger.components.schemas[this.getObjectName(objectName)];
|
|
264
|
-
// return component.enum != null;
|
|
265
|
-
// }
|
|
266
|
-
// isDate(schema: SwaggerSchema) {
|
|
267
|
-
// return schema.type == 'string' && schema.format == 'date-time';
|
|
268
|
-
// }
|
|
269
|
-
// getObjectName(ref: string) {
|
|
270
|
-
// return ref.replace('#/components/schemas/', '');
|
|
271
|
-
// }
|
|
272
|
-
// retrieveObjectContent(name: string, swaggerComponent: SwaggerComponent) {
|
|
273
|
-
// if (swaggerComponent.type == 'object')
|
|
274
|
-
// return this.retrieveComponentProperties(swaggerComponent);
|
|
275
|
-
// else if (swaggerComponent.type == 'integer')
|
|
276
|
-
// return this.retrieveEnumValues(name, swaggerComponent);
|
|
277
|
-
// }
|
|
278
|
-
// retrieveComponentProperties(swaggerCopmponent: SwaggerComponent) {
|
|
279
|
-
// if (swaggerCopmponent.properties == null) return ``;
|
|
280
|
-
// let properties = ``;
|
|
281
|
-
// for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
|
|
282
|
-
// const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
|
|
283
|
-
// properties +=
|
|
284
|
-
// `
|
|
285
|
-
// ${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`
|
|
286
|
-
// }
|
|
287
|
-
// return properties;
|
|
288
|
-
// }
|
|
289
|
-
// retrieveSchemaProperties(schema: SwaggerSchema) {
|
|
290
|
-
// if (schema.properties == null) return ``;
|
|
291
|
-
// let properties = ``;
|
|
292
|
-
// for (let j = 0; j < Object.getOwnPropertyNames(schema.properties).length; j++) {
|
|
293
|
-
// let propertyName = Object.getOwnPropertyNames(schema.properties)[j];
|
|
294
|
-
// properties +=
|
|
295
|
-
// `
|
|
296
|
-
// ${this.toFirstLetterLowercase(propertyName)}: ${this.retrieveType(schema.properties[propertyName])} | undefined;`
|
|
297
|
-
// }
|
|
298
|
-
// return properties;
|
|
299
|
-
// }
|
|
300
|
-
// retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
|
|
301
|
-
// if (swaggerCopmponent.enum == null) return ``;
|
|
302
|
-
// let properties = ``;
|
|
303
|
-
// for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
|
|
304
|
-
// const name = swaggerCopmponent.enum[index].split('-')[0].trim();
|
|
305
|
-
// const value = swaggerCopmponent.enum[index].split('-')[1].trim();
|
|
306
|
-
// properties +=
|
|
307
|
-
// `
|
|
308
|
-
// ${name} = ${value},`;
|
|
309
|
-
// }
|
|
310
|
-
// return properties;
|
|
311
|
-
// }
|
|
312
|
-
// retrieveNestedObjects(usedTypes: string[]) {
|
|
313
|
-
// for (let i = 0; i < usedTypes.length; i++) {
|
|
314
|
-
// const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
|
|
315
|
-
// // const name = usedTypes[i]
|
|
316
|
-
// // const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
|
|
317
|
-
// this.retrieveNestedObjectsRecursive(swaggerCopmponent, usedTypes);
|
|
318
|
-
// }
|
|
319
|
-
// }
|
|
320
|
-
// retrieveNestedObjectsRecursive(swaggerComponent: SwaggerComponent, usedTypes: string[]) {
|
|
321
|
-
// if (!swaggerComponent.properties) return;
|
|
322
|
-
// for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
|
|
323
|
-
// const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
|
|
324
|
-
// let nestedUsedType = '';
|
|
325
|
-
// if (swaggerComponent.properties[propertyName].$ref != null) {
|
|
326
|
-
// nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
|
|
327
|
-
// } else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
|
|
328
|
-
// nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
|
|
329
|
-
// }
|
|
330
|
-
// if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
|
|
331
|
-
// usedTypes.push(nestedUsedType);
|
|
332
|
-
// let nested = this._swagger.components.schemas[nestedUsedType];
|
|
333
|
-
// this.retrieveNestedObjectsRecursive(nested, usedTypes);
|
|
334
|
-
// }
|
|
335
|
-
// }
|
|
336
|
-
// }
|
|
337
|
-
// getNativeType(schema: SwaggerSchema): string {
|
|
338
|
-
// let nativeType = 'n.d.';
|
|
339
|
-
// if (schema.type == 'array') {
|
|
340
|
-
// nativeType = this.getNativeType(schema.items);
|
|
341
|
-
// nativeType += '[]';
|
|
342
|
-
// } else {
|
|
343
|
-
// if (schema.type == 'integer') nativeType = 'number';
|
|
344
|
-
// if (schema.type == 'string' && schema.format == null) nativeType = 'string';
|
|
345
|
-
// if (schema.type == 'string' && schema.format == 'date-time') nativeType = 'moment.Moment';
|
|
346
|
-
// if (schema.type == 'string' && schema.format == 'uuid') nativeType = 'string';
|
|
347
|
-
// if (schema.type == 'string' && schema.format == 'binary') nativeType = 'File';
|
|
348
|
-
// if (schema.type == 'number') nativeType = 'number';
|
|
349
|
-
// if (schema.type == 'boolean') nativeType = 'boolean';
|
|
350
|
-
// if (schema.type == 'object') nativeType = 'any';
|
|
351
|
-
// }
|
|
352
|
-
// if (nativeType.indexOf('n.d') == -1) {
|
|
353
|
-
// return nativeType;
|
|
354
|
-
// } else {
|
|
355
|
-
// console.error("unmanaged schema type", schema);
|
|
356
|
-
// throw new Error("unmanaged schema");
|
|
357
|
-
// }
|
|
358
|
-
// }
|
|
359
|
-
// getApiNameNormalized(apiName: string) {
|
|
360
|
-
// let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
361
|
-
// if (normalizedApiName.charAt(0) == '_') {
|
|
362
|
-
// normalizedApiName = normalizedApiName.slice(1);
|
|
363
|
-
// }
|
|
364
|
-
// return this.toFirstLetterLowercase(normalizedApiName);
|
|
365
|
-
// }
|
|
366
|
-
// toFirstLetterLowercase(value: string) {
|
|
367
|
-
// return value.charAt(0).toLowerCase() + value.slice(1);
|
|
368
|
-
// }
|
|
369
|
-
// }
|