@devlearning/swagger-generator 0.0.36 → 0.0.39
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 +1 -2
- package/README.md +3 -1
- package/autogeneration/output/api.autogenerated.ts +34 -1209
- package/autogeneration/output/model.autogenerated.ts +4 -1391
- package/dist/api.constants.js +7 -1
- package/dist/generator.js +30 -21
- package/package.json +3 -2
- package/src/api.constants.ts +7 -1
- package/src/generator.ts +32 -20
package/dist/api.constants.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
export const API_PRE = `import { HttpClient } from '@angular/common/http';
|
|
2
2
|
import { Observable, catchError, map } from 'rxjs';
|
|
3
|
-
import { httpOptions, httpOptionsMultipart } from 'src/app/core/utils/http-options';
|
|
4
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 = {};
|
|
5
11
|
|
|
6
12
|
export abstract class ApiAutogeneratedService {
|
|
7
13
|
constructor(
|
package/dist/generator.js
CHANGED
|
@@ -40,7 +40,7 @@ export class Generator {
|
|
|
40
40
|
}
|
|
41
41
|
apiMethods +=
|
|
42
42
|
`
|
|
43
|
-
public ${
|
|
43
|
+
public ${this.getApiNameNormalized(apiName)}(${parametersString}): Observable<${returnTypeString}> {
|
|
44
44
|
${queryParametersDeclaration}${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
|
|
45
45
|
.pipe(
|
|
46
46
|
map(x => this._handleResponse(x)),
|
|
@@ -88,32 +88,37 @@ ${API_POST}`, { flag: 'w' });
|
|
|
88
88
|
// });
|
|
89
89
|
console.debug(`Start autogeneration Models`);
|
|
90
90
|
let models = ``;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
`
|
|
91
|
+
if (this._swagger.components != null
|
|
92
|
+
&& this._swagger.components != undefined
|
|
93
|
+
&& this._swagger.components.schemas != null
|
|
94
|
+
&& this._swagger.components.schemas != undefined) {
|
|
95
|
+
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
|
|
96
|
+
const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
|
|
97
|
+
if (modelName == 'ActivatedVoucherSnackListResponse') {
|
|
98
|
+
console.debug("ActivatedVoucherSnackListResponse");
|
|
99
|
+
}
|
|
100
|
+
if (usedTypes.indexOf(modelName) < 0) {
|
|
101
|
+
console.debug(`\tModel SKIP - ${modelName}`);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
;
|
|
105
|
+
const swaggerCopmponent = this._swagger.components.schemas[modelName];
|
|
106
|
+
console.debug(`\tModel - ${modelName}`);
|
|
107
|
+
let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
|
|
108
|
+
let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
|
|
109
|
+
models +=
|
|
110
|
+
`
|
|
107
111
|
export ${type} ${modelName} {${content}
|
|
108
112
|
}
|
|
109
113
|
`;
|
|
114
|
+
}
|
|
110
115
|
}
|
|
111
116
|
usedMultiPart.forEach(apiName => {
|
|
112
117
|
const swaggerMethod = this._swagger.paths[apiName];
|
|
113
118
|
const method = Object.getOwnPropertyNames(swaggerMethod)[0];
|
|
114
119
|
const swaggerMethodInfo = swaggerMethod[method];
|
|
115
120
|
let type = 'class';
|
|
116
|
-
let modelName = this.
|
|
121
|
+
let modelName = this.getApiNameNormalized(apiName);
|
|
117
122
|
let content = this.retrieveSchemaProperties(swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData].schema);
|
|
118
123
|
models +=
|
|
119
124
|
`
|
|
@@ -127,7 +132,7 @@ ${MODEL_POST}`, { flag: 'w' });
|
|
|
127
132
|
}
|
|
128
133
|
retrieveParameters(swaggerMethodInfo, apiName) {
|
|
129
134
|
if (swaggerMethodInfo.requestBody != null && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null) {
|
|
130
|
-
var modelName = this.
|
|
135
|
+
var modelName = this.getApiNameNormalized(apiName);
|
|
131
136
|
return `request: Models.${modelName}`;
|
|
132
137
|
}
|
|
133
138
|
else {
|
|
@@ -373,8 +378,12 @@ ${MODEL_POST}`, { flag: 'w' });
|
|
|
373
378
|
throw new Error("unmanaged schema");
|
|
374
379
|
}
|
|
375
380
|
}
|
|
376
|
-
|
|
377
|
-
|
|
381
|
+
getApiNameNormalized(apiName) {
|
|
382
|
+
let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
383
|
+
if (normalizedApiName.charAt(0) == '_') {
|
|
384
|
+
normalizedApiName = normalizedApiName.slice(1);
|
|
385
|
+
}
|
|
386
|
+
return this.toFirstLetterLowercase(normalizedApiName);
|
|
378
387
|
}
|
|
379
388
|
toFirstLetterLowercase(value) {
|
|
380
389
|
return value.charAt(0).toLowerCase() + value.slice(1);
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devlearning/swagger-generator",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.39",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"dev": "ts-node --esm ./src/index.ts http://localhost:
|
|
8
|
+
"dev": "ts-node --esm ./src/index.ts http://localhost:7550/swagger/ApiGateway/swagger.json autogeneration/output",
|
|
9
|
+
"debug": "npx tsx src/index.ts http://localhost:7550/swagger/ApiGateway/swagger.json autogeneration/output",
|
|
9
10
|
"deploy": "npx tsc && npm publish"
|
|
10
11
|
},
|
|
11
12
|
"bin": {
|
package/src/api.constants.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
export const API_PRE =
|
|
2
2
|
`import { HttpClient } from '@angular/common/http';
|
|
3
3
|
import { Observable, catchError, map } from 'rxjs';
|
|
4
|
-
import { httpOptions, httpOptionsMultipart } from 'src/app/core/utils/http-options';
|
|
5
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 = {};
|
|
6
12
|
|
|
7
13
|
export abstract class ApiAutogeneratedService {
|
|
8
14
|
constructor(
|
package/src/generator.ts
CHANGED
|
@@ -51,7 +51,7 @@ export class Generator {
|
|
|
51
51
|
|
|
52
52
|
apiMethods +=
|
|
53
53
|
`
|
|
54
|
-
public ${
|
|
54
|
+
public ${this.getApiNameNormalized(apiName)}(${parametersString}): Observable<${returnTypeString}> {
|
|
55
55
|
${queryParametersDeclaration}${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', wrappedRequest' : ''}, ${httpOptions})
|
|
56
56
|
.pipe(
|
|
57
57
|
map(x => this._handleResponse(x)),
|
|
@@ -113,30 +113,37 @@ ${API_POST}`,
|
|
|
113
113
|
|
|
114
114
|
console.debug(`Start autogeneration Models`);
|
|
115
115
|
let models = ``;
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
if (this._swagger.components != null
|
|
117
|
+
&& this._swagger.components != undefined
|
|
118
|
+
&& this._swagger.components.schemas != null
|
|
119
|
+
&& this._swagger.components.schemas != undefined) {
|
|
118
120
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
121
|
+
for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
|
|
122
|
+
const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
|
|
122
123
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
};
|
|
124
|
+
if (modelName == 'ActivatedVoucherSnackListResponse') {
|
|
125
|
+
console.debug("ActivatedVoucherSnackListResponse");
|
|
126
|
+
}
|
|
127
127
|
|
|
128
|
-
|
|
128
|
+
if (usedTypes.indexOf(modelName) < 0) {
|
|
129
|
+
console.debug(`\tModel SKIP - ${modelName}`);
|
|
130
|
+
continue
|
|
131
|
+
};
|
|
129
132
|
|
|
130
|
-
|
|
133
|
+
const swaggerCopmponent = this._swagger.components.schemas[modelName];
|
|
131
134
|
|
|
132
|
-
|
|
133
|
-
let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
|
|
135
|
+
console.debug(`\tModel - ${modelName}`);
|
|
134
136
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
+
let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
|
|
138
|
+
let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
|
|
139
|
+
|
|
140
|
+
models +=
|
|
141
|
+
`
|
|
137
142
|
export ${type} ${modelName} {${content}
|
|
138
143
|
}
|
|
139
144
|
`;
|
|
145
|
+
}
|
|
146
|
+
|
|
140
147
|
}
|
|
141
148
|
|
|
142
149
|
usedMultiPart.forEach(apiName => {
|
|
@@ -145,7 +152,7 @@ export ${type} ${modelName} {${content}
|
|
|
145
152
|
const swaggerMethodInfo = swaggerMethod[method];
|
|
146
153
|
|
|
147
154
|
let type = 'class';
|
|
148
|
-
let modelName = this.
|
|
155
|
+
let modelName = this.getApiNameNormalized(apiName);
|
|
149
156
|
let content = this.retrieveSchemaProperties(swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData].schema);
|
|
150
157
|
models +=
|
|
151
158
|
`
|
|
@@ -163,7 +170,7 @@ ${MODEL_POST}`,
|
|
|
163
170
|
|
|
164
171
|
retrieveParameters(swaggerMethodInfo: SwaggerMethod, apiName: string) {
|
|
165
172
|
if (swaggerMethodInfo.requestBody != null && swaggerMethodInfo.requestBody.content[contentTypeMultipartFormData] != null) {
|
|
166
|
-
var modelName = this.
|
|
173
|
+
var modelName = this.getApiNameNormalized(apiName);
|
|
167
174
|
return `request: Models.${modelName}`;
|
|
168
175
|
} else {
|
|
169
176
|
if (swaggerMethodInfo.requestBody != null) {
|
|
@@ -426,8 +433,13 @@ ${MODEL_POST}`,
|
|
|
426
433
|
}
|
|
427
434
|
}
|
|
428
435
|
|
|
429
|
-
|
|
430
|
-
|
|
436
|
+
getApiNameNormalized(apiName: string) {
|
|
437
|
+
let normalizedApiName = apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
|
|
438
|
+
|
|
439
|
+
if (normalizedApiName.charAt(0) == '_') {
|
|
440
|
+
normalizedApiName = normalizedApiName.slice(1);
|
|
441
|
+
}
|
|
442
|
+
return this.toFirstLetterLowercase(normalizedApiName);
|
|
431
443
|
}
|
|
432
444
|
|
|
433
445
|
toFirstLetterLowercase(value: string) {
|