@devlearning/swagger-generator 0.0.39 → 1.0.1
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 +8 -2
- package/autogeneration/output/api.autogenerated.ts +1443 -33
- package/autogeneration/output/model.autogenerated.ts +2788 -1
- package/dist/generator-old.js +369 -0
- package/dist/generator.js +217 -214
- package/dist/generators-writers/angular/api-angular-writer.js +111 -0
- package/dist/generators-writers/angular/constants.js +28 -0
- package/dist/generators-writers/angular/model-angular-writer.js +42 -0
- package/dist/generators-writers/nextjs/api-nextjs-writer.js +127 -0
- package/dist/generators-writers/nextjs/constants.js +4 -0
- package/dist/generators-writers/nextjs/model-nextjs-writer.js +41 -0
- package/dist/generators-writers/utils.js +20 -0
- package/dist/index.js +7 -5
- package/dist/model.constants.js +1 -4
- package/dist/models/api-dto.js +1 -0
- package/dist/models/enum-value-dto.js +1 -0
- package/dist/models/model-dto.js +1 -0
- package/dist/models/parameter-dto.js +1 -0
- package/dist/models/property-dto.js +1 -0
- package/dist/models/swagger/swagger-component-property.js +1 -0
- package/dist/models/swagger/swagger-component.js +1 -0
- package/dist/models/swagger/swagger-content.js +1 -0
- package/dist/models/swagger/swagger-info.js +1 -0
- package/dist/models/swagger/swagger-method.js +1 -0
- package/dist/models/swagger/swagger-schema.js +1 -0
- package/dist/models/swagger/swagger.js +1 -0
- package/dist/models/type-dto.js +1 -0
- package/package.json +6 -3
- package/src/generator-old.ts +450 -0
- package/src/generator.ts +238 -228
- package/src/generators-writers/angular/api-angular-writer.ts +139 -0
- package/src/generators-writers/angular/constants.ts +36 -0
- package/src/generators-writers/angular/model-angular-writer.ts +60 -0
- package/src/generators-writers/nextjs/api-nextjs-writer.ts +157 -0
- package/src/generators-writers/nextjs/constants.ts +5 -0
- package/src/generators-writers/nextjs/model-nextjs-writer.ts +59 -0
- package/src/generators-writers/utils.ts +27 -0
- package/src/index.ts +7 -5
- package/src/model.constants.ts +0 -7
- package/src/models/api-dto.ts +17 -0
- package/src/models/enum-value-dto.ts +4 -0
- package/src/models/model-dto.ts +9 -0
- package/src/models/parameter-dto.ts +8 -0
- package/src/models/property-dto.ts +5 -0
- package/src/models/type-dto.ts +4 -0
- package/src/swagger-downloader.ts +1 -1
- package/tsconfig.json +1 -1
- /package/src/models/{swagger-component-property.ts → swagger/swagger-component-property.ts} +0 -0
- /package/src/models/{swagger-component.ts → swagger/swagger-component.ts} +0 -0
- /package/src/models/{swagger-content.ts → swagger/swagger-content.ts} +0 -0
- /package/src/models/{swagger-info.ts → swagger/swagger-info.ts} +0 -0
- /package/src/models/{swagger-method.ts → swagger/swagger-method.ts} +0 -0
- /package/src/models/{swagger-schema.ts → swagger/swagger-schema.ts} +0 -0
- /package/src/models/{swagger.ts → swagger/swagger.ts} +0 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { API_POST, API_PRE } from './constants.js';
|
|
3
|
+
import { ApiDto } from '@src/models/api-dto.js';
|
|
4
|
+
import { Utils } from '../utils.js';
|
|
5
|
+
import { ParameterDto } from '@src/models/parameter-dto.js';
|
|
6
|
+
|
|
7
|
+
export class ApiAngularWriter {
|
|
8
|
+
private _outputDirectory: string;
|
|
9
|
+
|
|
10
|
+
constructor(_outputDirectory: string) {
|
|
11
|
+
this._outputDirectory = _outputDirectory;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
write(apis: ApiDto[]) {
|
|
15
|
+
let apiString = '';
|
|
16
|
+
|
|
17
|
+
apis.forEach(api => {
|
|
18
|
+
apiString += this._apiString(api);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
this._writeFile(apiString);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private _apiString(api: ApiDto) {
|
|
26
|
+
|
|
27
|
+
let apiNameNormalized = Utils.getApiNameNormalized(api.name);
|
|
28
|
+
let parametersString = this._parameters(api);
|
|
29
|
+
let queryParametersPreparation = this._queryParametersPreparation(api);
|
|
30
|
+
let requestPreparation = this._requestPreparation(api);
|
|
31
|
+
let queryParameters = this._queryParameters(api);
|
|
32
|
+
let returnTypeString = this._returnType(api);
|
|
33
|
+
let haveRequest = api.haveRequest;
|
|
34
|
+
let method = api.method.toLowerCase();
|
|
35
|
+
let httpOptions = api.isMultiPart ? 'httpOptionsMultiPart' : 'httpOptions';
|
|
36
|
+
|
|
37
|
+
let apiString = `
|
|
38
|
+
public ${apiNameNormalized}(${parametersString}): Observable<${returnTypeString}> {
|
|
39
|
+
${queryParametersPreparation}${requestPreparation}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${api.url}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
|
|
40
|
+
.pipe(
|
|
41
|
+
map(x => this._handleResponse(x)),
|
|
42
|
+
catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
return apiString;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private _parameters(api: ApiDto) {
|
|
51
|
+
let parametersString = '';
|
|
52
|
+
|
|
53
|
+
api.parameters.forEach(parameter => {
|
|
54
|
+
parametersString += `${parameter.name}${parameter.nullable ? '?' : ''}: ${parameter.typeName}, `;
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (api.parameters.length > 0)
|
|
58
|
+
parametersString = parametersString.substring(0, parametersString.length - 2);
|
|
59
|
+
|
|
60
|
+
return parametersString;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private _returnType(api: ApiDto) {
|
|
64
|
+
return api.returnType ? api.returnType.typeName : 'any';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private _queryParametersPreparation(api: ApiDto) {
|
|
68
|
+
let queryParametersPreparation = '';
|
|
69
|
+
|
|
70
|
+
api.parameters.forEach(parameter => {
|
|
71
|
+
if (parameter.isQuery) {
|
|
72
|
+
queryParametersPreparation += this._queryParametersPreparationStatement(parameter);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return queryParametersPreparation
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private _queryParametersPreparationStatement(parameter: ParameterDto) {
|
|
80
|
+
if (parameter.nullable) {
|
|
81
|
+
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
82
|
+
return `let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined && ${parameter.name}.isValid() ? encodeURIComponent(this._momentToString(${parameter.name})) : '';
|
|
83
|
+
`;
|
|
84
|
+
} else {
|
|
85
|
+
return `let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined ? encodeURIComponent('' + ${parameter.name}) : '';
|
|
86
|
+
`;
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
90
|
+
return `let ${parameter.name}Param: string = encodeURIComponent(this._momentToString(${parameter.name}));
|
|
91
|
+
`;
|
|
92
|
+
} else {
|
|
93
|
+
return `let ${parameter.name}Param: string = encodeURIComponent('' + ${parameter.name});
|
|
94
|
+
`;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private _queryParameters(api: ApiDto) {
|
|
100
|
+
let queryParameters = '';
|
|
101
|
+
|
|
102
|
+
api.parameters.forEach(parameter => {
|
|
103
|
+
if (parameter.isQuery) {
|
|
104
|
+
queryParameters += this._queryParametersStatement(parameter);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
if (queryParameters.length > 0) {
|
|
109
|
+
queryParameters = '?' + queryParameters.substring(0, queryParameters.length - 1);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return queryParameters;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private _queryParametersStatement(parameter: ParameterDto) {
|
|
116
|
+
if (parameter.swaggerParameter == null) return '';
|
|
117
|
+
|
|
118
|
+
if (!parameter.isQuery) return '';
|
|
119
|
+
|
|
120
|
+
return `${parameter.name}=\${${parameter.name}Param}&`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private _requestPreparation(api: ApiDto) {
|
|
124
|
+
if (!api.haveRequest) {
|
|
125
|
+
return '';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return `let wrappedRequest = this._handleRequest(request);
|
|
129
|
+
`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private _writeFile(apis: string) {
|
|
133
|
+
fs.writeFileSync(this._outputDirectory + "/api.autogenerated.ts",
|
|
134
|
+
`${API_PRE}
|
|
135
|
+
${apis}
|
|
136
|
+
${API_POST}`,
|
|
137
|
+
{ flag: 'w' });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const API_PRE =
|
|
2
|
+
`import { HttpClient } from '@angular/common/http';
|
|
3
|
+
import { Observable, catchError, map } from 'rxjs';
|
|
4
|
+
import * as Models from './model.autogenerated';
|
|
5
|
+
import { HttpHeaders } from "@angular/common/http";
|
|
6
|
+
|
|
7
|
+
export const httpOptions = {
|
|
8
|
+
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const httpOptionsMultipart = {};
|
|
12
|
+
|
|
13
|
+
export abstract class ApiAutogeneratedService {
|
|
14
|
+
constructor(
|
|
15
|
+
public _http: HttpClient,
|
|
16
|
+
public _baseUrl: string,
|
|
17
|
+
) { }
|
|
18
|
+
|
|
19
|
+
protected abstract _momentToString(moment: moment.Moment): string;
|
|
20
|
+
protected abstract _handleRequest<T>(request: T): T;
|
|
21
|
+
protected abstract _handleMultipart<T>(request: T): FormData;
|
|
22
|
+
protected abstract _handleResponse<T>(response: T): T;
|
|
23
|
+
protected abstract _handleError(error: any, obs: any): Observable<never>;
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
export const API_POST =
|
|
27
|
+
`}`;
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export const MODEL_PRE =
|
|
31
|
+
`import * as moment from 'moment';
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
export const MODEL_POST =
|
|
35
|
+
`
|
|
36
|
+
`;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { MODEL_POST, MODEL_PRE } from './constants.js';
|
|
3
|
+
import { ModelDto } from '@src/models/model-dto.js';
|
|
4
|
+
|
|
5
|
+
export class ModelAngularWriter {
|
|
6
|
+
private _outputDirectory: string;
|
|
7
|
+
|
|
8
|
+
constructor(_outputDirectory: string) {
|
|
9
|
+
this._outputDirectory = _outputDirectory;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
write(models: ModelDto[]) {
|
|
13
|
+
let modelString = '';
|
|
14
|
+
|
|
15
|
+
models.forEach(model => {
|
|
16
|
+
modelString += this._modelString(model);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
this._writeFile(modelString);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private _modelString(model: ModelDto) {
|
|
24
|
+
let modelString = `
|
|
25
|
+
export ${model.modelType} ${model.name} {
|
|
26
|
+
${this._properties(model)}${this._enumValues(model)}
|
|
27
|
+
}
|
|
28
|
+
`;
|
|
29
|
+
|
|
30
|
+
return modelString;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private _properties(model: ModelDto) {
|
|
34
|
+
let propertiesString = '';
|
|
35
|
+
|
|
36
|
+
model.properties.forEach(property => {
|
|
37
|
+
propertiesString += ` ${property.name}${property.nullable ? '?' : ''}: ${property.typeName};\n`;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return propertiesString.trimEnd();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private _enumValues(model: ModelDto) {
|
|
44
|
+
let enumValuesString = '';
|
|
45
|
+
|
|
46
|
+
model.enumValues.forEach(enumValue => {
|
|
47
|
+
enumValuesString += ` ${enumValue.name} = ${enumValue.value},\n`;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return enumValuesString.trimEnd();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private _writeFile(models: string) {
|
|
54
|
+
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
|
|
55
|
+
`${MODEL_PRE}
|
|
56
|
+
${models}
|
|
57
|
+
${MODEL_POST}`,
|
|
58
|
+
{ flag: 'w' });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { API_PRE } from './constants.js';
|
|
3
|
+
import { ApiDto } from '@src/models/api-dto.js';
|
|
4
|
+
import { Utils } from '../utils.js';
|
|
5
|
+
import { ParameterDto } from '@src/models/parameter-dto.js';
|
|
6
|
+
|
|
7
|
+
export class ApiNextJsWriter {
|
|
8
|
+
private _outputDirectory: string;
|
|
9
|
+
|
|
10
|
+
constructor(_outputDirectory: string) {
|
|
11
|
+
this._outputDirectory = _outputDirectory;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
write(apis: ApiDto[]) {
|
|
15
|
+
let apiString = '';
|
|
16
|
+
|
|
17
|
+
apis.forEach(api => {
|
|
18
|
+
apiString += this._apiString(api);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
this._writeFile(apiString);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private _apiString(api: ApiDto) {
|
|
26
|
+
|
|
27
|
+
let apiNameNormalized = Utils.toCamelCase(Utils.getApiNameNormalized(api.name));
|
|
28
|
+
let parametersString = this._parameters(api);
|
|
29
|
+
let queryParametersPreparation = this._queryParametersPreparation(api);
|
|
30
|
+
let requestPreparation = this._requestPreparation(api);
|
|
31
|
+
let queryParameters = this._queryParameters(api);
|
|
32
|
+
let returnTypeString = this._returnType(api);
|
|
33
|
+
let haveRequest = api.haveRequest;
|
|
34
|
+
let method = api.method.toLowerCase();
|
|
35
|
+
let httpOptions = api.isMultiPart ? 'httpOptionsMultiPart' : 'httpOptions';
|
|
36
|
+
let preparation = `${queryParametersPreparation}
|
|
37
|
+
${requestPreparation}`.trim();
|
|
38
|
+
preparation = preparation.length > 0 ? ` ${preparation}\n ` : '';
|
|
39
|
+
|
|
40
|
+
//\`\${API_BASE_URL}
|
|
41
|
+
let apiString = `
|
|
42
|
+
export const ${apiNameNormalized} = async (${parametersString}): Promise<${returnTypeString}> {
|
|
43
|
+
${preparation}const response = await axios.${method}<${returnTypeString}>(\`${api.url}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''});
|
|
44
|
+
return response.data;
|
|
45
|
+
}
|
|
46
|
+
`;
|
|
47
|
+
|
|
48
|
+
return apiString;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private _parameters(api: ApiDto) {
|
|
52
|
+
let parametersString = '';
|
|
53
|
+
|
|
54
|
+
api.parameters.forEach(parameter => {
|
|
55
|
+
parametersString += `${parameter.name}${parameter.nullable ? '?' : ''}: ${parameter.typeName}, `;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (api.parameters.length > 0)
|
|
59
|
+
parametersString = parametersString.substring(0, parametersString.length - 2);
|
|
60
|
+
|
|
61
|
+
return parametersString;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private _returnType(api: ApiDto) {
|
|
65
|
+
return api.returnType ? `Models.${api.returnType.typeName}` : 'any';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private _queryParametersPreparation(api: ApiDto) {
|
|
69
|
+
let queryParametersPreparation = '';
|
|
70
|
+
|
|
71
|
+
api.parameters.forEach(parameter => {
|
|
72
|
+
if (parameter.isQuery) {
|
|
73
|
+
queryParametersPreparation += this._queryParametersPreparationStatement(parameter);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return `${queryParametersPreparation}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private _queryParametersPreparationStatement(parameter: ParameterDto) {
|
|
81
|
+
if (parameter.nullable) {
|
|
82
|
+
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
83
|
+
return ` let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined && ${parameter.name}.isValid() ? encodeURIComponent(this._momentToString(${parameter.name})) : '';
|
|
84
|
+
`;
|
|
85
|
+
} else {
|
|
86
|
+
return ` let ${parameter.name}Param: string = ${parameter.name} != null && ${parameter.name} != undefined ? encodeURIComponent('' + ${parameter.name}) : '';
|
|
87
|
+
`;
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
if (Utils.isDate(parameter.swaggerParameter?.schema)) {
|
|
91
|
+
return ` let ${parameter.name}Param: string = encodeURIComponent(this._momentToString(${parameter.name}));
|
|
92
|
+
`;
|
|
93
|
+
} else {
|
|
94
|
+
return ` let ${parameter.name}Param: string = encodeURIComponent('' + ${parameter.name});
|
|
95
|
+
`;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private _queryParameters(api: ApiDto) {
|
|
101
|
+
let queryParameters = '';
|
|
102
|
+
|
|
103
|
+
api.parameters.forEach(parameter => {
|
|
104
|
+
if (parameter.isQuery) {
|
|
105
|
+
queryParameters += this._queryParametersStatement(parameter);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
if (queryParameters.length > 0) {
|
|
110
|
+
queryParameters = '?' + queryParameters.substring(0, queryParameters.length - 1);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return queryParameters;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private _queryParametersStatement(parameter: ParameterDto) {
|
|
117
|
+
if (parameter.swaggerParameter == null) return '';
|
|
118
|
+
|
|
119
|
+
if (!parameter.isQuery) return '';
|
|
120
|
+
|
|
121
|
+
return `${parameter.name}=\${${parameter.name}Param}&`;
|
|
122
|
+
// if (parameter.isEnum) {
|
|
123
|
+
// return `${parameter.name}=\${` + parameter.name + `Param}&`;
|
|
124
|
+
// } else {
|
|
125
|
+
// return `${parameter.name}=\${` + parameter.name + `}&`;
|
|
126
|
+
// }
|
|
127
|
+
// if (parameter.swaggerParameter.schema.$ref != null) {
|
|
128
|
+
// if (this.isEnum(parameter.schema.$ref) != null) {
|
|
129
|
+
// parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;;
|
|
130
|
+
// } else {
|
|
131
|
+
// throw new Error("retrieveQueryParameters unmanaged parameter.schema.$ref");
|
|
132
|
+
// }
|
|
133
|
+
// } else {
|
|
134
|
+
// parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `Param}&`;
|
|
135
|
+
// }
|
|
136
|
+
// let paramName = Utils.toFirstLetterLowercase(parameter.name);
|
|
137
|
+
// return `${parameter.swaggerParameter?.name}=\${${paramName}Param}&`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private _requestPreparation(api: ApiDto) {
|
|
141
|
+
if (!api.haveRequest) {
|
|
142
|
+
return ``;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return ` let wrappedRequest = this._handleRequest(request);
|
|
146
|
+
`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
private _writeFile(apis: string) {
|
|
152
|
+
fs.writeFileSync(this._outputDirectory + "/api.autogenerated.ts",
|
|
153
|
+
`${API_PRE}
|
|
154
|
+
${apis}`,
|
|
155
|
+
{ flag: 'w' });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { ModelDto } from '@src/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
|
+
propertiesString += ` ${property.name}${property.nullable ? '?' : ''}: ${property.typeName};\n`;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return propertiesString.trimEnd();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private _enumValues(model: ModelDto) {
|
|
43
|
+
let enumValuesString = '';
|
|
44
|
+
|
|
45
|
+
model.enumValues.forEach(enumValue => {
|
|
46
|
+
enumValuesString += ` ${enumValue.name} = ${enumValue.value},\n`;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return enumValuesString.trimEnd();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private _writeFile(models: string) {
|
|
53
|
+
fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
|
|
54
|
+
`
|
|
55
|
+
${models}
|
|
56
|
+
`,
|
|
57
|
+
{ flag: 'w' });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { SwaggerSchema } from "@src/models/swagger/swagger-schema.js";
|
|
2
|
+
|
|
3
|
+
export class Utils {
|
|
4
|
+
|
|
5
|
+
public static getApiNameNormalized(apiName: string) {
|
|
6
|
+
let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
7
|
+
|
|
8
|
+
if (normalizedApiName.charAt(0) == '_') {
|
|
9
|
+
normalizedApiName = normalizedApiName.slice(1);
|
|
10
|
+
}
|
|
11
|
+
return this.toFirstLetterLowercase(normalizedApiName);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public static toFirstLetterLowercase(value: string) {
|
|
15
|
+
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public static toCamelCase(input: string): string {
|
|
19
|
+
return input.replace(/_([a-zA-Z])/g, (_, letter) => letter.toUpperCase());
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public static isDate(schema?: SwaggerSchema) {
|
|
23
|
+
if (!schema) return false;
|
|
24
|
+
|
|
25
|
+
return schema.type == 'string' && schema.format == 'date-time';
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -5,14 +5,16 @@ import { SwaggerDownloader } from './swagger-downloader.js';
|
|
|
5
5
|
|
|
6
6
|
var args = process.argv.slice(2);
|
|
7
7
|
|
|
8
|
-
if (args.length !==
|
|
9
|
-
console.log("Warning: Requires
|
|
10
|
-
console.log("node index.js [swaggerJsonUrl] [outputDirectory]");
|
|
8
|
+
if (args.length !== 4) {
|
|
9
|
+
console.log("Warning: Requires 3 arguments");
|
|
10
|
+
console.log("node index.js [swaggerJsonUrl] [outputDirectory] [angular|next] [moment|date-fns]");
|
|
11
11
|
process.exit();
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
const swaggerJsonUrl = args[0];
|
|
15
15
|
const outputDirectory = args[1];
|
|
16
|
+
const outputFormat = args[2] == 'next' ? 'next' : 'angular';
|
|
17
|
+
const dateLib = args[3] == 'moment' ? 'moment' : 'date-fns';
|
|
16
18
|
|
|
17
19
|
//const excludedModels = ['Type', 'MethodBase', 'Assembly', 'MethodInfo']
|
|
18
20
|
// const apiUrl = args[0]//"http://localhost:5208";
|
|
@@ -23,9 +25,9 @@ const swaggerDownloader = new SwaggerDownloader();
|
|
|
23
25
|
|
|
24
26
|
swaggerDownloader.download(new URL(swaggerJsonUrl))
|
|
25
27
|
.then(swaggerDoc => {
|
|
26
|
-
return new Generator(swaggerDoc, outputDirectory);
|
|
28
|
+
return new Generator(swaggerDoc, outputDirectory, outputFormat, dateLib);
|
|
27
29
|
})
|
|
28
|
-
.then(generator => { generator.
|
|
30
|
+
.then(generator => { generator.generate(); });
|
|
29
31
|
|
|
30
32
|
|
|
31
33
|
// require('./index.js')({swaggerDownloader});
|
package/src/model.constants.ts
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ParameterDto } from "./parameter-dto.js";
|
|
2
|
+
import { SwaggerMethod } from "./swagger/swagger-method.js";
|
|
3
|
+
import { TypeDto } from "./type-dto.js";
|
|
4
|
+
|
|
5
|
+
export interface ApiDto {
|
|
6
|
+
name: string;
|
|
7
|
+
method: string;
|
|
8
|
+
url: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
parameters: ParameterDto[];
|
|
11
|
+
returnType: TypeDto | undefined;
|
|
12
|
+
haveRequest: boolean;
|
|
13
|
+
isMultiPart: boolean;
|
|
14
|
+
|
|
15
|
+
swaggerMethodKey: { [key: string]: SwaggerMethod; };
|
|
16
|
+
swaggerMethod: SwaggerMethod;
|
|
17
|
+
}
|
package/tsconfig.json
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|