ng-openapi 0.1.14 → 0.1.15-pr-25-union-support-1044cca.0

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.
Files changed (4) hide show
  1. package/cli.cjs +66 -18
  2. package/index.d.ts +2 -1
  3. package/index.js +69 -17
  4. package/package.json +1 -1
package/cli.cjs CHANGED
@@ -44,14 +44,11 @@ var fs4 = __toESM(require("fs"));
44
44
  var path12 = __toESM(require("path"));
45
45
 
46
46
  // package.json
47
- var version = "0.1.13";
47
+ var version = "0.1.14";
48
48
 
49
49
  // src/lib/core/generator.ts
50
50
  var import_ts_morph7 = require("ts-morph");
51
51
 
52
- // src/lib/generators/type/type.generator.ts
53
- var import_ts_morph = require("ts-morph");
54
-
55
52
  // ../shared/src/utils/string.utils.ts
56
53
  function camelCase(str) {
57
54
  return str.replace(/[-_\s]+(.)?/g, (_, char) => char ? char.toUpperCase() : "").replace(/^./, (char) => char.toLowerCase());
@@ -61,6 +58,10 @@ function pascalCase(str) {
61
58
  return str.replace(/[-_\s]+(.)?/g, (_, char) => char ? char.toUpperCase() : "").replace(/^./, (char) => char.toUpperCase());
62
59
  }
63
60
  __name(pascalCase, "pascalCase");
61
+ function screamingSnakeCase(str) {
62
+ return str.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[-\s]+/g, "_").toUpperCase();
63
+ }
64
+ __name(screamingSnakeCase, "screamingSnakeCase");
64
65
 
65
66
  // ../shared/src/utils/type.utils.ts
66
67
  function getTypeScriptType(schemaOrType, config, formatOrNullable, isNullable, context = "type") {
@@ -114,8 +115,7 @@ function getTypeScriptType(schemaOrType, config, formatOrNullable, isNullable, c
114
115
  case "null":
115
116
  return "null";
116
117
  default:
117
- console.warn(`Unknown swagger type: ${schema.type}`);
118
- return nullableType("any", nullable);
118
+ return nullableType("unknown", nullable);
119
119
  }
120
120
  }
121
121
  __name(getTypeScriptType, "getTypeScriptType");
@@ -614,6 +614,7 @@ var SwaggerParser = class _SwaggerParser {
614
614
  };
615
615
 
616
616
  // src/lib/generators/type/type.generator.ts
