@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.
@@ -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 ${apiName.replace('/api/v{version}/', '').replaceAll('/', '_')}(${parametersString}): Observable<${returnTypeString}> {
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
- for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
92
- const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
93
- if (modelName == 'ActivatedVoucherSnackListResponse') {
94
- console.debug("ActivatedVoucherSnackListResponse");
95
- }
96
- if (usedTypes.indexOf(modelName) < 0) {
97
- console.debug(`\tModel SKIP - ${modelName}`);
98
- continue;
99
- }
100
- ;
101
- const swaggerCopmponent = this._swagger.components.schemas[modelName];
102
- console.debug(`\tModel - ${modelName}`);
103
- let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
104
- let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
105
- models +=
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.getModelNameByApi(apiName);
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.getModelNameByApi(apiName);
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
- getModelNameByApi(apiName) {
377
- return apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
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.36",
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:5208/swagger/v1/swagger.json autogeneration/output",
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": {
@@ -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 ${apiName.replace('/api/v{version}/', '').replaceAll('/', '_')}(${parametersString}): Observable<${returnTypeString}> {
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
- for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
117
- const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
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
- if (modelName == 'ActivatedVoucherSnackListResponse') {
120
- console.debug("ActivatedVoucherSnackListResponse");
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
- if (usedTypes.indexOf(modelName) < 0) {
124
- console.debug(`\tModel SKIP - ${modelName}`);
125
- continue
126
- };
124
+ if (modelName == 'ActivatedVoucherSnackListResponse') {
125
+ console.debug("ActivatedVoucherSnackListResponse");
126
+ }
127
127
 
128
- const swaggerCopmponent = this._swagger.components.schemas[modelName];
128
+ if (usedTypes.indexOf(modelName) < 0) {
129
+ console.debug(`\tModel SKIP - ${modelName}`);
130
+ continue
131
+ };
129
132
 
130
- console.debug(`\tModel - ${modelName}`);
133
+ const swaggerCopmponent = this._swagger.components.schemas[modelName];
131
134
 
132
- let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
133
- let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
135
+ console.debug(`\tModel - ${modelName}`);
134
136
 
135
- models +=
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.getModelNameByApi(apiName);
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.getModelNameByApi(apiName);
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
- getModelNameByApi(apiName: string) {
430
- return apiName.replace('/api/v{version}/', '').replaceAll('/', '_');
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) {