@cerios/openapi-to-zod 0.2.0 → 0.4.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 +114 -116
- package/dist/cli.js +5252 -171
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +5232 -171
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +26 -14
- package/dist/index.d.ts +26 -14
- package/dist/index.js +49 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +49 -24
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -1
package/dist/index.d.mts
CHANGED
|
@@ -63,11 +63,9 @@ type TypeMode = "inferred" | "native";
|
|
|
63
63
|
*/
|
|
64
64
|
type NativeEnumType = "union" | "enum";
|
|
65
65
|
/**
|
|
66
|
-
*
|
|
67
|
-
* These options override root-level options when specified.
|
|
68
|
-
* Resolution order: root options → request/response overrides (nested wins silently)
|
|
66
|
+
* Common options shared by both request and response contexts
|
|
69
67
|
*/
|
|
70
|
-
interface
|
|
68
|
+
interface CommonSchemaOptions {
|
|
71
69
|
/**
|
|
72
70
|
* Object validation mode
|
|
73
71
|
* - 'strict': Uses z.strictObject() - no additional properties allowed
|
|
@@ -90,10 +88,16 @@ interface RequestResponseOptions {
|
|
|
90
88
|
* Whether to include descriptions as JSDoc comments
|
|
91
89
|
*/
|
|
92
90
|
includeDescriptions?: boolean;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Request-specific options that can override root-level options
|
|
94
|
+
* Requests support native TypeScript type generation as an alternative to Zod schemas
|
|
95
|
+
*/
|
|
96
|
+
interface RequestOptions extends CommonSchemaOptions {
|
|
93
97
|
/**
|
|
94
|
-
* Type generation mode
|
|
98
|
+
* Type generation mode
|
|
95
99
|
* - 'inferred': Generate Zod schemas with z.infer types (default)
|
|
96
|
-
* - 'native': Generate native TypeScript types
|
|
100
|
+
* - 'native': Generate native TypeScript types without Zod validation
|
|
97
101
|
*/
|
|
98
102
|
typeMode?: TypeMode;
|
|
99
103
|
/**
|
|
@@ -103,7 +107,13 @@ interface RequestResponseOptions {
|
|
|
103
107
|
*/
|
|
104
108
|
nativeEnumType?: NativeEnumType;
|
|
105
109
|
}
|
|
106
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Response-specific options that can override root-level options
|
|
112
|
+
* Responses always use Zod schemas for runtime validation
|
|
113
|
+
*/
|
|
114
|
+
interface ResponseOptions extends CommonSchemaOptions {
|
|
115
|
+
}
|
|
116
|
+
interface OpenApiGeneratorOptions {
|
|
107
117
|
/**
|
|
108
118
|
* Object validation mode
|
|
109
119
|
* - 'strict': Uses z.strictObject() - no additional properties allowed
|
|
@@ -168,13 +178,15 @@ interface GeneratorOptions {
|
|
|
168
178
|
/**
|
|
169
179
|
* Request-specific options that override root-level options
|
|
170
180
|
* Applied when schemas are used in request contexts
|
|
181
|
+
* Supports native TypeScript type generation
|
|
171
182
|
*/
|
|
172
|
-
request?:
|
|
183
|
+
request?: RequestOptions;
|
|
173
184
|
/**
|
|
174
185
|
* Response-specific options that override root-level options
|
|
175
186
|
* Applied when schemas are used in response contexts
|
|
187
|
+
* Always generates Zod schemas for runtime validation
|
|
176
188
|
*/
|
|
177
|
-
response?:
|
|
189
|
+
response?: ResponseOptions;
|
|
178
190
|
}
|
|
179
191
|
interface OpenAPISchema {
|
|
180
192
|
type?: string | string[];
|
|
@@ -251,12 +263,12 @@ interface ConfigFile {
|
|
|
251
263
|
* Global default options applied to all specs
|
|
252
264
|
* Can be overridden by individual spec configurations
|
|
253
265
|
*/
|
|
254
|
-
defaults?: Partial<Omit<
|
|
266
|
+
defaults?: Partial<Omit<OpenApiGeneratorOptions, "input" | "output">>;
|
|
255
267
|
/**
|
|
256
268
|
* Array of OpenAPI specifications to process
|
|
257
269
|
* Each spec must have input and output paths
|
|
258
270
|
*/
|
|
259
|
-
specs:
|
|
271
|
+
specs: OpenApiGeneratorOptions[];
|
|
260
272
|
/**
|
|
261
273
|
* Execution mode for batch processing
|
|
262
274
|
* @default "parallel"
|
|
@@ -285,7 +297,7 @@ interface ConfigFile {
|
|
|
285
297
|
*/
|
|
286
298
|
declare function defineConfig(config: ConfigFile): ConfigFile;
|
|
287
299
|
|
|
288
|
-
declare class
|
|
300
|
+
declare class OpenApiGenerator {
|
|
289
301
|
private schemas;
|
|
290
302
|
private types;
|
|
291
303
|
private enums;
|
|
@@ -299,7 +311,7 @@ declare class ZodSchemaGenerator {
|
|
|
299
311
|
private requestOptions;
|
|
300
312
|
private responseOptions;
|
|
301
313
|
private needsZodImport;
|
|
302
|
-
constructor(options:
|
|
314
|
+
constructor(options: OpenApiGeneratorOptions);
|
|
303
315
|
/**
|
|
304
316
|
* Generate schemas as a string (without writing to file)
|
|
305
317
|
* @returns The generated TypeScript code as a string
|
|
@@ -400,4 +412,4 @@ declare class ZodSchemaGenerator {
|
|
|
400
412
|
private generateStats;
|
|
401
413
|
}
|
|
402
414
|
|
|
403
|
-
export { CircularReferenceError, CliOptionsError, type ConfigFile, ConfigValidationError, type ExecutionMode, FileOperationError, GeneratorError, type
|
|
415
|
+
export { CircularReferenceError, CliOptionsError, type ConfigFile, ConfigValidationError, type ExecutionMode, FileOperationError, GeneratorError, type OpenAPISpec, OpenApiGenerator, type OpenApiGeneratorOptions, SchemaGenerationError, SpecValidationError, defineConfig };
|
package/dist/index.d.ts
CHANGED
|
@@ -63,11 +63,9 @@ type TypeMode = "inferred" | "native";
|
|
|
63
63
|
*/
|
|
64
64
|
type NativeEnumType = "union" | "enum";
|
|
65
65
|
/**
|
|
66
|
-
*
|
|
67
|
-
* These options override root-level options when specified.
|
|
68
|
-
* Resolution order: root options → request/response overrides (nested wins silently)
|
|
66
|
+
* Common options shared by both request and response contexts
|
|
69
67
|
*/
|
|
70
|
-
interface
|
|
68
|
+
interface CommonSchemaOptions {
|
|
71
69
|
/**
|
|
72
70
|
* Object validation mode
|
|
73
71
|
* - 'strict': Uses z.strictObject() - no additional properties allowed
|
|
@@ -90,10 +88,16 @@ interface RequestResponseOptions {
|
|
|
90
88
|
* Whether to include descriptions as JSDoc comments
|
|
91
89
|
*/
|
|
92
90
|
includeDescriptions?: boolean;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Request-specific options that can override root-level options
|
|
94
|
+
* Requests support native TypeScript type generation as an alternative to Zod schemas
|
|
95
|
+
*/
|
|
96
|
+
interface RequestOptions extends CommonSchemaOptions {
|
|
93
97
|
/**
|
|
94
|
-
* Type generation mode
|
|
98
|
+
* Type generation mode
|
|
95
99
|
* - 'inferred': Generate Zod schemas with z.infer types (default)
|
|
96
|
-
* - 'native': Generate native TypeScript types
|
|
100
|
+
* - 'native': Generate native TypeScript types without Zod validation
|
|
97
101
|
*/
|
|
98
102
|
typeMode?: TypeMode;
|
|
99
103
|
/**
|
|
@@ -103,7 +107,13 @@ interface RequestResponseOptions {
|
|
|
103
107
|
*/
|
|
104
108
|
nativeEnumType?: NativeEnumType;
|
|
105
109
|
}
|
|
106
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Response-specific options that can override root-level options
|
|
112
|
+
* Responses always use Zod schemas for runtime validation
|
|
113
|
+
*/
|
|
114
|
+
interface ResponseOptions extends CommonSchemaOptions {
|
|
115
|
+
}
|
|
116
|
+
interface OpenApiGeneratorOptions {
|
|
107
117
|
/**
|
|
108
118
|
* Object validation mode
|
|
109
119
|
* - 'strict': Uses z.strictObject() - no additional properties allowed
|
|
@@ -168,13 +178,15 @@ interface GeneratorOptions {
|
|
|
168
178
|
/**
|
|
169
179
|
* Request-specific options that override root-level options
|
|
170
180
|
* Applied when schemas are used in request contexts
|
|
181
|
+
* Supports native TypeScript type generation
|
|
171
182
|
*/
|
|
172
|
-
request?:
|
|
183
|
+
request?: RequestOptions;
|
|
173
184
|
/**
|
|
174
185
|
* Response-specific options that override root-level options
|
|
175
186
|
* Applied when schemas are used in response contexts
|
|
187
|
+
* Always generates Zod schemas for runtime validation
|
|
176
188
|
*/
|
|
177
|
-
response?:
|
|
189
|
+
response?: ResponseOptions;
|
|
178
190
|
}
|
|
179
191
|
interface OpenAPISchema {
|
|
180
192
|
type?: string | string[];
|
|
@@ -251,12 +263,12 @@ interface ConfigFile {
|
|
|
251
263
|
* Global default options applied to all specs
|
|
252
264
|
* Can be overridden by individual spec configurations
|
|
253
265
|
*/
|
|
254
|
-
defaults?: Partial<Omit<
|
|
266
|
+
defaults?: Partial<Omit<OpenApiGeneratorOptions, "input" | "output">>;
|
|
255
267
|
/**
|
|
256
268
|
* Array of OpenAPI specifications to process
|
|
257
269
|
* Each spec must have input and output paths
|
|
258
270
|
*/
|
|
259
|
-
specs:
|
|
271
|
+
specs: OpenApiGeneratorOptions[];
|
|
260
272
|
/**
|
|
261
273
|
* Execution mode for batch processing
|
|
262
274
|
* @default "parallel"
|
|
@@ -285,7 +297,7 @@ interface ConfigFile {
|
|
|
285
297
|
*/
|
|
286
298
|
declare function defineConfig(config: ConfigFile): ConfigFile;
|
|
287
299
|
|
|
288
|
-
declare class
|
|
300
|
+
declare class OpenApiGenerator {
|
|
289
301
|
private schemas;
|
|
290
302
|
private types;
|
|
291
303
|
private enums;
|
|
@@ -299,7 +311,7 @@ declare class ZodSchemaGenerator {
|
|
|
299
311
|
private requestOptions;
|
|
300
312
|
private responseOptions;
|
|
301
313
|
private needsZodImport;
|
|
302
|
-
constructor(options:
|
|
314
|
+
constructor(options: OpenApiGeneratorOptions);
|
|
303
315
|
/**
|
|
304
316
|
* Generate schemas as a string (without writing to file)
|
|
305
317
|
* @returns The generated TypeScript code as a string
|
|
@@ -400,4 +412,4 @@ declare class ZodSchemaGenerator {
|
|
|
400
412
|
private generateStats;
|
|
401
413
|
}
|
|
402
414
|
|
|
403
|
-
export { CircularReferenceError, CliOptionsError, type ConfigFile, ConfigValidationError, type ExecutionMode, FileOperationError, GeneratorError, type
|
|
415
|
+
export { CircularReferenceError, CliOptionsError, type ConfigFile, ConfigValidationError, type ExecutionMode, FileOperationError, GeneratorError, type OpenAPISpec, OpenApiGenerator, type OpenApiGeneratorOptions, SchemaGenerationError, SpecValidationError, defineConfig };
|
package/dist/index.js
CHANGED
|
@@ -25,9 +25,9 @@ __export(src_exports, {
|
|
|
25
25
|
ConfigValidationError: () => ConfigValidationError,
|
|
26
26
|
FileOperationError: () => FileOperationError,
|
|
27
27
|
GeneratorError: () => GeneratorError,
|
|
28
|
+
OpenApiGenerator: () => OpenApiGenerator,
|
|
28
29
|
SchemaGenerationError: () => SchemaGenerationError,
|
|
29
30
|
SpecValidationError: () => SpecValidationError,
|
|
30
|
-
ZodSchemaGenerator: () => ZodSchemaGenerator,
|
|
31
31
|
defineConfig: () => defineConfig
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -91,7 +91,7 @@ var ConfigurationError = class extends GeneratorError {
|
|
|
91
91
|
}
|
|
92
92
|
};
|
|
93
93
|
|
|
94
|
-
// src/generator.ts
|
|
94
|
+
// src/openapi-generator.ts
|
|
95
95
|
var import_node_fs = require("fs");
|
|
96
96
|
var import_node_path = require("path");
|
|
97
97
|
var import_yaml = require("yaml");
|
|
@@ -1279,8 +1279,8 @@ _PropertyGenerator.INCLUSION_RULES = {
|
|
|
1279
1279
|
};
|
|
1280
1280
|
var PropertyGenerator = _PropertyGenerator;
|
|
1281
1281
|
|
|
1282
|
-
// src/generator.ts
|
|
1283
|
-
var
|
|
1282
|
+
// src/openapi-generator.ts
|
|
1283
|
+
var OpenApiGenerator = class {
|
|
1284
1284
|
constructor(options) {
|
|
1285
1285
|
this.schemas = /* @__PURE__ */ new Map();
|
|
1286
1286
|
this.types = /* @__PURE__ */ new Map();
|
|
@@ -1320,19 +1320,41 @@ var ZodSchemaGenerator = class {
|
|
|
1320
1320
|
}
|
|
1321
1321
|
}
|
|
1322
1322
|
try {
|
|
1323
|
-
const
|
|
1324
|
-
|
|
1323
|
+
const content = (0, import_node_fs.readFileSync)(this.options.input, "utf-8");
|
|
1324
|
+
try {
|
|
1325
|
+
this.spec = (0, import_yaml.parse)(content);
|
|
1326
|
+
} catch (yamlError) {
|
|
1327
|
+
try {
|
|
1328
|
+
this.spec = JSON.parse(content);
|
|
1329
|
+
} catch {
|
|
1330
|
+
if (yamlError instanceof Error) {
|
|
1331
|
+
const errorMessage = [
|
|
1332
|
+
`Failed to parse OpenAPI specification from: ${this.options.input}`,
|
|
1333
|
+
"",
|
|
1334
|
+
`Error: ${yamlError.message}`,
|
|
1335
|
+
"",
|
|
1336
|
+
"Please ensure:",
|
|
1337
|
+
" - The file exists and is readable",
|
|
1338
|
+
" - The file contains valid YAML or JSON syntax",
|
|
1339
|
+
" - The file is a valid OpenAPI 3.x specification"
|
|
1340
|
+
].join("\n");
|
|
1341
|
+
throw new SpecValidationError(errorMessage, {
|
|
1342
|
+
filePath: this.options.input,
|
|
1343
|
+
originalError: yamlError.message
|
|
1344
|
+
});
|
|
1345
|
+
}
|
|
1346
|
+
throw yamlError;
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1325
1349
|
} catch (error) {
|
|
1350
|
+
if (error instanceof SpecValidationError) {
|
|
1351
|
+
throw error;
|
|
1352
|
+
}
|
|
1326
1353
|
if (error instanceof Error) {
|
|
1327
1354
|
const errorMessage = [
|
|
1328
|
-
`Failed to
|
|
1329
|
-
"",
|
|
1330
|
-
`Error: ${error.message}`,
|
|
1355
|
+
`Failed to read OpenAPI specification from: ${this.options.input}`,
|
|
1331
1356
|
"",
|
|
1332
|
-
|
|
1333
|
-
" - The file exists and is readable",
|
|
1334
|
-
" - The file contains valid YAML syntax",
|
|
1335
|
-
" - The file is a valid OpenAPI 3.x specification"
|
|
1357
|
+
`Error: ${error.message}`
|
|
1336
1358
|
].join("\n");
|
|
1337
1359
|
throw new SpecValidationError(errorMessage, { filePath: this.options.input, originalError: error.message });
|
|
1338
1360
|
}
|
|
@@ -1433,7 +1455,8 @@ var ZodSchemaGenerator = class {
|
|
|
1433
1455
|
* Ensure directory exists for a file path
|
|
1434
1456
|
*/
|
|
1435
1457
|
ensureDirectoryExists(filePath) {
|
|
1436
|
-
const
|
|
1458
|
+
const normalizedPath = (0, import_node_path.normalize)(filePath);
|
|
1459
|
+
const dir = (0, import_node_path.dirname)(normalizedPath);
|
|
1437
1460
|
if (!(0, import_node_fs.existsSync)(dir)) {
|
|
1438
1461
|
(0, import_node_fs.mkdirSync)(dir, { recursive: true });
|
|
1439
1462
|
}
|
|
@@ -1449,8 +1472,9 @@ var ZodSchemaGenerator = class {
|
|
|
1449
1472
|
);
|
|
1450
1473
|
}
|
|
1451
1474
|
const output = this.generateString();
|
|
1452
|
-
|
|
1453
|
-
|
|
1475
|
+
const normalizedOutput = (0, import_node_path.normalize)(this.options.output);
|
|
1476
|
+
this.ensureDirectoryExists(normalizedOutput);
|
|
1477
|
+
(0, import_node_fs.writeFileSync)(normalizedOutput, output);
|
|
1454
1478
|
}
|
|
1455
1479
|
/**
|
|
1456
1480
|
* Resolve options for a specific context (request or response)
|
|
@@ -1458,17 +1482,18 @@ var ZodSchemaGenerator = class {
|
|
|
1458
1482
|
* Response schemas always use 'inferred' mode (Zod schemas)
|
|
1459
1483
|
*/
|
|
1460
1484
|
resolveOptionsForContext(context) {
|
|
1461
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
1485
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
1462
1486
|
const contextOptions = context === "request" ? this.options.request : this.options.response;
|
|
1487
|
+
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";
|
|
1463
1488
|
return {
|
|
1464
|
-
mode: (
|
|
1465
|
-
enumType: (
|
|
1466
|
-
useDescribe: (
|
|
1467
|
-
includeDescriptions: (
|
|
1489
|
+
mode: (_f = (_e = contextOptions == null ? void 0 : contextOptions.mode) != null ? _e : this.options.mode) != null ? _f : "normal",
|
|
1490
|
+
enumType: (_h = (_g = contextOptions == null ? void 0 : contextOptions.enumType) != null ? _g : this.options.enumType) != null ? _h : "zod",
|
|
1491
|
+
useDescribe: (_j = (_i = contextOptions == null ? void 0 : contextOptions.useDescribe) != null ? _i : this.options.useDescribe) != null ? _j : false,
|
|
1492
|
+
includeDescriptions: (_l = (_k = contextOptions == null ? void 0 : contextOptions.includeDescriptions) != null ? _k : this.options.includeDescriptions) != null ? _l : true,
|
|
1468
1493
|
// Response schemas always use 'inferred' mode (Zod schemas are required)
|
|
1469
1494
|
// Request schemas can optionally use 'native' mode
|
|
1470
|
-
typeMode: context === "response" ? "inferred" : (
|
|
1471
|
-
nativeEnumType
|
|
1495
|
+
typeMode: context === "response" ? "inferred" : (_n = (_m = this.options.request) == null ? void 0 : _m.typeMode) != null ? _n : "inferred",
|
|
1496
|
+
nativeEnumType
|
|
1472
1497
|
};
|
|
1473
1498
|
}
|
|
1474
1499
|
/**
|
|
@@ -2198,9 +2223,9 @@ function defineConfig(config) {
|
|
|
2198
2223
|
ConfigValidationError,
|
|
2199
2224
|
FileOperationError,
|
|
2200
2225
|
GeneratorError,
|
|
2226
|
+
OpenApiGenerator,
|
|
2201
2227
|
SchemaGenerationError,
|
|
2202
2228
|
SpecValidationError,
|
|
2203
|
-
ZodSchemaGenerator,
|
|
2204
2229
|
defineConfig
|
|
2205
2230
|
});
|
|
2206
2231
|
//# sourceMappingURL=index.js.map
|