@cerios/openapi-to-zod 0.5.3 → 0.6.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.
- package/README.md +7 -16
- package/dist/cli.js +56 -325
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +56 -325
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +1 -77
- package/dist/index.d.ts +1 -77
- package/dist/index.js +51 -312
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +51 -312
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/cli.mjs
CHANGED
|
@@ -5053,34 +5053,11 @@ var init_name_utils = __esm({
|
|
|
5053
5053
|
|
|
5054
5054
|
// src/generators/enum-generator.ts
|
|
5055
5055
|
function generateEnum(name, values, options) {
|
|
5056
|
-
const enumName = name.endsWith("EnumOptions") ? name.replace("EnumOptions", "Enum") : `${name}Enum`;
|
|
5057
5056
|
const schemaName = `${toCamelCase(name, options)}Schema`;
|
|
5058
|
-
if (options.enumType === "typescript") {
|
|
5059
|
-
const usedKeys = /* @__PURE__ */ new Set();
|
|
5060
|
-
const enumEntries = values.map((value) => {
|
|
5061
|
-
let key = toPascalCase(value);
|
|
5062
|
-
if (usedKeys.has(key)) {
|
|
5063
|
-
let counter = 2;
|
|
5064
|
-
while (usedKeys.has(`${key}${counter}`)) {
|
|
5065
|
-
counter++;
|
|
5066
|
-
}
|
|
5067
|
-
key = `${key}${counter}`;
|
|
5068
|
-
}
|
|
5069
|
-
usedKeys.add(key);
|
|
5070
|
-
const stringValue = typeof value === "string" ? `"${value}"` : value;
|
|
5071
|
-
return ` ${key} = ${stringValue},`;
|
|
5072
|
-
}).join("\n");
|
|
5073
|
-
const enumCode = `export enum ${enumName} {
|
|
5074
|
-
${enumEntries}
|
|
5075
|
-
}`;
|
|
5076
|
-
const schemaCode2 = `export const ${schemaName} = z.nativeEnum(${enumName});`;
|
|
5077
|
-
const typeCode2 = `export type ${name} = z.infer<typeof ${schemaName}>;`;
|
|
5078
|
-
return { enumCode, schemaCode: schemaCode2, typeCode: typeCode2 };
|
|
5079
|
-
}
|
|
5080
5057
|
const enumValues = values.map((v) => `"${v}"`).join(", ");
|
|
5081
5058
|
const schemaCode = `export const ${schemaName} = z.enum([${enumValues}]);`;
|
|
5082
5059
|
const typeCode = `export type ${name} = z.infer<typeof ${schemaName}>;`;
|
|
5083
|
-
return {
|
|
5060
|
+
return { schemaCode, typeCode };
|
|
5084
5061
|
}
|
|
5085
5062
|
var init_enum_generator = __esm({
|
|
5086
5063
|
"src/generators/enum-generator.ts"() {
|
|
@@ -6450,12 +6427,9 @@ var init_openapi_generator = __esm({
|
|
|
6450
6427
|
constructor(options) {
|
|
6451
6428
|
this.schemas = /* @__PURE__ */ new Map();
|
|
6452
6429
|
this.types = /* @__PURE__ */ new Map();
|
|
6453
|
-
this.enums = /* @__PURE__ */ new Map();
|
|
6454
|
-
this.nativeEnums = /* @__PURE__ */ new Map();
|
|
6455
6430
|
this.schemaDependencies = /* @__PURE__ */ new Map();
|
|
6456
6431
|
this.schemaUsageMap = /* @__PURE__ */ new Map();
|
|
6457
|
-
this.
|
|
6458
|
-
this.needsZodImport = false;
|
|
6432
|
+
this.needsZodImport = true;
|
|
6459
6433
|
this.filterStats = createFilterStatistics();
|
|
6460
6434
|
var _a, _b, _c;
|
|
6461
6435
|
if (!options.input) {
|
|
@@ -6466,13 +6440,11 @@ var init_openapi_generator = __esm({
|
|
|
6466
6440
|
input: options.input,
|
|
6467
6441
|
output: options.output,
|
|
6468
6442
|
includeDescriptions: (_a = options.includeDescriptions) != null ? _a : true,
|
|
6469
|
-
enumType: options.enumType || "zod",
|
|
6470
6443
|
useDescribe: (_b = options.useDescribe) != null ? _b : false,
|
|
6471
6444
|
schemaType: options.schemaType || "all",
|
|
6472
6445
|
prefix: options.prefix,
|
|
6473
6446
|
suffix: options.suffix,
|
|
6474
6447
|
showStats: (_c = options.showStats) != null ? _c : true,
|
|
6475
|
-
nativeEnumType: options.nativeEnumType || "union",
|
|
6476
6448
|
request: options.request,
|
|
6477
6449
|
response: options.response,
|
|
6478
6450
|
operationFilters: options.operationFilters
|
|
@@ -6532,7 +6504,6 @@ var init_openapi_generator = __esm({
|
|
|
6532
6504
|
this.requestOptions = this.resolveOptionsForContext("request");
|
|
6533
6505
|
this.responseOptions = this.resolveOptionsForContext("response");
|
|
6534
6506
|
this.analyzeSchemaUsage();
|
|
6535
|
-
this.determineSchemaTypeModes();
|
|
6536
6507
|
this.propertyGenerator = new PropertyGenerator({
|
|
6537
6508
|
spec: this.spec,
|
|
6538
6509
|
schemaDependencies: this.schemaDependencies,
|
|
@@ -6540,8 +6511,6 @@ var init_openapi_generator = __esm({
|
|
|
6540
6511
|
mode: this.requestOptions.mode,
|
|
6541
6512
|
includeDescriptions: this.requestOptions.includeDescriptions,
|
|
6542
6513
|
useDescribe: this.requestOptions.useDescribe,
|
|
6543
|
-
typeMode: this.requestOptions.typeMode,
|
|
6544
|
-
nativeEnumType: this.requestOptions.nativeEnumType,
|
|
6545
6514
|
namingOptions: {
|
|
6546
6515
|
prefix: this.options.prefix,
|
|
6547
6516
|
suffix: this.options.suffix
|
|
@@ -6557,25 +6526,6 @@ var init_openapi_generator = __esm({
|
|
|
6557
6526
|
if (!((_a = this.spec.components) == null ? void 0 : _a.schemas)) {
|
|
6558
6527
|
throw new SpecValidationError("No schemas found in OpenAPI spec", { filePath: this.options.input });
|
|
6559
6528
|
}
|
|
6560
|
-
for (const [name, schema] of Object.entries(this.spec.components.schemas)) {
|
|
6561
|
-
if (schema.enum) {
|
|
6562
|
-
const context = this.schemaUsageMap.get(name);
|
|
6563
|
-
const resolvedOptions = context === "response" ? this.responseOptions : this.requestOptions;
|
|
6564
|
-
if (resolvedOptions.enumType === "typescript") {
|
|
6565
|
-
this.generateNativeEnum(name, schema);
|
|
6566
|
-
} else {
|
|
6567
|
-
const { enumCode } = generateEnum(name, schema.enum, {
|
|
6568
|
-
enumType: "zod",
|
|
6569
|
-
prefix: this.options.prefix,
|
|
6570
|
-
suffix: this.options.suffix
|
|
6571
|
-
});
|
|
6572
|
-
if (enumCode) {
|
|
6573
|
-
this.enums.set(name, enumCode);
|
|
6574
|
-
this.needsZodImport = true;
|
|
6575
|
-
}
|
|
6576
|
-
}
|
|
6577
|
-
}
|
|
6578
|
-
}
|
|
6579
6529
|
for (const [name, schema] of Object.entries(this.spec.components.schemas)) {
|
|
6580
6530
|
this.generateComponentSchema(name, schema);
|
|
6581
6531
|
}
|
|
@@ -6592,22 +6542,11 @@ var init_openapi_generator = __esm({
|
|
|
6592
6542
|
output.push('import { z } from "zod";');
|
|
6593
6543
|
output.push("");
|
|
6594
6544
|
}
|
|
6595
|
-
if (this.nativeEnums.size > 0) {
|
|
6596
|
-
output.push("// Native Enums");
|
|
6597
|
-
for (const enumCode of this.nativeEnums.values()) {
|
|
6598
|
-
output.push(enumCode);
|
|
6599
|
-
output.push("");
|
|
6600
|
-
}
|
|
6601
|
-
}
|
|
6602
6545
|
output.push("// Schemas and Types");
|
|
6603
6546
|
for (const name of orderedSchemaNames) {
|
|
6604
|
-
const enumCode = this.enums.get(name);
|
|
6605
6547
|
const schemaCode = this.schemas.get(name);
|
|
6606
6548
|
const typeCode = this.types.get(name);
|
|
6607
|
-
if (
|
|
6608
|
-
output.push(enumCode);
|
|
6609
|
-
output.push("");
|
|
6610
|
-
} else if (schemaCode) {
|
|
6549
|
+
if (schemaCode) {
|
|
6611
6550
|
output.push(schemaCode);
|
|
6612
6551
|
if (!schemaCode.includes(`export type ${name}`)) {
|
|
6613
6552
|
const schemaName = `${toCamelCase(name, { prefix: this.options.prefix, suffix: this.options.suffix })}Schema`;
|
|
@@ -6649,21 +6588,14 @@ var init_openapi_generator = __esm({
|
|
|
6649
6588
|
/**
|
|
6650
6589
|
* Resolve options for a specific context (request or response)
|
|
6651
6590
|
* Nested options silently override root-level options
|
|
6652
|
-
* Response schemas always use 'inferred' mode (Zod schemas)
|
|
6653
6591
|
*/
|
|
6654
6592
|
resolveOptionsForContext(context) {
|
|
6655
|
-
var _a, _b, _c, _d, _e, _f
|
|
6593
|
+
var _a, _b, _c, _d, _e, _f;
|
|
6656
6594
|
const contextOptions = context === "request" ? this.options.request : this.options.response;
|
|
6657
|
-
const nativeEnumType = context === "request" ? (_c = (_b = (_a = this.options.request) == null ? void 0 : _a.nativeEnumType) != null ? _b : this.options.nativeEnumType) != null ? _c : "union" : (_d = this.options.nativeEnumType) != null ? _d : "union";
|
|
6658
6595
|
return {
|
|
6659
|
-
mode: (
|
|
6660
|
-
|
|
6661
|
-
|
|
6662
|
-
includeDescriptions: (_l = (_k = contextOptions == null ? void 0 : contextOptions.includeDescriptions) != null ? _k : this.options.includeDescriptions) != null ? _l : true,
|
|
6663
|
-
// Response schemas always use 'inferred' mode (Zod schemas are required)
|
|
6664
|
-
// Request schemas can optionally use 'native' mode
|
|
6665
|
-
typeMode: context === "response" ? "inferred" : (_n = (_m = this.options.request) == null ? void 0 : _m.typeMode) != null ? _n : "inferred",
|
|
6666
|
-
nativeEnumType
|
|
6596
|
+
mode: (_b = (_a = contextOptions == null ? void 0 : contextOptions.mode) != null ? _a : this.options.mode) != null ? _b : "normal",
|
|
6597
|
+
useDescribe: (_d = (_c = contextOptions == null ? void 0 : contextOptions.useDescribe) != null ? _c : this.options.useDescribe) != null ? _d : false,
|
|
6598
|
+
includeDescriptions: (_f = (_e = contextOptions == null ? void 0 : contextOptions.includeDescriptions) != null ? _e : this.options.includeDescriptions) != null ? _f : true
|
|
6667
6599
|
};
|
|
6668
6600
|
}
|
|
6669
6601
|
/**
|
|
@@ -6854,28 +6786,6 @@ var init_openapi_generator = __esm({
|
|
|
6854
6786
|
detectCycle(name);
|
|
6855
6787
|
}
|
|
6856
6788
|
}
|
|
6857
|
-
/**
|
|
6858
|
-
* Determine the typeMode for each schema based on its usage context
|
|
6859
|
-
* Response schemas always use 'inferred' mode
|
|
6860
|
-
*/
|
|
6861
|
-
determineSchemaTypeModes() {
|
|
6862
|
-
var _a;
|
|
6863
|
-
for (const [name] of Object.entries(((_a = this.spec.components) == null ? void 0 : _a.schemas) || {})) {
|
|
6864
|
-
const context = this.schemaUsageMap.get(name);
|
|
6865
|
-
if (context === "request") {
|
|
6866
|
-
this.schemaTypeModeMap.set(name, this.requestOptions.typeMode);
|
|
6867
|
-
} else if (context === "response") {
|
|
6868
|
-
this.schemaTypeModeMap.set(name, "inferred");
|
|
6869
|
-
} else if (context === "both") {
|
|
6870
|
-
this.schemaTypeModeMap.set(name, "inferred");
|
|
6871
|
-
} else {
|
|
6872
|
-
this.schemaTypeModeMap.set(name, "inferred");
|
|
6873
|
-
}
|
|
6874
|
-
if (this.schemaTypeModeMap.get(name) === "inferred") {
|
|
6875
|
-
this.needsZodImport = true;
|
|
6876
|
-
}
|
|
6877
|
-
}
|
|
6878
|
-
}
|
|
6879
6789
|
/**
|
|
6880
6790
|
* Validate the OpenAPI specification
|
|
6881
6791
|
*/
|
|
@@ -6951,80 +6861,54 @@ var init_openapi_generator = __esm({
|
|
|
6951
6861
|
if (!this.schemaDependencies.has(name)) {
|
|
6952
6862
|
this.schemaDependencies.set(name, /* @__PURE__ */ new Set());
|
|
6953
6863
|
}
|
|
6954
|
-
const typeMode = this.schemaTypeModeMap.get(name) || "inferred";
|
|
6955
6864
|
const context = this.schemaUsageMap.get(name);
|
|
6956
6865
|
const resolvedOptions = context === "response" ? this.responseOptions : this.requestOptions;
|
|
6957
6866
|
if (schema.enum) {
|
|
6958
|
-
const
|
|
6959
|
-
|
|
6960
|
-
|
|
6961
|
-
|
|
6962
|
-
|
|
6963
|
-
|
|
6964
|
-
});
|
|
6965
|
-
const enumSchemaCode = `${jsdoc}${schemaCode}
|
|
6966
|
-
${typeCode}`;
|
|
6967
|
-
this.schemas.set(name, enumSchemaCode);
|
|
6968
|
-
} else {
|
|
6969
|
-
const { enumCode, schemaCode, typeCode } = generateEnum(name, schema.enum, {
|
|
6970
|
-
enumType: "zod",
|
|
6971
|
-
prefix: this.options.prefix,
|
|
6972
|
-
suffix: this.options.suffix
|
|
6973
|
-
});
|
|
6974
|
-
if (enumCode) {
|
|
6975
|
-
this.enums.set(name, enumCode);
|
|
6976
|
-
}
|
|
6977
|
-
const enumSchemaCode = `${jsdoc}${schemaCode}
|
|
6867
|
+
const jsdoc2 = generateJSDoc(schema, name, { includeDescriptions: resolvedOptions.includeDescriptions });
|
|
6868
|
+
const { schemaCode, typeCode } = generateEnum(name, schema.enum, {
|
|
6869
|
+
prefix: this.options.prefix,
|
|
6870
|
+
suffix: this.options.suffix
|
|
6871
|
+
});
|
|
6872
|
+
const enumSchemaCode = `${jsdoc2}${schemaCode}
|
|
6978
6873
|
${typeCode}`;
|
|
6979
|
-
|
|
6980
|
-
}
|
|
6874
|
+
this.schemas.set(name, enumSchemaCode);
|
|
6981
6875
|
return;
|
|
6982
6876
|
}
|
|
6983
|
-
|
|
6984
|
-
|
|
6985
|
-
|
|
6986
|
-
const
|
|
6987
|
-
|
|
6988
|
-
|
|
6989
|
-
|
|
6990
|
-
|
|
6991
|
-
|
|
6992
|
-
|
|
6993
|
-
|
|
6994
|
-
|
|
6877
|
+
const schemaName = `${toCamelCase(name, { prefix: this.options.prefix, suffix: this.options.suffix })}Schema`;
|
|
6878
|
+
const jsdoc = generateJSDoc(schema, name, { includeDescriptions: resolvedOptions.includeDescriptions });
|
|
6879
|
+
if (schema.allOf && schema.allOf.length === 1 && schema.allOf[0].$ref) {
|
|
6880
|
+
const refName = resolveRef(schema.allOf[0].$ref);
|
|
6881
|
+
(_a = this.schemaDependencies.get(name)) == null ? void 0 : _a.add(refName);
|
|
6882
|
+
}
|
|
6883
|
+
this.propertyGenerator = new PropertyGenerator({
|
|
6884
|
+
spec: this.spec,
|
|
6885
|
+
schemaDependencies: this.schemaDependencies,
|
|
6886
|
+
schemaType: this.options.schemaType || "all",
|
|
6887
|
+
mode: resolvedOptions.mode,
|
|
6888
|
+
includeDescriptions: resolvedOptions.includeDescriptions,
|
|
6889
|
+
useDescribe: resolvedOptions.useDescribe,
|
|
6890
|
+
namingOptions: {
|
|
6891
|
+
prefix: this.options.prefix,
|
|
6892
|
+
suffix: this.options.suffix
|
|
6995
6893
|
}
|
|
6996
|
-
|
|
6997
|
-
|
|
6998
|
-
|
|
6999
|
-
|
|
7000
|
-
|
|
7001
|
-
|
|
7002
|
-
|
|
7003
|
-
|
|
7004
|
-
|
|
7005
|
-
|
|
7006
|
-
|
|
7007
|
-
|
|
7008
|
-
|
|
7009
|
-
});
|
|
7010
|
-
const isAlias = !!(schema.$ref && !schema.properties && !schema.allOf && !schema.oneOf && !schema.anyOf);
|
|
7011
|
-
const zodSchema = this.propertyGenerator.generatePropertySchema(schema, name, isAlias);
|
|
7012
|
-
const zodSchemaCode = `${jsdoc}export const ${schemaName} = ${zodSchema};`;
|
|
7013
|
-
if (zodSchema.includes("z.discriminatedUnion(")) {
|
|
7014
|
-
const match = zodSchema.match(/z\.discriminatedUnion\([^,]+,\s*\[([^\]]+)\]/);
|
|
7015
|
-
if (match) {
|
|
7016
|
-
const refs = match[1].split(",").map((ref) => ref.trim());
|
|
7017
|
-
for (const ref of refs) {
|
|
7018
|
-
const depMatch = ref.match(/^([a-z][a-zA-Z0-9]*?)Schema$/);
|
|
7019
|
-
if (depMatch) {
|
|
7020
|
-
const depName = depMatch[1].charAt(0).toUpperCase() + depMatch[1].slice(1);
|
|
7021
|
-
(_b = this.schemaDependencies.get(name)) == null ? void 0 : _b.add(depName);
|
|
7022
|
-
}
|
|
6894
|
+
});
|
|
6895
|
+
const isAlias = !!(schema.$ref && !schema.properties && !schema.allOf && !schema.oneOf && !schema.anyOf);
|
|
6896
|
+
const zodSchema = this.propertyGenerator.generatePropertySchema(schema, name, isAlias);
|
|
6897
|
+
const zodSchemaCode = `${jsdoc}export const ${schemaName} = ${zodSchema};`;
|
|
6898
|
+
if (zodSchema.includes("z.discriminatedUnion(")) {
|
|
6899
|
+
const match = zodSchema.match(/z\.discriminatedUnion\([^,]+,\s*\[([^\]]+)\]/);
|
|
6900
|
+
if (match) {
|
|
6901
|
+
const refs = match[1].split(",").map((ref) => ref.trim());
|
|
6902
|
+
for (const ref of refs) {
|
|
6903
|
+
const depMatch = ref.match(/^([a-z][a-zA-Z0-9]*?)Schema$/);
|
|
6904
|
+
if (depMatch) {
|
|
6905
|
+
const depName = depMatch[1].charAt(0).toUpperCase() + depMatch[1].slice(1);
|
|
6906
|
+
(_b = this.schemaDependencies.get(name)) == null ? void 0 : _b.add(depName);
|
|
7023
6907
|
}
|
|
7024
6908
|
}
|
|
7025
6909
|
}
|
|
7026
|
-
this.schemas.set(name, zodSchemaCode);
|
|
7027
6910
|
}
|
|
6911
|
+
this.schemas.set(name, zodSchemaCode);
|
|
7028
6912
|
}
|
|
7029
6913
|
/**
|
|
7030
6914
|
* Generate query parameter schemas for each operation
|
|
@@ -7230,151 +7114,11 @@ ${propsCode}
|
|
|
7230
7114
|
}
|
|
7231
7115
|
return "z.unknown()";
|
|
7232
7116
|
}
|
|
7233
|
-
|
|
7234
|
-
|
|
7235
|
-
|
|
7236
|
-
|
|
7237
|
-
|
|
7238
|
-
const context = this.schemaUsageMap.get(name);
|
|
7239
|
-
const resolvedOptions = context === "response" ? this.responseOptions : this.requestOptions;
|
|
7240
|
-
const jsdoc = generateJSDoc(schema, name, { includeDescriptions: resolvedOptions.includeDescriptions });
|
|
7241
|
-
if (resolvedOptions.nativeEnumType === "enum") {
|
|
7242
|
-
const enumName = `${name}Enum`;
|
|
7243
|
-
const members = schema.enum.map((value) => {
|
|
7244
|
-
const key = typeof value === "string" ? this.toEnumKey(value) : `N${value}`;
|
|
7245
|
-
const val = typeof value === "string" ? `"${value}"` : value;
|
|
7246
|
-
return ` ${key} = ${val}`;
|
|
7247
|
-
}).join(",\n");
|
|
7248
|
-
const enumCode = `${jsdoc}export enum ${enumName} {
|
|
7249
|
-
${members}
|
|
7250
|
-
}`;
|
|
7251
|
-
this.nativeEnums.set(name, enumCode);
|
|
7252
|
-
const typeCode = `export type ${name} = ${enumName};`;
|
|
7253
|
-
this.types.set(name, typeCode);
|
|
7254
|
-
} else {
|
|
7255
|
-
const unionType = schema.enum.map((v) => typeof v === "string" ? `"${v}"` : v).join(" | ");
|
|
7256
|
-
const typeCode = `${jsdoc}export type ${name} = ${unionType};`;
|
|
7257
|
-
this.types.set(name, typeCode);
|
|
7258
|
-
}
|
|
7259
|
-
}
|
|
7260
|
-
/**
|
|
7261
|
-
* Convert string to valid enum key
|
|
7262
|
-
*/
|
|
7263
|
-
toEnumKey(value) {
|
|
7264
|
-
const cleaned = value.replace(/[^a-zA-Z0-9]/g, "_");
|
|
7265
|
-
const pascalCase = cleaned.split("_").map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("");
|
|
7266
|
-
return pascalCase || "Value";
|
|
7267
|
-
}
|
|
7268
|
-
/**
|
|
7269
|
-
* Add constraint annotations to JSDoc for native types
|
|
7270
|
-
*/
|
|
7271
|
-
addConstraintsToJSDoc(jsdoc, schema, includeDescriptions) {
|
|
7272
|
-
if (!includeDescriptions) return jsdoc;
|
|
7273
|
-
const constraints = [];
|
|
7274
|
-
if (schema.minLength !== void 0) constraints.push(`@minLength ${schema.minLength}`);
|
|
7275
|
-
if (schema.maxLength !== void 0) constraints.push(`@maxLength ${schema.maxLength}`);
|
|
7276
|
-
if (schema.pattern) constraints.push(`@pattern ${schema.pattern}`);
|
|
7277
|
-
if (schema.minimum !== void 0) constraints.push(`@minimum ${schema.minimum}`);
|
|
7278
|
-
if (schema.maximum !== void 0) constraints.push(`@maximum ${schema.maximum}`);
|
|
7279
|
-
if (schema.minItems !== void 0) constraints.push(`@minItems ${schema.minItems}`);
|
|
7280
|
-
if (schema.maxItems !== void 0) constraints.push(`@maxItems ${schema.maxItems}`);
|
|
7281
|
-
if (schema.minProperties !== void 0) constraints.push(`@minProperties ${schema.minProperties}`);
|
|
7282
|
-
if (schema.maxProperties !== void 0) constraints.push(`@maxProperties ${schema.maxProperties}`);
|
|
7283
|
-
if (schema.multipleOf !== void 0) constraints.push(`@multipleOf ${schema.multipleOf}`);
|
|
7284
|
-
if (schema.format) constraints.push(`@format ${schema.format}`);
|
|
7285
|
-
if (constraints.length === 0) return jsdoc;
|
|
7286
|
-
if (jsdoc) {
|
|
7287
|
-
const lines = jsdoc.trim().split("\n");
|
|
7288
|
-
if (lines[0] === "/**" && lines[lines.length - 1] === " */") {
|
|
7289
|
-
const newLines = [...lines.slice(0, -1), ...constraints.map((c) => ` * ${c}`), " */\n"];
|
|
7290
|
-
return newLines.join("\n");
|
|
7291
|
-
}
|
|
7292
|
-
const content = jsdoc.replace("/** ", "").replace(" */\n", "");
|
|
7293
|
-
return `/**
|
|
7294
|
-
* ${content}
|
|
7295
|
-
${constraints.map((c) => ` * ${c}`).join("\n")}
|
|
7296
|
-
*/
|
|
7297
|
-
`;
|
|
7298
|
-
}
|
|
7299
|
-
return `/**
|
|
7300
|
-
${constraints.map((c) => ` * ${c}`).join("\n")}
|
|
7301
|
-
*/
|
|
7302
|
-
`;
|
|
7303
|
-
}
|
|
7304
|
-
/**
|
|
7305
|
-
* Generate native TypeScript type definition from OpenAPI schema
|
|
7306
|
-
*/
|
|
7307
|
-
generateNativeTypeDefinition(schema, _schemaName) {
|
|
7308
|
-
if (schema.$ref) {
|
|
7309
|
-
return resolveRef(schema.$ref);
|
|
7310
|
-
}
|
|
7311
|
-
if (schema.const !== void 0) {
|
|
7312
|
-
return typeof schema.const === "string" ? `"${schema.const}"` : String(schema.const);
|
|
7313
|
-
}
|
|
7314
|
-
const isNullable2 = schema.nullable || Array.isArray(schema.type) && schema.type.includes("null");
|
|
7315
|
-
const wrapNullable2 = (type) => isNullable2 ? `(${type}) | null` : type;
|
|
7316
|
-
const primaryType = Array.isArray(schema.type) ? schema.type.find((t) => t !== "null") : schema.type;
|
|
7317
|
-
switch (primaryType) {
|
|
7318
|
-
case "string":
|
|
7319
|
-
return wrapNullable2("string");
|
|
7320
|
-
case "number":
|
|
7321
|
-
case "integer":
|
|
7322
|
-
return wrapNullable2("number");
|
|
7323
|
-
case "boolean":
|
|
7324
|
-
return wrapNullable2("boolean");
|
|
7325
|
-
case "array":
|
|
7326
|
-
if (schema.items) {
|
|
7327
|
-
const itemType = this.generateNativeTypeDefinition(schema.items);
|
|
7328
|
-
return wrapNullable2(`${itemType}[]`);
|
|
7329
|
-
}
|
|
7330
|
-
return wrapNullable2("unknown[]");
|
|
7331
|
-
case "object":
|
|
7332
|
-
return wrapNullable2(this.generateObjectType(schema));
|
|
7333
|
-
default:
|
|
7334
|
-
if (schema.allOf) {
|
|
7335
|
-
const types = schema.allOf.map((s) => this.generateNativeTypeDefinition(s));
|
|
7336
|
-
return wrapNullable2(types.join(" & "));
|
|
7337
|
-
}
|
|
7338
|
-
if (schema.oneOf || schema.anyOf) {
|
|
7339
|
-
const schemas = schema.oneOf || schema.anyOf || [];
|
|
7340
|
-
const types = schemas.map((s) => this.generateNativeTypeDefinition(s));
|
|
7341
|
-
return wrapNullable2(types.join(" | "));
|
|
7342
|
-
}
|
|
7343
|
-
return wrapNullable2("unknown");
|
|
7344
|
-
}
|
|
7345
|
-
}
|
|
7346
|
-
/**
|
|
7347
|
-
* Generate TypeScript object type definition
|
|
7348
|
-
*/
|
|
7349
|
-
generateObjectType(schema) {
|
|
7350
|
-
if (!schema.properties || Object.keys(schema.properties).length === 0) {
|
|
7351
|
-
return "Record<string, unknown>";
|
|
7352
|
-
}
|
|
7353
|
-
const context = this.schemaUsageMap.get(schema.$ref ? resolveRef(schema.$ref) : "");
|
|
7354
|
-
const resolvedOptions = context === "response" ? this.responseOptions : this.requestOptions;
|
|
7355
|
-
const required = new Set(schema.required || []);
|
|
7356
|
-
const props = [];
|
|
7357
|
-
for (const [propName, propSchema] of Object.entries(schema.properties)) {
|
|
7358
|
-
const propType = this.generateNativeTypeDefinition(propSchema);
|
|
7359
|
-
const optional = !required.has(propName) ? "?" : "";
|
|
7360
|
-
let propJsdoc = generateJSDoc(propSchema, propName, { includeDescriptions: resolvedOptions.includeDescriptions });
|
|
7361
|
-
if (resolvedOptions.includeDescriptions && !propJsdoc) {
|
|
7362
|
-
propJsdoc = this.addConstraintsToJSDoc("", propSchema, resolvedOptions.includeDescriptions);
|
|
7363
|
-
} else if (propJsdoc && resolvedOptions.includeDescriptions) {
|
|
7364
|
-
propJsdoc = this.addConstraintsToJSDoc(propJsdoc, propSchema, resolvedOptions.includeDescriptions);
|
|
7365
|
-
}
|
|
7366
|
-
if (propJsdoc) {
|
|
7367
|
-
const cleanJsdoc = propJsdoc.trimEnd();
|
|
7368
|
-
props.push(` ${cleanJsdoc}
|
|
7369
|
-
${propName}${optional}: ${propType};`);
|
|
7370
|
-
} else {
|
|
7371
|
-
props.push(` ${propName}${optional}: ${propType};`);
|
|
7372
|
-
}
|
|
7373
|
-
}
|
|
7374
|
-
return `{
|
|
7375
|
-
${props.join("\n")}
|
|
7376
|
-
}`;
|
|
7377
|
-
}
|
|
7117
|
+
// REMOVED: generateNativeEnum method - no longer needed as we only generate Zod schemas
|
|
7118
|
+
// REMOVED: toEnumKey method - was only used by generateNativeEnum
|
|
7119
|
+
// REMOVED: addConstraintsToJSDoc method - was only used for native TypeScript types
|
|
7120
|
+
// REMOVED: generateNativeTypeDefinition method - was only used for native TypeScript types
|
|
7121
|
+
// REMOVED: generateObjectType method - was only used for native TypeScript types
|
|
7378
7122
|
/**
|
|
7379
7123
|
* Topological sort for schema dependencies
|
|
7380
7124
|
* Returns schemas in the order they should be declared
|
|
@@ -7386,9 +7130,6 @@ ${props.join("\n")}
|
|
|
7386
7130
|
const aliases = [];
|
|
7387
7131
|
const circularDeps = /* @__PURE__ */ new Set();
|
|
7388
7132
|
const codeCache = /* @__PURE__ */ new Map();
|
|
7389
|
-
for (const [name, code] of this.enums) {
|
|
7390
|
-
codeCache.set(name, code);
|
|
7391
|
-
}
|
|
7392
7133
|
for (const [name, code] of this.schemas) {
|
|
7393
7134
|
codeCache.set(name, code);
|
|
7394
7135
|
}
|
|
@@ -7413,7 +7154,7 @@ ${props.join("\n")}
|
|
|
7413
7154
|
const deps = this.schemaDependencies.get(name);
|
|
7414
7155
|
if (deps && deps.size > 0) {
|
|
7415
7156
|
for (const dep of deps) {
|
|
7416
|
-
if (this.
|
|
7157
|
+
if (this.schemas.has(dep) || this.types.has(dep)) {
|
|
7417
7158
|
visit(dep);
|
|
7418
7159
|
}
|
|
7419
7160
|
}
|
|
@@ -7424,7 +7165,7 @@ ${props.join("\n")}
|
|
|
7424
7165
|
sorted.push(name);
|
|
7425
7166
|
}
|
|
7426
7167
|
};
|
|
7427
|
-
const allNames = /* @__PURE__ */ new Set([...this.
|
|
7168
|
+
const allNames = /* @__PURE__ */ new Set([...this.schemas.keys(), ...this.types.keys()]);
|
|
7428
7169
|
for (const name of allNames) {
|
|
7429
7170
|
visit(name);
|
|
7430
7171
|
}
|
|
@@ -7442,7 +7183,6 @@ ${props.join("\n")}
|
|
|
7442
7183
|
generateStats() {
|
|
7443
7184
|
const stats = {
|
|
7444
7185
|
totalSchemas: this.schemas.size,
|
|
7445
|
-
enums: this.enums.size,
|
|
7446
7186
|
withCircularRefs: 0,
|
|
7447
7187
|
withDiscriminators: 0,
|
|
7448
7188
|
withConstraints: 0
|
|
@@ -7457,7 +7197,6 @@ ${props.join("\n")}
|
|
|
7457
7197
|
const output = [
|
|
7458
7198
|
"// Generation Statistics:",
|
|
7459
7199
|
`// Total schemas: ${stats.totalSchemas}`,
|
|
7460
|
-
`// Enums: ${stats.enums}`,
|
|
7461
7200
|
`// Circular references: ${stats.withCircularRefs}`,
|
|
7462
7201
|
`// Discriminated unions: ${stats.withDiscriminators}`,
|
|
7463
7202
|
`// With constraints: ${stats.withConstraints}`
|
|
@@ -7644,7 +7383,6 @@ function mergeConfigWithDefaults(config) {
|
|
|
7644
7383
|
// Apply defaults first
|
|
7645
7384
|
mode: defaults.mode,
|
|
7646
7385
|
includeDescriptions: defaults.includeDescriptions,
|
|
7647
|
-
enumType: defaults.enumType,
|
|
7648
7386
|
useDescribe: defaults.useDescribe,
|
|
7649
7387
|
schemaType: defaults.schemaType,
|
|
7650
7388
|
prefix: defaults.prefix,
|
|
@@ -7656,20 +7394,15 @@ function mergeConfigWithDefaults(config) {
|
|
|
7656
7394
|
return merged;
|
|
7657
7395
|
});
|
|
7658
7396
|
}
|
|
7659
|
-
var
|
|
7397
|
+
var RequestResponseOptionsSchema, OperationFiltersSchema, OpenApiGeneratorOptionsSchema, ConfigFileSchema, createTypeScriptLoader;
|
|
7660
7398
|
var init_config_loader = __esm({
|
|
7661
7399
|
"src/utils/config-loader.ts"() {
|
|
7662
7400
|
"use strict";
|
|
7663
7401
|
init_esm_shims();
|
|
7664
|
-
TypeModeSchema = z.enum(["inferred", "native"]);
|
|
7665
|
-
NativeEnumTypeSchema = z.enum(["union", "enum"]);
|
|
7666
7402
|
RequestResponseOptionsSchema = z.strictObject({
|
|
7667
7403
|
mode: z.enum(["strict", "normal", "loose"]).optional(),
|
|
7668
|
-
enumType: z.enum(["zod", "typescript"]).optional(),
|
|
7669
7404
|
useDescribe: z.boolean().optional(),
|
|
7670
|
-
includeDescriptions: z.boolean().optional()
|
|
7671
|
-
typeMode: TypeModeSchema.optional(),
|
|
7672
|
-
nativeEnumType: NativeEnumTypeSchema.optional()
|
|
7405
|
+
includeDescriptions: z.boolean().optional()
|
|
7673
7406
|
});
|
|
7674
7407
|
OperationFiltersSchema = z.strictObject({
|
|
7675
7408
|
includeTags: z.array(z.string()).optional(),
|
|
@@ -7687,13 +7420,11 @@ var init_config_loader = __esm({
|
|
|
7687
7420
|
input: z.string(),
|
|
7688
7421
|
output: z.string(),
|
|
7689
7422
|
includeDescriptions: z.boolean().optional(),
|
|
7690
|
-
enumType: z.enum(["zod", "typescript"]).optional(),
|
|
7691
7423
|
useDescribe: z.boolean().optional(),
|
|
7692
7424
|
schemaType: z.enum(["all", "request", "response"]).optional(),
|
|
7693
7425
|
prefix: z.string().optional(),
|
|
7694
7426
|
suffix: z.string().optional(),
|
|
7695
7427
|
showStats: z.boolean().optional(),
|
|
7696
|
-
nativeEnumType: NativeEnumTypeSchema.optional(),
|
|
7697
7428
|
request: RequestResponseOptionsSchema.optional(),
|
|
7698
7429
|
response: RequestResponseOptionsSchema.optional(),
|
|
7699
7430
|
name: z.string().optional(),
|
|
@@ -7703,13 +7434,11 @@ var init_config_loader = __esm({
|
|
|
7703
7434
|
defaults: z.strictObject({
|
|
7704
7435
|
mode: z.enum(["strict", "normal", "loose"]).optional(),
|
|
7705
7436
|
includeDescriptions: z.boolean().optional(),
|
|
7706
|
-
enumType: z.enum(["zod", "typescript"]).optional(),
|
|
7707
7437
|
useDescribe: z.boolean().optional(),
|
|
7708
7438
|
schemaType: z.enum(["all", "request", "response"]).optional(),
|
|
7709
7439
|
prefix: z.string().optional(),
|
|
7710
7440
|
suffix: z.string().optional(),
|
|
7711
7441
|
showStats: z.boolean().optional(),
|
|
7712
|
-
nativeEnumType: NativeEnumTypeSchema.optional(),
|
|
7713
7442
|
request: RequestResponseOptionsSchema.optional(),
|
|
7714
7443
|
response: RequestResponseOptionsSchema.optional(),
|
|
7715
7444
|
operationFilters: OperationFiltersSchema.optional()
|
|
@@ -7948,7 +7677,7 @@ Breaking Changes (v2.0):
|
|
|
7948
7677
|
{
|
|
7949
7678
|
type: "confirm",
|
|
7950
7679
|
name: "includeDefaults",
|
|
7951
|
-
message: "Include commonly-used defaults?
|
|
7680
|
+
message: "Include commonly-used recommended defaults?",
|
|
7952
7681
|
initial: true
|
|
7953
7682
|
}
|
|
7954
7683
|
]);
|
|
@@ -7969,7 +7698,9 @@ export default defineConfig({
|
|
|
7969
7698
|
defaults: {
|
|
7970
7699
|
mode: 'strict',
|
|
7971
7700
|
includeDescriptions: true,
|
|
7701
|
+
useDescribe: false,
|
|
7972
7702
|
showStats: false,
|
|
7703
|
+
schemaType: 'all',
|
|
7973
7704
|
},
|
|
7974
7705
|
specs: [
|
|
7975
7706
|
{
|