@cerios/openapi-to-zod 1.1.0 → 1.1.1
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 +84 -28
- package/dist/cli.js +97 -58
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +97 -58
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +97 -58
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +97 -58
- package/dist/index.mjs.map +1 -1
- package/dist/internal.d.mts +12 -18
- package/dist/internal.d.ts +12 -18
- package/dist/internal.js +36 -49
- package/dist/internal.js.map +1 -1
- package/dist/internal.mjs +36 -49
- package/dist/internal.mjs.map +1 -1
- package/dist/{types-B7ePTDjr.d.mts → types-CI48CjiU.d.mts} +19 -8
- package/dist/{types-B7ePTDjr.d.ts → types-CI48CjiU.d.ts} +19 -8
- package/package.json +93 -102
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { O as OpenApiGeneratorOptions } from './types-
|
|
2
|
-
export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPISchema, c as OpenAPISpec, d as OperationFilters, R as RequestOptions, e as ResponseOptions, f as defineConfig } from './types-
|
|
1
|
+
import { O as OpenApiGeneratorOptions } from './types-CI48CjiU.mjs';
|
|
2
|
+
export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPISchema, c as OpenAPISpec, d as OperationFilters, R as RequestOptions, e as ResponseOptions, f as defineConfig } from './types-CI48CjiU.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Custom error classes for better error handling and debugging
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { O as OpenApiGeneratorOptions } from './types-
|
|
2
|
-
export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPISchema, c as OpenAPISpec, d as OperationFilters, R as RequestOptions, e as ResponseOptions, f as defineConfig } from './types-
|
|
1
|
+
import { O as OpenApiGeneratorOptions } from './types-CI48CjiU.js';
|
|
2
|
+
export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPISchema, c as OpenAPISpec, d as OperationFilters, R as RequestOptions, e as ResponseOptions, f as defineConfig } from './types-CI48CjiU.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Custom error classes for better error handling and debugging
|
package/dist/index.js
CHANGED
|
@@ -94,7 +94,7 @@ var ConfigurationError = class extends GeneratorError {
|
|
|
94
94
|
// src/openapi-generator.ts
|
|
95
95
|
var import_node_fs = require("fs");
|
|
96
96
|
var import_node_path = require("path");
|
|
97
|
-
var
|
|
97
|
+
var import_minimatch3 = require("minimatch");
|
|
98
98
|
var import_yaml = require("yaml");
|
|
99
99
|
|
|
100
100
|
// src/utils/name-utils.ts
|
|
@@ -153,8 +153,26 @@ function resolveRef(ref) {
|
|
|
153
153
|
function generateEnum(name, values, options) {
|
|
154
154
|
const schemaName = `${toCamelCase(name, options)}Schema`;
|
|
155
155
|
const typeName = toPascalCase(name);
|
|
156
|
-
const
|
|
157
|
-
|
|
156
|
+
const allBooleans = values.every((v) => typeof v === "boolean");
|
|
157
|
+
if (allBooleans) {
|
|
158
|
+
const schemaCode2 = `export const ${schemaName} = z.boolean();`;
|
|
159
|
+
const typeCode2 = `export type ${typeName} = z.infer<typeof ${schemaName}>;`;
|
|
160
|
+
return { schemaCode: schemaCode2, typeCode: typeCode2 };
|
|
161
|
+
}
|
|
162
|
+
const allStrings = values.every((v) => typeof v === "string");
|
|
163
|
+
if (allStrings) {
|
|
164
|
+
const enumValues = values.map((v) => `"${v}"`).join(", ");
|
|
165
|
+
const schemaCode2 = `export const ${schemaName} = z.enum([${enumValues}]);`;
|
|
166
|
+
const typeCode2 = `export type ${typeName} = z.infer<typeof ${schemaName}>;`;
|
|
167
|
+
return { schemaCode: schemaCode2, typeCode: typeCode2 };
|
|
168
|
+
}
|
|
169
|
+
const literalValues = values.map((v) => {
|
|
170
|
+
if (typeof v === "string") {
|
|
171
|
+
return `z.literal("${v}")`;
|
|
172
|
+
}
|
|
173
|
+
return `z.literal(${v})`;
|
|
174
|
+
}).join(", ");
|
|
175
|
+
const schemaCode = `export const ${schemaName} = z.union([${literalValues}]);`;
|
|
158
176
|
const typeCode = `export type ${typeName} = z.infer<typeof ${schemaName}>;`;
|
|
159
177
|
return { schemaCode, typeCode };
|
|
160
178
|
}
|
|
@@ -164,7 +182,7 @@ function escapeDescription(str) {
|
|
|
164
182
|
return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n");
|
|
165
183
|
}
|
|
166
184
|
function escapePattern(str) {
|
|
167
|
-
return str.replace(
|
|
185
|
+
return str.replace(/\//g, "\\/");
|
|
168
186
|
}
|
|
169
187
|
function escapeJSDoc(str) {
|
|
170
188
|
return str.replace(/\*\//g, "*\\/");
|
|
@@ -289,61 +307,36 @@ var LRUCache = class {
|
|
|
289
307
|
};
|
|
290
308
|
|
|
291
309
|
// src/utils/pattern-utils.ts
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
if (/\\[dDwWsS]/.test(pattern)) {
|
|
297
|
-
return true;
|
|
298
|
-
}
|
|
299
|
-
if (/\.\*|\.\+/.test(pattern)) {
|
|
300
|
-
return true;
|
|
301
|
-
}
|
|
302
|
-
if (/[[\]()]/.test(pattern)) {
|
|
303
|
-
return true;
|
|
304
|
-
}
|
|
305
|
-
if (/[^/][+?*]\{/.test(pattern)) {
|
|
310
|
+
var import_minimatch = require("minimatch");
|
|
311
|
+
function isValidGlobPattern(pattern) {
|
|
312
|
+
try {
|
|
313
|
+
new import_minimatch.minimatch.Minimatch(pattern);
|
|
306
314
|
return true;
|
|
315
|
+
} catch {
|
|
316
|
+
return false;
|
|
307
317
|
}
|
|
308
|
-
return false;
|
|
309
318
|
}
|
|
310
|
-
function
|
|
311
|
-
|
|
312
|
-
return pattern;
|
|
313
|
-
}
|
|
314
|
-
if (isRegexPattern(pattern)) {
|
|
315
|
-
try {
|
|
316
|
-
return new RegExp(pattern);
|
|
317
|
-
} catch (error) {
|
|
318
|
-
console.warn(`\u26A0\uFE0F Invalid regex pattern "${pattern}": ${error instanceof Error ? error.message : String(error)}`);
|
|
319
|
-
return null;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
return null;
|
|
319
|
+
function isGlobPattern(pattern) {
|
|
320
|
+
return /[*?[\]{}!]/.test(pattern);
|
|
323
321
|
}
|
|
324
322
|
function stripPrefix(input, pattern, ensureLeadingChar) {
|
|
325
323
|
if (!pattern) {
|
|
326
324
|
return input;
|
|
327
325
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
return `${ensureLeadingChar}${stripped}`;
|
|
339
|
-
}
|
|
326
|
+
if (isGlobPattern(pattern) && !isValidGlobPattern(pattern)) {
|
|
327
|
+
console.warn(`\u26A0\uFE0F Invalid glob pattern "${pattern}": Pattern is malformed`);
|
|
328
|
+
return input;
|
|
329
|
+
}
|
|
330
|
+
if (isGlobPattern(pattern)) {
|
|
331
|
+
let longestMatch = -1;
|
|
332
|
+
for (let i = 1; i <= input.length; i++) {
|
|
333
|
+
const testPrefix = input.substring(0, i);
|
|
334
|
+
if ((0, import_minimatch.minimatch)(testPrefix, pattern)) {
|
|
335
|
+
longestMatch = i;
|
|
340
336
|
}
|
|
341
|
-
return stripped;
|
|
342
337
|
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
if (input.startsWith(stringPattern)) {
|
|
346
|
-
const stripped = input.substring(stringPattern.length);
|
|
338
|
+
if (longestMatch > 0) {
|
|
339
|
+
const stripped = input.substring(longestMatch);
|
|
347
340
|
if (ensureLeadingChar) {
|
|
348
341
|
if (stripped === "") {
|
|
349
342
|
return ensureLeadingChar;
|
|
@@ -352,8 +345,21 @@ function stripPrefix(input, pattern, ensureLeadingChar) {
|
|
|
352
345
|
return `${ensureLeadingChar}${stripped}`;
|
|
353
346
|
}
|
|
354
347
|
}
|
|
355
|
-
return stripped;
|
|
348
|
+
return stripped === "" && !ensureLeadingChar ? input : stripped;
|
|
349
|
+
}
|
|
350
|
+
return input;
|
|
351
|
+
}
|
|
352
|
+
if (input.startsWith(pattern)) {
|
|
353
|
+
const stripped = input.substring(pattern.length);
|
|
354
|
+
if (ensureLeadingChar) {
|
|
355
|
+
if (stripped === "") {
|
|
356
|
+
return ensureLeadingChar;
|
|
357
|
+
}
|
|
358
|
+
if (!stripped.startsWith(ensureLeadingChar)) {
|
|
359
|
+
return `${ensureLeadingChar}${stripped}`;
|
|
360
|
+
}
|
|
356
361
|
}
|
|
362
|
+
return stripped;
|
|
357
363
|
}
|
|
358
364
|
return input;
|
|
359
365
|
}
|
|
@@ -1228,9 +1234,25 @@ var _PropertyGenerator = class _PropertyGenerator {
|
|
|
1228
1234
|
return wrapNullable(zodLiteral, nullable);
|
|
1229
1235
|
}
|
|
1230
1236
|
if (schema.enum) {
|
|
1231
|
-
const
|
|
1232
|
-
|
|
1233
|
-
|
|
1237
|
+
const allBooleans = schema.enum.every((v) => typeof v === "boolean");
|
|
1238
|
+
if (allBooleans) {
|
|
1239
|
+
const zodBoolean = "z.boolean()";
|
|
1240
|
+
return wrapNullable(zodBoolean, nullable);
|
|
1241
|
+
}
|
|
1242
|
+
const allStrings = schema.enum.every((v) => typeof v === "string");
|
|
1243
|
+
if (allStrings) {
|
|
1244
|
+
const enumValues = schema.enum.map((v) => `"${v}"`).join(", ");
|
|
1245
|
+
const zodEnum = `z.enum([${enumValues}])`;
|
|
1246
|
+
return wrapNullable(zodEnum, nullable);
|
|
1247
|
+
}
|
|
1248
|
+
const literalValues = schema.enum.map((v) => {
|
|
1249
|
+
if (typeof v === "string") {
|
|
1250
|
+
return `z.literal("${v}")`;
|
|
1251
|
+
}
|
|
1252
|
+
return `z.literal(${v})`;
|
|
1253
|
+
}).join(", ");
|
|
1254
|
+
const zodUnion = `z.union([${literalValues}])`;
|
|
1255
|
+
return wrapNullable(zodUnion, nullable);
|
|
1234
1256
|
}
|
|
1235
1257
|
if (schema.allOf) {
|
|
1236
1258
|
let composition = generateAllOf(
|
|
@@ -1361,7 +1383,7 @@ _PropertyGenerator.INCLUSION_RULES = {
|
|
|
1361
1383
|
var PropertyGenerator = _PropertyGenerator;
|
|
1362
1384
|
|
|
1363
1385
|
// src/utils/operation-filters.ts
|
|
1364
|
-
var
|
|
1386
|
+
var import_minimatch2 = require("minimatch");
|
|
1365
1387
|
function createFilterStatistics() {
|
|
1366
1388
|
return {
|
|
1367
1389
|
totalOperations: 0,
|
|
@@ -1380,7 +1402,7 @@ function matchesAnyPattern(value, patterns) {
|
|
|
1380
1402
|
if (!value) {
|
|
1381
1403
|
return false;
|
|
1382
1404
|
}
|
|
1383
|
-
return patterns.some((pattern) => (0,
|
|
1405
|
+
return patterns.some((pattern) => (0, import_minimatch2.minimatch)(value, pattern));
|
|
1384
1406
|
}
|
|
1385
1407
|
function containsAny(arr, values) {
|
|
1386
1408
|
if (!values || values.length === 0) {
|
|
@@ -1607,6 +1629,9 @@ var OpenApiGenerator = class {
|
|
|
1607
1629
|
throw new SpecValidationError("No schemas found in OpenAPI spec", { filePath: this.options.input });
|
|
1608
1630
|
}
|
|
1609
1631
|
for (const [name, schema] of Object.entries(this.spec.components.schemas)) {
|
|
1632
|
+
if (this.options.operationFilters && this.schemaUsageMap.size > 0 && !this.schemaUsageMap.has(name)) {
|
|
1633
|
+
continue;
|
|
1634
|
+
}
|
|
1610
1635
|
this.generateComponentSchema(name, schema);
|
|
1611
1636
|
}
|
|
1612
1637
|
this.generateQueryParameterSchemas();
|
|
@@ -2095,7 +2120,7 @@ ${propsCode}
|
|
|
2095
2120
|
const headerLower = headerName.toLowerCase();
|
|
2096
2121
|
return ignorePatterns.some((pattern) => {
|
|
2097
2122
|
const patternLower = pattern.toLowerCase();
|
|
2098
|
-
return (0,
|
|
2123
|
+
return (0, import_minimatch3.minimatch)(headerLower, patternLower);
|
|
2099
2124
|
});
|
|
2100
2125
|
}
|
|
2101
2126
|
/**
|
|
@@ -2183,8 +2208,22 @@ ${propsCode}
|
|
|
2183
2208
|
return `${schemaName}Schema`;
|
|
2184
2209
|
}
|
|
2185
2210
|
if (schema.enum) {
|
|
2186
|
-
const
|
|
2187
|
-
|
|
2211
|
+
const allBooleans = schema.enum.every((v) => typeof v === "boolean");
|
|
2212
|
+
if (allBooleans) {
|
|
2213
|
+
return "z.boolean()";
|
|
2214
|
+
}
|
|
2215
|
+
const allStrings = schema.enum.every((v) => typeof v === "string");
|
|
2216
|
+
if (allStrings) {
|
|
2217
|
+
const enumValues = schema.enum.map((v) => `"${v}"`).join(", ");
|
|
2218
|
+
return `z.enum([${enumValues}])`;
|
|
2219
|
+
}
|
|
2220
|
+
const literalValues = schema.enum.map((v) => {
|
|
2221
|
+
if (typeof v === "string") {
|
|
2222
|
+
return `z.literal("${v}")`;
|
|
2223
|
+
}
|
|
2224
|
+
return `z.literal(${v})`;
|
|
2225
|
+
}).join(", ");
|
|
2226
|
+
return `z.union([${literalValues}])`;
|
|
2188
2227
|
}
|
|
2189
2228
|
const type = schema.type;
|
|
2190
2229
|
if (type === "string") {
|