openapi-ts-generator 4.89.5 → 4.145.2

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,18 @@ 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,
52
62
  name: schemaName,
63
+ camelSingularName: (0, lodash_1.camelCase)((0, pluralize_1.singular)(schemaName)),
53
64
  description: schemaWrapperInfo.description,
54
65
  referenceProperties: schemaWrapperInfo.referenceProperties,
55
66
  valueProperties: schemaWrapperInfo.valueProperties.filter(this.options.valuePropertyTypeFilterCallBack || generator_options_1.defaultFilter),
@@ -60,6 +71,17 @@ var OpenApiDocConverter = /** @class */ (function () {
60
71
  }
61
72
  return entities.filter(this.options.typeFilterCallBack || generator_options_1.defaultFilter);
62
73
  };
74
+ OpenApiDocConverter.prototype.buildSchemaWrapperInfoForEnum = function (schemaWrapperInfo) {
75
+ var _a;
76
+ var _this = this;
77
+ schemaWrapperInfo.isEnum = true;
78
+ (_a = schemaWrapperInfo.enumValues).push.apply(_a, (schemaWrapperInfo.componentSchemaObject.enum || []).map(function (x) {
79
+ var _a, _b;
80
+ var key = (_a = _this.startNumberregex.exec(x)) === null || _a === void 0 ? void 0 : _a.at(0);
81
+ var name = ((_b = _this.endAlphaNumRegex.exec(x)) === null || _b === void 0 ? void 0 : _b.at(0)) || '';
82
+ return { key: key ? +key : undefined, name: name, titleName: _.startCase(name) };
83
+ }));
84
+ };
63
85
  OpenApiDocConverter.prototype.buildSchemaWrapperInfo = function (schemaWrapperInfo) {
64
86
  for (var propertyName in schemaWrapperInfo.componentSchemaObject.properties) {
65
87
  if ((schemaWrapperInfo.propertySchemaObject = schemaWrapperInfo.componentSchemaObject.properties[propertyName]).type && // NOSONAR
@@ -129,12 +151,15 @@ var OpenApiDocConverter = /** @class */ (function () {
129
151
  return __assign(__assign({}, this.convertReferenceObjectToPropertyType(propertyName, schemaWrapperInfo)), { isArray: true });
130
152
  };
131
153
  OpenApiDocConverter.prototype.convertReferenceObjectToPropertyType = function (propertyName, schemaWrapperInfo) {
154
+ var _a;
155
+ var propertySchema = (((_a = this.apiDocument.components) === null || _a === void 0 ? void 0 : _a.schemas) || {})[this.parseRef(schemaWrapperInfo)];
132
156
  return {
133
157
  name: propertyName,
134
158
  snakeCaseName: _.snakeCase(propertyName).toUpperCase(),
135
159
  referenceTypeName: this.parseRef(schemaWrapperInfo),
136
160
  isArray: false,
137
161
  required: (schemaWrapperInfo.componentSchemaObject.required || []).indexOf(propertyName) > -1,
162
+ isEnum: (propertySchema.enum || []).length > 0,
138
163
  };
139
164
  };
140
165
  OpenApiDocConverter.prototype.getPropertyTypeScriptType = function (schemaWrapperInfo) {
@@ -160,7 +185,7 @@ var OpenApiDocConverter = /** @class */ (function () {
160
185
  var result = null;
161
186
  if (schemaWrapperInfo.propertyReferenceObject.$ref &&
162
187
  // tslint:disable-next-line: no-conditional-assignment
163
- (regexResult = this.regex.exec(schemaWrapperInfo.propertyReferenceObject.$ref)) // NOSONAR
188
+ (regexResult = this.endAlphaNumRegex.exec(schemaWrapperInfo.propertyReferenceObject.$ref)) // NOSONAR
164
189
  ) {
165
190
  schemaWrapperInfo.propertyReferenceObject.$ref = regexResult[0];
166
191
  result = schemaWrapperInfo.propertyReferenceObject.$ref;
@@ -168,11 +193,20 @@ var OpenApiDocConverter = /** @class */ (function () {
168
193
  return result || 'unknown';
169
194
  };
170
195
  OpenApiDocConverter.prototype.getImportTypes = function (entityName, schemaWrapperInfo) {
196
+ var _this = this;
171
197
  return schemaWrapperInfo.referenceProperties
172
198
  .map(function (t) { return t.referenceTypeName; })
173
199
  .filter(function (t) { return t !== entityName; })
174
200
  .filter(function (value, index, array) { return array.indexOf(value) === index; })
175
- .map(function (value) { return ({ name: value, kebabCasedTypeName: _.kebabCase(value) }); });
201
+ .map(function (value) {
202
+ var _a;
203
+ var propertySchema = (((_a = _this.apiDocument.components) === null || _a === void 0 ? void 0 : _a.schemas) || {})[value];
204
+ return {
205
+ name: value,
206
+ kebabCasedTypeName: _.kebabCase(value),
207
+ isEnum: (propertySchema.enum || []).length > 0,
208
+ };
209
+ });
176
210
  };
177
211
  OpenApiDocConverter.prototype.getIsRequired = function (propertyName, schemaWrapperInfo) {
178
212
  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.89.5",
3
+ "version": "4.145.2",
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",
@@ -37,28 +37,30 @@
37
37
  "homepage": "https://github.com/ikemtz/OpenApi-TS-Generator#readme",
38
38
  "devDependencies": {
39
39
  "@types/jest": "^27.4.1",
40
- "@types/lodash": "^4.14.180",
41
- "@types/node": "^16.11.26",
42
- "@typescript-eslint/eslint-plugin": "^5.16.0",
43
- "@typescript-eslint/parser": "^5.16.0",
44
- "eslint": "^8.11.0",
40
+ "@types/lodash": "^4.14.182",
41
+ "@types/node": "^16.11.27",
42
+ "@types/pluralize": "^0.0.29",
43
+ "@typescript-eslint/eslint-plugin": "^5.20.0",
44
+ "@typescript-eslint/parser": "^5.20.0",
45
+ "eslint": "^8.14.0",
45
46
  "eslint-config-prettier": "^8.5.0",
46
- "eslint-config-standard": "^16.0.2",
47
- "eslint-plugin-import": "^2.25.4",
47
+ "eslint-config-standard": "^16.0.3",
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
- "jest-junit": "^13.0.0",
52
+ "jest-junit": "^13.2.0",
52
53
  "openapi3-ts": "^2.0.2",
53
- "prettier": "^2.6.0",
54
+ "prettier": "^2.6.2",
54
55
  "rxjs": "^7.5.5",
55
56
  "ts-jest": "^27.1.3",
56
- "typescript": "^4.6.2"
57
+ "typescript": "^4.6.3"
57
58
  },
58
59
  "dependencies": {
59
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",
@@ -1,3 +1,4 @@
1
+ /* istanbul ignore file */
1
2
  /**
2
3
  * This file is generated by the openapi-ts-generator
3
4
  * #endpoints-ts.hbs
@@ -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
+ ];
@@ -1,3 +1,4 @@
1
+ /* istanbul ignore file */
1
2
  /**
2
3
  * This file is generated by the openapi-ts-generator
3
4
  * #form-group-factory.ts.hbs
@@ -1,3 +1,4 @@
1
+ /* istanbul ignore file */
1
2
  /**
2
3
  * This file is generated by the openapi-ts-generator
3
4
  * #generate-barrel-ts.hbs
@@ -1,3 +1,4 @@
1
+ /* istanbul ignore file */
1
2
  /**
2
3
  * This file is generated by the openapi-ts-generator
3
4
  * #model-properties.ts.hbs
@@ -1,19 +1,19 @@
1
+ /* istanbul ignore file */
1
2
  /**
2
3
  * This file is generated by the openapi-ts-generator
3
4
  * #model.ts.hbs
4
5
  * For issues or feature request, visit the repo: https://github.com/ikemtz/openapi-ts-generator
5
6
  * Do not edit.
6
7
  */
7
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
8
- {{#if importTypes}}
9
- {{#importTypes}}
10
- import { I{{name}} } from './{{kebabCasedTypeName}}.model';
11
- {{/importTypes}}
8
+ {{#if importTypes}}{{#importTypes}}{{#if isEnum}}
9
+ import { {{name}} } from './{{kebabCasedTypeName}}.enum';{{else}}
10
+ import { I{{name}} } from './{{kebabCasedTypeName}}.model';{{/if}}{{/importTypes}}
12
11
 
13
12
  {{/if}}{{#if description}}/**
14
13
  * {{description}}
15
14
  */
16
- {{/if}}export interface I{{{name}}} {
15
+ {{/if}}// eslint-disable-next-line @typescript-eslint/no-empty-interface
16
+ export interface I{{{name}}} {
17
17
  {{#valueProperties}}{{#if description}}/**
18
18
  * {{description}}
19
19
  */
@@ -21,6 +21,6 @@ import { I{{name}} } from './{{kebabCasedTypeName}}.model';
21
21
  {{name}}?: {{typeScriptType}}{{#if isArray}}[]{{/if}};
22
22
  {{/valueProperties}}
23
23
  {{#referenceProperties}}
24
- {{name}}?: I{{referenceTypeName}}{{#if isArray}}[]{{/if}};
24
+ {{name}}?: {{#unless isEnum}}I{{/unless}}{{referenceTypeName}}{{#if isArray}}[]{{/if}};
25
25
  {{/referenceProperties}}
26
26
  }