617
+ var import_ts_morph = require("ts-morph");
617
618
  var TypeGenerator = class {
618
619
  static {
619
620
  __name(this, "TypeGenerator");
@@ -679,30 +680,74 @@ var TypeGenerator = class {
679
680
  }
680
681
  collectEnumStructure(name, definition) {
681
682
  if (!definition.enum?.length) return;
683
+ const docs = !this.config.options.generateEnumBasedOnDescription && definition.description ? [
684
+ definition.description
685
+ ] : void 0;
686
+ if (this.config.options.enumStyle === "enum") {
687
+ const statement = this.buildEnumAsEnum(name, definition, docs);
688
+ this.statements.push(...statement);
689
+ } else {
690
+ const statement = this.buildEnumAsUnion(name, definition, docs);
691
+ this.statements.push(...statement);
692
+ }
693
+ }
694
+ buildEnumAsEnum(name, definition, docs) {
695
+ if (!definition.enum?.length) throw Error("Enum definition has no values");
696
+ const statements = [];
682
697
  const isStringEnum = definition.enum.some((value) => typeof value === "string");
683
698
  if (isStringEnum) {
684
- const unionType = definition.enum.map((value) => typeof value === "string" ? `'${this.escapeString(value)}'` : String(value)).join(" | ");
685
- this.statements.push({
686
- kind: import_ts_morph.StructureKind.TypeAlias,
699
+ const members = definition.enum.map((value) => ({
700
+ name: this.toEnumKey(value),
701
+ value: `${String(value)}`
702
+ }));
703
+ statements.push({
704
+ kind: import_ts_morph.StructureKind.Enum,
687
705
  name,
688
- type: unionType,
689
706
  isExported: true,
690
- docs: definition.description ? [
691
- definition.description
692
- ] : void 0
707
+ docs,
708
+ members
693
709
  });
694
710
  } else {
695
711
  const members = this.buildEnumMembers(definition);
696
- this.statements.push({
712
+ statements.push({
697
713
  kind: import_ts_morph.StructureKind.Enum,
698
714
  name,
699
715
  isExported: true,
700
- docs: definition.description ? [
701
- definition.description
702
- ] : void 0,
716
+ docs,
703
717
  members
704
718
  });
705
719
  }
720
+ return statements;
721
+ }
722
+ buildEnumAsUnion(name, definition, docs) {
723
+ if (!definition.enum?.length) throw Error("Enum definition has no values");
724
+ const statements = [];
725
+ const objectProperties = [];
726
+ const unionType = definition.enum.map((value) => {
727
+ const key = this.toEnumKey(value);
728
+ const val = typeof value === "string" ? `'${this.escapeString(value)}'` : isNaN(value) ? `'${value}'` : `${value}`;
729
+ objectProperties.push(`${key}: ${val} as ${name}`);
730
+ return val;
731
+ }).join(" | ");
732
+ statements.push({
733
+ kind: import_ts_morph.StructureKind.TypeAlias,
734
+ name,
735
+ type: unionType,
736
+ isExported: true,
737
+ docs
738
+ });
739
+ statements.push({
740
+ kind: import_ts_morph.StructureKind.VariableStatement,
741
+ declarationKind: import_ts_morph.VariableDeclarationKind.Const,
742
+ isExported: true,
743
+ declarations: [
744
+ {
745
+ name,
746
+ initializer: `{ ${objectProperties.join(",\n")} }`
747
+ }
748
+ ]
749
+ });
750
+ return statements;
706
751
  }
707
752
  buildEnumMembers(definition) {
708
753
  if (definition.description && this.config.options.generateEnumBasedOnDescription) {
@@ -988,7 +1033,10 @@ var TypeGenerator = class {
988
1033
  return name;
989
1034
  }
990
1035
  toEnumKey(value) {
991
- return value.toString().replace(/[^a-zA-Z0-9]/g, "_").replace(/^([0-9])/, "_$1").toUpperCase();
1036
+ const str = value.toString();
1037
+ const hasLeadingMinus = str.startsWith("-");
1038
+ const snakeCased = screamingSnakeCase(str);
1039
+ return hasLeadingMinus ? snakeCased.replace("_", "_n") : snakeCased.replace(/^([0-9])/, "_$1");
992
1040
  }
993
1041
  getArrayItemType(items) {
994
1042
  if (Array.isArray(items)) {
package/index.d.ts CHANGED
@@ -221,6 +221,7 @@ interface NgOpenapiClientConfig {
221
221
  declare function camelCase(str: string): string;
222
222
  declare function kebabCase(str: string): string;
223
223
  declare function pascalCase(str: string): string;
224
+ declare function screamingSnakeCase(str: string): string;
224
225
 
225
226
  /**
226
227
  * Convert OpenAPI/Swagger types to TypeScript types
@@ -277,4 +278,4 @@ declare function validateInput(inputPath: string): void;
277
278
  */
278
279
  declare function generateFromConfig(config: GeneratorConfig): Promise<void>;
279
280
 
280
- export { BASE_INTERCEPTOR_HEADER_COMMENT, type EnumValueObject, type GeneratorConfig, type GetMethodGenerationContext, HTTP_RESOURCE_GENERATOR_HEADER_COMMENT, type IPluginGenerator, type IPluginGeneratorClass, MAIN_INDEX_GENERATOR_HEADER_COMMENT, type MethodGenerationContext, type NgOpenapiClientConfig, PROVIDER_GENERATOR_HEADER_COMMENT, type Parameter, type PathInfo, type RequestBody, SERVICE_GENERATOR_HEADER_COMMENT, SERVICE_INDEX_GENERATOR_HEADER_COMMENT, type SwaggerDefinition, SwaggerParser, type SwaggerResponse, type SwaggerSpec, TYPE_GENERATOR_HEADER_COMMENT, type TypeSchema, camelCase, collectUsedTypes, escapeString, extractPaths, generateFromConfig, generateParseRequestTypeParams, getBasePathTokenName, getClientContextTokenName, getRequestBodyType, getResponseType, getResponseTypeFromResponse, getTypeScriptType, hasDuplicateFunctionNames, inferResponseTypeFromContentType, isDataTypeInterface, isPrimitiveType, kebabCase, nullableType, pascalCase, type placeHolder, validateInput };
281
+ export { BASE_INTERCEPTOR_HEADER_COMMENT, type EnumValueObject, type GeneratorConfig, type GetMethodGenerationContext, HTTP_RESOURCE_GENERATOR_HEADER_COMMENT, type IPluginGenerator, type IPluginGeneratorClass, MAIN_INDEX_GENERATOR_HEADER_COMMENT, type MethodGenerationContext, type NgOpenapiClientConfig, PROVIDER_GENERATOR_HEADER_COMMENT, type Parameter, type PathInfo, type RequestBody, SERVICE_GENERATOR_HEADER_COMMENT, SERVICE_INDEX_GENERATOR_HEADER_COMMENT, type SwaggerDefinition, SwaggerParser, type SwaggerResponse, type SwaggerSpec, TYPE_GENERATOR_HEADER_COMMENT, type TypeSchema, camelCase, collectUsedTypes, escapeString, extractPaths, generateFromConfig, generateParseRequestTypeParams, getBasePathTokenName, getClientContextTokenName, getRequestBodyType, getResponseType, getResponseTypeFromResponse, getTypeScriptType, hasDuplicateFunctionNames, inferResponseTypeFromContentType, isDataTypeInterface, isPrimitiveType, kebabCase, nullableType, pascalCase, type placeHolder, screamingSnakeCase, validateInput };
package/index.js CHANGED
@@ -100,6 +100,7 @@ __export(index_exports, {
100
100
  kebabCase: () => kebabCase,
101
101
  nullableType: () => nullableType,
102
102
  pascalCase: () => pascalCase,
103
+ screamingSnakeCase: () => screamingSnakeCase,
103
104
  validateInput: () => validateInput
104
105
  });
105
106
  module.exports = __toCommonJS(index_exports);
@@ -107,9 +108,6 @@ module.exports = __toCommonJS(index_exports);
107
108
  // src/lib/core/generator.ts
108
109
  var import_ts_morph7 = require("ts-morph");
109
110
 
110
- // src/lib/generators/type/type.generator.ts
111
- var import_ts_morph = require("ts-morph");
112
-
113
111
  // ../shared/src/utils/string.utils.ts
114
112
  function camelCase(str) {
115
113
  return str.replace(/[-_\s]+(.)?/g, (_, char) => char ? char.toUpperCase() : "").replace(/^./, (char) => char.toLowerCase());
@@ -123,6 +121,10 @@ function pascalCase(str) {
123
121
  return str.replace(/[-_\s]+(.)?/g, (_, char) => char ? char.toUpperCase() : "").replace(/^./, (char) => char.toUpperCase());
124
122
  }
125
123
  __name(pascalCase, "pascalCase");
124
+ function screamingSnakeCase(str) {
125
+ return str.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[-\s]+/g, "_").toUpperCase();
126
+ }
127
+ __name(screamingSnakeCase, "screamingSnakeCase");
126
128
 
127
129
  // ../shared/src/utils/type.utils.ts
128
130
  function getTypeScriptType(schemaOrType, config, formatOrNullable, isNullable, context = "type") {
@@ -176,8 +178,7 @@ function getTypeScriptType(schemaOrType, config, formatOrNullable, isNullable, c
176
178
  case "null":
177
179
  return "null";
178
180
  default:
179
- console.warn(`Unknown swagger type: ${schema.type}`);
180
- return nullableType("any", nullable);
181
+ return nullableType("unknown", nullable);
181
182
  }
182
183
  }
183
184
  __name(getTypeScriptType, "getTypeScriptType");
@@ -719,6 +720,7 @@ __name(_SwaggerParser, "SwaggerParser");
719
720
  var SwaggerParser = _SwaggerParser;
720
721
 
721
722
  // src/lib/generators/type/type.generator.ts
723
+ var import_ts_morph = require("ts-morph");
722
724
  var _TypeGenerator = class _TypeGenerator {
723
725
  constructor(parser, project, config, outputRoot) {
724
726
  __publicField(this, "project");
@@ -785,30 +787,76 @@ var _TypeGenerator = class _TypeGenerator {
785
787
  collectEnumStructure(name, definition) {
786
788
  var _a;
787
789
  if (!((_a = definition.enum) == null ? void 0 : _a.length)) return;
790
+ const docs = !this.config.options.generateEnumBasedOnDescription && definition.description ? [
791
+ definition.description
792
+ ] : void 0;
793
+ if (this.config.options.enumStyle === "enum") {
794
+ const statement = this.buildEnumAsEnum(name, definition, docs);
795
+ this.statements.push(...statement);
796
+ } else {
797
+ const statement = this.buildEnumAsUnion(name, definition, docs);
798
+ this.statements.push(...statement);
799
+ }
800
+ }
801
+ buildEnumAsEnum(name, definition, docs) {
802
+ var _a;
803
+ if (!((_a = definition.enum) == null ? void 0 : _a.length)) throw Error("Enum definition has no values");
804
+ const statements = [];
788
805
  const isStringEnum = definition.enum.some((value) => typeof value === "string");
789
806
  if (isStringEnum) {
790
- const unionType = definition.enum.map((value) => typeof value === "string" ? `'${this.escapeString(value)}'` : String(value)).join(" | ");
791
- this.statements.push({
792
- kind: import_ts_morph.StructureKind.TypeAlias,
807
+ const members = definition.enum.map((value) => ({
808
+ name: this.toEnumKey(value),
809
+ value: `${String(value)}`
810
+ }));
811
+ statements.push({
812
+ kind: import_ts_morph.StructureKind.Enum,
793
813
  name,
794
- type: unionType,
795
814
  isExported: true,
796
- docs: definition.description ? [
797
- definition.description
798
- ] : void 0
815
+ docs,
816
+ members
799
817
  });
800
818
  } else {
801
819
  const members = this.buildEnumMembers(definition);
802
- this.statements.push({
820
+ statements.push({
803
821
  kind: import_ts_morph.StructureKind.Enum,
804
822
  name,
805
823
  isExported: true,
806
- docs: definition.description ? [
807
- definition.description
808
- ] : void 0,
824
+ docs,
809
825
  members
810
826
  });
811
827
  }
828
+ return statements;
829
+ }
830
+ buildEnumAsUnion(name, definition, docs) {
831
+ var _a;
832
+ if (!((_a = definition.enum) == null ? void 0 : _a.length)) throw Error("Enum definition has no values");
833
+ const statements = [];
834
+ const objectProperties = [];
835
+ const unionType = definition.enum.map((value) => {
836
+ const key = this.toEnumKey(value);
837
+ const val = typeof value === "string" ? `'${this.escapeString(value)}'` : isNaN(value) ? `'${value}'` : `${value}`;
838
+ objectProperties.push(`${key}: ${val} as ${name}`);
839
+ return val;
840
+ }).join(" | ");
841
+ statements.push({
842
+ kind: import_ts_morph.StructureKind.TypeAlias,
843
+ name,
844
+ type: unionType,
845
+ isExported: true,
846
+ docs
847
+ });
848
+ statements.push({
849
+ kind: import_ts_morph.StructureKind.VariableStatement,
850
+ declarationKind: import_ts_morph.VariableDeclarationKind.Const,
851
+ isExported: true,
852
+ declarations: [
853
+ {
854
+ name,
855
+ initializer: `{ ${objectProperties.join(",\n")} }`
856
+ }
857
+ ]
858
+ });
859
+ return statements;
812
860
  }
813
861
  buildEnumMembers(definition) {
814
862
  var _a;
@@ -1100,7 +1148,10 @@ var _TypeGenerator = class _TypeGenerator {
1100
1148
  return name;
1101
1149
  }
1102
1150
  toEnumKey(value) {
1103
- return value.toString().replace(/[^a-zA-Z0-9]/g, "_").replace(/^([0-9])/, "_$1").toUpperCase();
1151
+ const str = value.toString();
1152
+ const hasLeadingMinus = str.startsWith("-");
1153
+ const snakeCased = screamingSnakeCase(str);
1154
+ return hasLeadingMinus ? snakeCased.replace("_", "_n") : snakeCased.replace(/^([0-9])/, "_$1");
1104
1155
  }
1105
1156
  getArrayItemType(items) {
1106
1157
  if (Array.isArray(items)) {
@@ -2913,6 +2964,7 @@ __name(generateFromConfig, "generateFromConfig");
2913
2964
  kebabCase,
2914
2965
  nullableType,
2915
2966
  pascalCase,
2967
+ screamingSnakeCase,
2916
2968
  validateInput
2917
2969
  });
2918
2970
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ng-openapi",
3
- "version": "0.1.14",
3
+ "version": "0.1.15-pr-25-union-support-1044cca.0",
4
4
  "description": "Generate Angular services and TypeScript types from OpenAPI/Swagger specifications",
5
5
  "keywords": [
6
6
  "ng-openapi",