openapi-ts-generator 4.144.1 → 4.192.4

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.
@@ -0,0 +1,8 @@
1
+ import { IGeneratorOptions } from '../models/generator-options';
2
+ import { IEntity, ITemplateData } from '../models/template-data';
3
+ import { BaseGenerator } from './base-generator';
4
+ export declare class EnumGenerator extends BaseGenerator<IEntity> {
5
+ readonly GeneratorName = "EnumGenerator";
6
+ constructor(options: IGeneratorOptions);
7
+ generate(templateData: ITemplateData): void;
8
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.EnumGenerator = void 0;
19
+ var _ = require("lodash");
20
+ var base_generator_1 = require("./base-generator");
21
+ var EnumGenerator = /** @class */ (function (_super) {
22
+ __extends(EnumGenerator, _super);
23
+ function EnumGenerator(options) {
24
+ var _this = this;
25
+ var _a;
26
+ _this = _super.call(this, options, (_a = options.templates) === null || _a === void 0 ? void 0 : _a.enum) || this;
27
+ _this.GeneratorName = 'EnumGenerator';
28
+ return _this;
29
+ }
30
+ EnumGenerator.prototype.generate = function (templateData) {
31
+ var _this = this;
32
+ var _a;
33
+ (_a = templateData.entities) === null || _a === void 0 ? void 0 : _a.filter(function (entity) { return entity.isEnum; }).forEach(function (entity) {
34
+ _super.prototype.generateFile.call(_this, "".concat(_this.generatorOptions.outputPath, "/").concat(_.kebabCase(entity.name), ".enum.ts"), entity);
35
+ });
36
+ };
37
+ return EnumGenerator;
38
+ }(base_generator_1.BaseGenerator));
39
+ exports.EnumGenerator = EnumGenerator;
@@ -2,3 +2,4 @@ export * from './model-generator';
2
2
  export * from './form-group-generator';
3
3
  export * from './model-properties-generator';
4
4
  export * from './barrel-generator';
5
+ export * from './enum-generator';
@@ -18,3 +18,4 @@ __exportStar(require("./model-generator"), exports);
18
18
  __exportStar(require("./form-group-generator"), exports);
19
19
  __exportStar(require("./model-properties-generator"), exports);
20
20
  __exportStar(require("./barrel-generator"), exports);
21
+ __exportStar(require("./enum-generator"), exports);
@@ -30,7 +30,7 @@ var ModelGenerator = /** @class */ (function (_super) {
30
30
  ModelGenerator.prototype.generate = function (templateData) {
31
31
  var _this = this;
32
32
  var _a;
33
- (_a = templateData.entities) === null || _a === void 0 ? void 0 : _a.forEach(function (entity) {
33
+ (_a = templateData.entities) === null || _a === void 0 ? void 0 : _a.filter(function (entity) { return !entity.isEnum; }).forEach(function (entity) {
34
34
  _super.prototype.generateFile.call(_this, "".concat(_this.generatorOptions.outputPath, "/").concat(_.kebabCase(entity.name), ".model.ts"), entity);
35
35
  });
36
36
  };
@@ -30,7 +30,7 @@ var ModelPropertiesGenerator = /** @class */ (function (_super) {
30
30
  ModelPropertiesGenerator.prototype.generate = function (templateData) {
31
31
  var _this = this;
32
32
  var _a;
33
- (_a = templateData.entities) === null || _a === void 0 ? void 0 : _a.filter(function (val) { var _a, _b; return ((_a = val.valueProperties) === null || _a === void 0 ? void 0 : _a.length) > 0 || ((_b = val.referenceProperties) === null || _b === void 0 ? void 0 : _b.length) > 0; }).forEach(function (entity) {
33
+ (_a = templateData.entities) === null || _a === void 0 ? void 0 : _a.filter(function (entity) { return !entity.isEnum; }).filter(function (val) { var _a, _b; return ((_a = val.valueProperties) === null || _a === void 0 ? void 0 : _a.length) > 0 || ((_b = val.referenceProperties) === null || _b === void 0 ? void 0 : _b.length) > 0; }).forEach(function (entity) {
34
34
  _super.prototype.generateFile.call(_this, "".concat(_this.generatorOptions.outputPath, "/").concat(_.kebabCase(entity.name), ".properties.ts"), entity);
35
35
  });
36
36
  };
package/index.js CHANGED
@@ -108,5 +108,7 @@ function generateOutput(options, templateData) {
108
108
  var endpointGenerator = new endpoints_generator_1.EndPointsGenerator(options);
109
109
  endpointGenerator.generate(templateData);
110
110
  var barrelGenerator = new generators_1.BarrelGenerator(options);
111
+ var enumGenerator = new generators_1.EnumGenerator(options);
112
+ enumGenerator.generate(templateData);
111
113
  barrelGenerator.generate();
112
114
  }
@@ -1,3 +1,3 @@
1
1
  import { IEntity, IValueProperty } from './template-data';
2
- export declare function nrsrxTypeFilterCallBack(val: IEntity, i: number, arr: IEntity[]): boolean;
3
- export declare function nrsrxValuePropertyTypeFilterCallBack(val: IValueProperty, i: number, arr: IValueProperty[]): boolean;
2
+ export declare function nrsrxTypeFilterCallBack(val: IEntity, _i: number, _arr: IEntity[]): boolean;
3
+ export declare function nrsrxValuePropertyTypeFilterCallBack(val: IValueProperty, _i: number, _arr: IValueProperty[]): boolean;
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.nrsrxValuePropertyTypeFilterCallBack = exports.nrsrxTypeFilterCallBack = void 0;
4
- function nrsrxTypeFilterCallBack(val, i, arr) {
5
- return !val.name.endsWith('ODataEnvelope');
4
+ var edmRegex = /^Edm[A-z]*Kind$/;
5
+ function nrsrxTypeFilterCallBack(val, _i, _arr) {
6
+ return !val.name.endsWith('ODataEnvelope') && !edmRegex.test(val.name);
6
7
  }
7
8
  exports.nrsrxTypeFilterCallBack = nrsrxTypeFilterCallBack;
8
- function nrsrxValuePropertyTypeFilterCallBack(val, i, arr) {
9
+ function nrsrxValuePropertyTypeFilterCallBack(val, _i, _arr) {
9
10
  return !val.name.startsWith('created') && !val.name.startsWith('updated');
10
11
  }
11
12
  exports.nrsrxValuePropertyTypeFilterCallBack = nrsrxValuePropertyTypeFilterCallBack;
@@ -4,6 +4,12 @@ import { IReferenceProperty, IValueProperty } from './template-data';
4
4
  export declare class SchemaWrapperInfo {
5
5
  propertySchemaObject: SchemaObject;
6
6
  propertyReferenceObject: ReferenceObject;
7
+ isEnum?: boolean;
8
+ readonly enumValues: (string | {
9
+ key?: number;
10
+ name: string;
11
+ titleName: string;
12
+ })[];
7
13
  readonly componentSchemaObject: SchemaObject;
8
14
  readonly valueProperties: IValueProperty[];
9
15
  referenceProperties: IReferenceProperty[];
@@ -10,6 +10,7 @@ var SchemaWrapperInfo = /** @class */ (function () {
10
10
  this.description = schemaItem.description;
11
11
  this.valueProperties = [];
12
12
  this.referenceProperties = [];
13
+ this.enumValues = [];
13
14
  }
14
15
  SchemaWrapperInfo.prototype.updateReferenceProperties = function (options) {
15
16
  this.referenceProperties = this.referenceProperties.filter(options.referencePropertyTypeFilterCallBack || generator_options_1.defaultFilter);
@@ -7,7 +7,14 @@ export interface IPath {
7
7
  endpoint: string;
8
8
  }
9
9
  export interface IEntity {
10
+ isEnum?: boolean;
11
+ enumValues: (string | {
12
+ key?: number;
13
+ name: string;
14
+ titleName: string;
15
+ })[];
10
16
  name: string;
17
+ camelSingularName: string;
11
18
  description?: string;
12
19
  importTypes: IImportType[];
13
20
  valueProperties: IValueProperty[];
@@ -16,6 +23,7 @@ export interface IEntity {
16
23
  export interface IImportType {
17
24
  kebabCasedTypeName: string;
18
25
  name: string;
26
+ isEnum: boolean;
19
27
  }
20
28
  export interface IValueProperty {
21
29
  name: string;
@@ -37,4 +45,5 @@ export interface IReferenceProperty {
37
45
  referenceTypeName: string;
38
46
  isArray: boolean;
39
47
  required: boolean;
48
+ isEnum?: boolean;
40
49
  }
@@ -5,11 +5,13 @@ import { IEntity, IImportType, IPath, IReferenceProperty, ITemplateData, IValueP
5
5
  export declare class OpenApiDocConverter {
6
6
  private readonly options;
7
7
  private readonly apiDocument;
8
- readonly regex: RegExp;
8
+ readonly endAlphaNumRegex: RegExp;
9
+ readonly startNumberregex: RegExp;
9
10
  constructor(options: IGeneratorOptions, apiDocument: OpenAPIObject);
10
11
  convertDocument(): ITemplateData;
11
12
  convertPaths(): IPath[];
12
13
  convertEntities(): IEntity[];
14
+ buildSchemaWrapperInfoForEnum(schemaWrapperInfo: SchemaWrapperInfo): void;
13
15
  buildSchemaWrapperInfo(schemaWrapperInfo: SchemaWrapperInfo): void;
14
16
  convertArray(propertyName: string, schemaWrapperInfo: SchemaWrapperInfo): void;
15
17
  convertSchemaObjectToPropertyType(propertyName: string, schemaWrapperInfo: SchemaWrapperInfo): IValueProperty;
@@ -15,11 +15,14 @@ exports.OpenApiDocConverter = void 0;
15
15
  var generator_options_1 = require("./models/generator-options");
16
16
  var schema_info_1 = require("./models/schema-info");
17
17
  var _ = require("lodash");
18
+ var pluralize_1 = require("pluralize");
19
+ var lodash_1 = require("lodash");
18
20
  var OpenApiDocConverter = /** @class */ (function () {
19
21
  function OpenApiDocConverter(options, apiDocument) {
20
22
  this.options = options;
21
23
  this.apiDocument = apiDocument;
22
- this.regex = /[A-z0-9]*$/s;
24
+ this.endAlphaNumRegex = /[A-z0-9]*$/s;
25
+ this.startNumberregex = /^\d*/;
23
26
  }
24
27
  OpenApiDocConverter.prototype.convertDocument = function () {
25
28
  var entities = this.convertEntities();
@@ -46,10 +49,20 @@ var OpenApiDocConverter = /** @class */ (function () {
46
49
  for (var schemaName in (_a = this.apiDocument.components) === null || _a === void 0 ? void 0 : _a.schemas) {
47
50
  if ((_b = this.apiDocument.components) === null || _b === void 0 ? void 0 : _b.schemas[schemaName]) {
48
51
  var schemaWrapperInfo = new schema_info_1.SchemaWrapperInfo((_c = this.apiDocument.components) === null || _c === void 0 ? void 0 : _c.schemas[schemaName]);
49
- this.buildSchemaWrapperInfo(schemaWrapperInfo);
52
+ if (schemaWrapperInfo.componentSchemaObject.enum) {
53
+ this.buildSchemaWrapperInfoForEnum(schemaWrapperInfo);
54
+ }
55
+ else {
56
+ this.buildSchemaWrapperInfo(schemaWrapperInfo);
57
+ }
50
58
  schemaWrapperInfo.updateReferenceProperties(this.options);
51
59
  var entity = {
60
+ isEnum: schemaWrapperInfo.isEnum,
61
+ enumValues: schemaWrapperInfo.enumValues.map(function (t) {
62
+ return typeof t === 'string' || t instanceof String ? t : __assign(__assign({}, t), { key: t.key || 0 });
63
+ }),
52
64
  name: schemaName,
65
+ camelSingularName: (0, lodash_1.camelCase)((0, pluralize_1.singular)(schemaName)),
53
66
  description: schemaWrapperInfo.description,
54
67
  referenceProperties: schemaWrapperInfo.referenceProperties,
55
68
  valueProperties: schemaWrapperInfo.valueProperties.filter(this.options.valuePropertyTypeFilterCallBack || generator_options_1.defaultFilter),
@@ -60,6 +73,17 @@ var OpenApiDocConverter = /** @class */ (function () {
60
73
  }
61
74
  return entities.filter(this.options.typeFilterCallBack || generator_options_1.defaultFilter);
62
75
  };
76
+ OpenApiDocConverter.prototype.buildSchemaWrapperInfoForEnum = function (schemaWrapperInfo) {
77
+ var _a;
78
+ var _this = this;
79
+ schemaWrapperInfo.isEnum = true;
80
+ (_a = schemaWrapperInfo.enumValues).push.apply(_a, (schemaWrapperInfo.componentSchemaObject.enum || []).map(function (x) {
81
+ var _a, _b;
82
+ var key = (_a = _this.startNumberregex.exec(x)) === null || _a === void 0 ? void 0 : _a.at(0);
83
+ var name = ((_b = _this.endAlphaNumRegex.exec(x)) === null || _b === void 0 ? void 0 : _b.at(0)) || '';
84
+ return { key: key ? +key : 0, name: name, titleName: _.startCase(name) };
85
+ }));
86
+ };
63
87
  OpenApiDocConverter.prototype.buildSchemaWrapperInfo = function (schemaWrapperInfo) {
64
88
  for (var propertyName in schemaWrapperInfo.componentSchemaObject.properties) {
65
89
  if ((schemaWrapperInfo.propertySchemaObject = schemaWrapperInfo.componentSchemaObject.properties[propertyName]).type && // NOSONAR
@@ -129,12 +153,15 @@ var OpenApiDocConverter = /** @class */ (function () {
129
153
  return __assign(__assign({}, this.convertReferenceObjectToPropertyType(propertyName, schemaWrapperInfo)), { isArray: true });
130
154
  };
131
155
  OpenApiDocConverter.prototype.convertReferenceObjectToPropertyType = function (propertyName, schemaWrapperInfo) {
156
+ var _a;
157
+ var propertySchema = (((_a = this.apiDocument.components) === null || _a === void 0 ? void 0 : _a.schemas) || {})[this.parseRef(schemaWrapperInfo)];
132
158
  return {
133
159
  name: propertyName,
134
160
  snakeCaseName: _.snakeCase(propertyName).toUpperCase(),
135
161
  referenceTypeName: this.parseRef(schemaWrapperInfo),
136
162
  isArray: false,
137
163
  required: (schemaWrapperInfo.componentSchemaObject.required || []).indexOf(propertyName) > -1,
164
+ isEnum: (propertySchema.enum || []).length > 0,
138
165
  };
139
166
  };
140
167
  OpenApiDocConverter.prototype.getPropertyTypeScriptType = function (schemaWrapperInfo) {
@@ -160,7 +187,7 @@ var OpenApiDocConverter = /** @class */ (function () {
160
187
  var result = null;
161
188
  if (schemaWrapperInfo.propertyReferenceObject.$ref &&
162
189
  // tslint:disable-next-line: no-conditional-assignment
163
- (regexResult = this.regex.exec(schemaWrapperInfo.propertyReferenceObject.$ref)) // NOSONAR
190
+ (regexResult = this.endAlphaNumRegex.exec(schemaWrapperInfo.propertyReferenceObject.$ref)) // NOSONAR
164
191
  ) {
165
192
  schemaWrapperInfo.propertyReferenceObject.$ref = regexResult[0];
166
193
  result = schemaWrapperInfo.propertyReferenceObject.$ref;
@@ -168,11 +195,20 @@ var OpenApiDocConverter = /** @class */ (function () {
168
195
  return result || 'unknown';
169
196
  };
170
197
  OpenApiDocConverter.prototype.getImportTypes = function (entityName, schemaWrapperInfo) {
198
+ var _this = this;
171
199
  return schemaWrapperInfo.referenceProperties
172
200
  .map(function (t) { return t.referenceTypeName; })
173
201
  .filter(function (t) { return t !== entityName; })
174
202
  .filter(function (value, index, array) { return array.indexOf(value) === index; })
175
- .map(function (value) { return ({ name: value, kebabCasedTypeName: _.kebabCase(value) }); });
203
+ .map(function (value) {
204
+ var _a;
205
+ var propertySchema = (((_a = _this.apiDocument.components) === null || _a === void 0 ? void 0 : _a.schemas) || {})[value];
206
+ return {
207
+ name: value,
208
+ kebabCasedTypeName: _.kebabCase(value),
209
+ isEnum: (propertySchema.enum || []).length > 0,
210
+ };
211
+ });
176
212
  };
177
213
  OpenApiDocConverter.prototype.getIsRequired = function (propertyName, schemaWrapperInfo) {
178
214
  return (((schemaWrapperInfo.componentSchemaObject.required || []).indexOf(propertyName) > -1 ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-generator",
3
- "version": "4.144.1",
3
+ "version": "4.192.4",
4
4
  "description": "Based on swagger-ts-generator, this is a type script model generator specifically for services with OpenApi spec documentation.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -39,26 +39,28 @@
39
39
  "@types/jest": "^27.4.1",
40
40
  "@types/lodash": "^4.14.182",
41
41
  "@types/node": "^16.11.27",
42
- "@typescript-eslint/eslint-plugin": "^5.20.0",
43
- "@typescript-eslint/parser": "^5.20.0",
44
- "eslint": "^8.14.0",
42
+ "@types/pluralize": "^0.0.29",
43
+ "@typescript-eslint/eslint-plugin": "^5.30.5",
44
+ "@typescript-eslint/parser": "^5.30.5",
45
+ "eslint": "^8.19.0",
45
46
  "eslint-config-prettier": "^8.5.0",
46
- "eslint-config-standard": "^16.0.3",
47
+ "eslint-config-standard": "^17.0.0",
47
48
  "eslint-plugin-import": "^2.26.0",
48
49
  "eslint-plugin-node": "^11.1.0",
49
50
  "eslint-plugin-promise": "^6.0.0",
50
51
  "jest": "^27.5.1",
51
52
  "jest-junit": "^13.2.0",
52
53
  "openapi3-ts": "^2.0.2",
53
- "prettier": "^2.6.2",
54
- "rxjs": "^7.5.5",
54
+ "prettier": "^2.7.1",
55
+ "rxjs": "^7.5.6",
55
56
  "ts-jest": "^27.1.3",
56
57
  "typescript": "^4.6.3"
57
58
  },
58
59
  "dependencies": {
59
- "axios": ">=0.27.2",
60
+ "axios": ">=0.x",
60
61
  "handlebars": ">=4.x",
61
- "lodash": ">=4.x"
62
+ "lodash": ">=4.x",
63
+ "pluralize": "^8.0.0"
62
64
  },
63
65
  "peerDependencies": {
64
66
  "axios": ">=0.x",
@@ -0,0 +1,22 @@
1
+ /* istanbul ignore file */
2
+ /**
3
+ * This file is generated by the openapi-ts-generator
4
+ * #enum.ts.hbs
5
+ * For issues or feature request, visit the repo: https://github.com/ikemtz/openapi-ts-generator
6
+ * Do not edit.
7
+ */
8
+
9
+ {{#if description}}/**
10
+ * {{description}}
11
+ */
12
+ {{/if}}export enum {{{name}}} {
13
+ {{#enumValues}}
14
+ {{name}}{{#if key}} = {{key}}{{/if}},
15
+ {{/enumValues}}
16
+ }
17
+
18
+ export const {{{camelSingularName}}}Values = [
19
+ {{#enumValues}}
20
+ { {{#if key}}key: {{key}}, {{/if}}name: '{{titleName}}'},
21
+ {{/enumValues}}
22
+ ];
@@ -5,16 +5,15 @@
5
5
  * For issues or feature request, visit the repo: https://github.com/ikemtz/openapi-ts-generator
6
6
  * Do not edit.
7
7
  */
8
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
9
- {{#if importTypes}}
10
- {{#importTypes}}
11
- import { I{{name}} } from './{{kebabCasedTypeName}}.model';
12
- {{/importTypes}}
8
+ {{#if importTypes}}{{#importTypes}}{{#if isEnum}}
9
+ import { {{name}} } from './{{kebabCasedTypeName}}.enum';{{else}}
10
+ import { I{{name}} } from './{{kebabCasedTypeName}}.model';{{/if}}{{/importTypes}}
13
11
 
14
12
  {{/if}}{{#if description}}/**
15
13
  * {{description}}
16
14
  */
17
- {{/if}}export interface I{{{name}}} {
15
+ {{/if}}// eslint-disable-next-line @typescript-eslint/no-empty-interface
16
+ export interface I{{{name}}} {
18
17
  {{#valueProperties}}{{#if description}}/**
19
18
  * {{description}}
20
19
  */
@@ -22,6 +21,6 @@ import { I{{name}} } from './{{kebabCasedTypeName}}.model';
22
21
  {{name}}?: {{typeScriptType}}{{#if isArray}}[]{{/if}};
23
22
  {{/valueProperties}}
24
23
  {{#referenceProperties}}
25
- {{name}}?: I{{referenceTypeName}}{{#if isArray}}[]{{/if}};
24
+ {{name}}?: {{#unless isEnum}}I{{/unless}}{{referenceTypeName}}{{#if isArray}}[]{{/if}};
26
25
  {{/referenceProperties}}
27
26
  }