@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.mjs
CHANGED
|
@@ -64,9 +64,9 @@ var ConfigurationError = class extends GeneratorError {
|
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
-
// src/generator.ts
|
|
67
|
+
// src/openapi-generator.ts
|
|
68
68
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
69
|
-
import { dirname } from "path";
|
|
69
|
+
import { dirname, normalize } from "path";
|
|
70
70
|
import { parse } from "yaml";
|
|
71
71
|
|
|
72
72
|
// src/utils/name-utils.ts
|
|
@@ -1252,8 +1252,8 @@ _PropertyGenerator.INCLUSION_RULES = {
|
|
|
1252
1252
|
};
|
|
1253
1253
|
var PropertyGenerator = _PropertyGenerator;
|
|
1254
1254
|
|
|
1255
|
-
// src/generator.ts
|
|
1256
|
-
var
|
|
1255
|
+
// src/openapi-generator.ts
|
|
1256
|
+
var OpenApiGenerator = class {
|
|
1257
1257
|
constructor(options) {
|
|
1258
1258
|
this.schemas = /* @__PURE__ */ new Map();
|
|
1259
1259
|
this.types = /* @__PURE__ */ new Map();
|
|
@@ -1293,19 +1293,41 @@ var ZodSchemaGenerator = class {
|
|
|
1293
1293
|
}
|
|
1294
1294
|
}
|
|
1295
1295
|
try {
|
|
1296
|
-
const
|
|
1297
|
-
|
|
1296
|
+
const content = readFileSync(this.options.input, "utf-8");
|
|
1297
|
+
try {
|
|
1298
|
+
this.spec = parse(content);
|
|
1299
|
+
} catch (yamlError) {
|
|
1300
|
+
try {
|
|
1301
|
+
this.spec = JSON.parse(content);
|
|
1302
|
+
} catch {
|
|
1303
|
+
if (yamlError instanceof Error) {
|
|
1304
|
+
const errorMessage = [
|
|
1305
|
+
`Failed to parse OpenAPI specification from: ${this.options.input}`,
|
|
1306
|
+
"",
|
|
1307
|
+
`Error: ${yamlError.message}`,
|
|
1308
|
+
"",
|
|
1309
|
+
"Please ensure:",
|
|
1310
|
+
" - The file exists and is readable",
|
|
1311
|
+
" - The file contains valid YAML or JSON syntax",
|
|
1312
|
+
" - The file is a valid OpenAPI 3.x specification"
|
|
1313
|
+
].join("\n");
|
|
1314
|
+
throw new SpecValidationError(errorMessage, {
|
|
1315
|
+
filePath: this.options.input,
|
|
1316
|
+
originalError: yamlError.message
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
throw yamlError;
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1298
1322
|
} catch (error) {
|
|
1323
|
+
if (error instanceof SpecValidationError) {
|
|
1324
|
+
throw error;
|
|
1325
|
+
}
|
|
1299
1326
|
if (error instanceof Error) {
|
|
1300
1327
|
const errorMessage = [
|
|
1301
|
-
`Failed to
|
|
1302
|
-
"",
|
|
1303
|
-
`Error: ${error.message}`,
|
|
1328
|
+
`Failed to read OpenAPI specification from: ${this.options.input}`,
|
|
1304
1329
|
"",
|
|
1305
|
-
|
|
1306
|
-
" - The file exists and is readable",
|
|
1307
|
-
" - The file contains valid YAML syntax",
|
|
1308
|
-
" - The file is a valid OpenAPI 3.x specification"
|
|
1330
|
+
`Error: ${error.message}`
|
|
1309
1331
|
].join("\n");
|
|
1310
1332
|
throw new SpecValidationError(errorMessage, { filePath: this.options.input, originalError: error.message });
|
|
1311
1333
|
}
|
|
@@ -1406,7 +1428,8 @@ var ZodSchemaGenerator = class {
|
|
|
1406
1428
|
* Ensure directory exists for a file path
|
|
1407
1429
|
*/
|
|
1408
1430
|
ensureDirectoryExists(filePath) {
|
|
1409
|
-
const
|
|
1431
|
+
const normalizedPath = normalize(filePath);
|
|
1432
|
+
const dir = dirname(normalizedPath);
|
|
1410
1433
|
if (!existsSync(dir)) {
|
|
1411
1434
|
mkdirSync(dir, { recursive: true });
|
|
1412
1435
|
}
|
|
@@ -1422,8 +1445,9 @@ var ZodSchemaGenerator = class {
|
|
|
1422
1445
|
);
|
|
1423
1446
|
}
|
|
1424
1447
|
const output = this.generateString();
|
|
1425
|
-
|
|
1426
|
-
|
|
1448
|
+
const normalizedOutput = normalize(this.options.output);
|
|
1449
|
+
this.ensureDirectoryExists(normalizedOutput);
|
|
1450
|
+
writeFileSync(normalizedOutput, output);
|
|
1427
1451
|
}
|
|
1428
1452
|
/**
|
|
1429
1453
|
* Resolve options for a specific context (request or response)
|
|
@@ -1431,17 +1455,18 @@ var ZodSchemaGenerator = class {
|
|
|
1431
1455
|
* Response schemas always use 'inferred' mode (Zod schemas)
|
|
1432
1456
|
*/
|
|
1433
1457
|
resolveOptionsForContext(context) {
|
|
1434
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
1458
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
1435
1459
|
const contextOptions = context === "request" ? this.options.request : this.options.response;
|
|
1460
|
+
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";
|
|
1436
1461
|
return {
|
|
1437
|
-
mode: (
|
|
1438
|
-
enumType: (
|
|
1439
|
-
useDescribe: (
|
|
1440
|
-
includeDescriptions: (
|
|
1462
|
+
mode: (_f = (_e = contextOptions == null ? void 0 : contextOptions.mode) != null ? _e : this.options.mode) != null ? _f : "normal",
|
|
1463
|
+
enumType: (_h = (_g = contextOptions == null ? void 0 : contextOptions.enumType) != null ? _g : this.options.enumType) != null ? _h : "zod",
|
|
1464
|
+
useDescribe: (_j = (_i = contextOptions == null ? void 0 : contextOptions.useDescribe) != null ? _i : this.options.useDescribe) != null ? _j : false,
|
|
1465
|
+
includeDescriptions: (_l = (_k = contextOptions == null ? void 0 : contextOptions.includeDescriptions) != null ? _k : this.options.includeDescriptions) != null ? _l : true,
|
|
1441
1466
|
// Response schemas always use 'inferred' mode (Zod schemas are required)
|
|
1442
1467
|
// Request schemas can optionally use 'native' mode
|
|
1443
|
-
typeMode: context === "response" ? "inferred" : (
|
|
1444
|
-
nativeEnumType
|
|
1468
|
+
typeMode: context === "response" ? "inferred" : (_n = (_m = this.options.request) == null ? void 0 : _m.typeMode) != null ? _n : "inferred",
|
|
1469
|
+
nativeEnumType
|
|
1445
1470
|
};
|
|
1446
1471
|
}
|
|
1447
1472
|
/**
|
|
@@ -2170,9 +2195,9 @@ export {
|
|
|
2170
2195
|
ConfigValidationError,
|
|
2171
2196
|
FileOperationError,
|
|
2172
2197
|
GeneratorError,
|
|
2198
|
+
OpenApiGenerator,
|
|
2173
2199
|
SchemaGenerationError,
|
|
2174
2200
|
SpecValidationError,
|
|
2175
|
-
ZodSchemaGenerator,
|
|
2176
2201
|
defineConfig
|
|
2177
2202
|
};
|
|
2178
2203
|
//# sourceMappingURL=index.mjs.map
|