@cerios/openapi-to-zod 0.3.0 → 0.5.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 +2 -2
- package/dist/cli.js +397 -33
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +408 -38
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +155 -6
- package/dist/index.d.ts +155 -6
- package/dist/index.js +283 -23
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +277 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
package/dist/index.d.mts
CHANGED
|
@@ -113,7 +113,7 @@ interface RequestOptions extends CommonSchemaOptions {
|
|
|
113
113
|
*/
|
|
114
114
|
interface ResponseOptions extends CommonSchemaOptions {
|
|
115
115
|
}
|
|
116
|
-
interface
|
|
116
|
+
interface OpenApiGeneratorOptions {
|
|
117
117
|
/**
|
|
118
118
|
* Object validation mode
|
|
119
119
|
* - 'strict': Uses z.strictObject() - no additional properties allowed
|
|
@@ -187,6 +187,98 @@ interface GeneratorOptions {
|
|
|
187
187
|
* Always generates Zod schemas for runtime validation
|
|
188
188
|
*/
|
|
189
189
|
response?: ResponseOptions;
|
|
190
|
+
/**
|
|
191
|
+
* Filter which operations to include/exclude from generation
|
|
192
|
+
* Useful for generating separate schemas for different API subsets
|
|
193
|
+
*
|
|
194
|
+
* Filtering logic:
|
|
195
|
+
* 1. If no filters specified, all operations are included
|
|
196
|
+
* 2. Empty arrays are treated as "no constraint" (not as "exclude all")
|
|
197
|
+
* 3. Include filters are applied first (allowlist)
|
|
198
|
+
* 4. Exclude filters are applied second (blocklist)
|
|
199
|
+
* 5. Exclude rules always win over include rules
|
|
200
|
+
*
|
|
201
|
+
* Supports glob patterns for paths and operationIds (e.g., "/api/v1/**", "get*")
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* // Only generate schemas for user-related endpoints
|
|
205
|
+
* operationFilters: {
|
|
206
|
+
* includeTags: ["users"]
|
|
207
|
+
* }
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* // Generate only GET endpoints, excluding deprecated ones
|
|
211
|
+
* operationFilters: {
|
|
212
|
+
* includeMethods: ["get"],
|
|
213
|
+
* excludeDeprecated: true
|
|
214
|
+
* }
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* // Generate only v1 API endpoints
|
|
218
|
+
* operationFilters: {
|
|
219
|
+
* includePaths: ["/api/v1/**"]
|
|
220
|
+
* }
|
|
221
|
+
*/
|
|
222
|
+
operationFilters?: OperationFilters;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Operation filtering options
|
|
226
|
+
* Controls which operations from the OpenAPI spec are included in generation
|
|
227
|
+
*/
|
|
228
|
+
interface OperationFilters {
|
|
229
|
+
/**
|
|
230
|
+
* Include only operations with these tags
|
|
231
|
+
* If specified, only operations with at least one matching tag are included
|
|
232
|
+
* Empty array = no constraint
|
|
233
|
+
*/
|
|
234
|
+
includeTags?: string[];
|
|
235
|
+
/**
|
|
236
|
+
* Exclude operations with these tags
|
|
237
|
+
* Operations with any matching tag are excluded
|
|
238
|
+
* Empty array = no constraint
|
|
239
|
+
*/
|
|
240
|
+
excludeTags?: string[];
|
|
241
|
+
/**
|
|
242
|
+
* Include only operations matching these path patterns
|
|
243
|
+
* Supports glob patterns (e.g., "/users/**", "/api/v1/*")
|
|
244
|
+
* Empty array = no constraint
|
|
245
|
+
*/
|
|
246
|
+
includePaths?: string[];
|
|
247
|
+
/**
|
|
248
|
+
* Exclude operations matching these path patterns
|
|
249
|
+
* Supports glob patterns (e.g., "/internal/**", "/admin/*")
|
|
250
|
+
* Empty array = no constraint
|
|
251
|
+
*/
|
|
252
|
+
excludePaths?: string[];
|
|
253
|
+
/**
|
|
254
|
+
* Include only these HTTP methods
|
|
255
|
+
* Valid values: "get", "post", "put", "patch", "delete", "head", "options"
|
|
256
|
+
* Empty array = no constraint
|
|
257
|
+
*/
|
|
258
|
+
includeMethods?: string[];
|
|
259
|
+
/**
|
|
260
|
+
* Exclude these HTTP methods
|
|
261
|
+
* Valid values: "get", "post", "put", "patch", "delete", "head", "options"
|
|
262
|
+
* Empty array = no constraint
|
|
263
|
+
*/
|
|
264
|
+
excludeMethods?: string[];
|
|
265
|
+
/**
|
|
266
|
+
* Include only operations matching these operationId patterns
|
|
267
|
+
* Supports glob patterns (e.g., "getUser*", "*Admin")
|
|
268
|
+
* Empty array = no constraint
|
|
269
|
+
*/
|
|
270
|
+
includeOperationIds?: string[];
|
|
271
|
+
/**
|
|
272
|
+
* Exclude operations matching these operationId patterns
|
|
273
|
+
* Supports glob patterns (e.g., "deleteUser*", "*Internal")
|
|
274
|
+
* Empty array = no constraint
|
|
275
|
+
*/
|
|
276
|
+
excludeOperationIds?: string[];
|
|
277
|
+
/**
|
|
278
|
+
* Whether to exclude deprecated operations
|
|
279
|
+
* @default false
|
|
280
|
+
*/
|
|
281
|
+
excludeDeprecated?: boolean;
|
|
190
282
|
}
|
|
191
283
|
interface OpenAPISchema {
|
|
192
284
|
type?: string | string[];
|
|
@@ -263,12 +355,12 @@ interface ConfigFile {
|
|
|
263
355
|
* Global default options applied to all specs
|
|
264
356
|
* Can be overridden by individual spec configurations
|
|
265
357
|
*/
|
|
266
|
-
defaults?: Partial<Omit<
|
|
358
|
+
defaults?: Partial<Omit<OpenApiGeneratorOptions, "input" | "output">>;
|
|
267
359
|
/**
|
|
268
360
|
* Array of OpenAPI specifications to process
|
|
269
361
|
* Each spec must have input and output paths
|
|
270
362
|
*/
|
|
271
|
-
specs:
|
|
363
|
+
specs: OpenApiGeneratorOptions[];
|
|
272
364
|
/**
|
|
273
365
|
* Execution mode for batch processing
|
|
274
366
|
* @default "parallel"
|
|
@@ -297,7 +389,7 @@ interface ConfigFile {
|
|
|
297
389
|
*/
|
|
298
390
|
declare function defineConfig(config: ConfigFile): ConfigFile;
|
|
299
391
|
|
|
300
|
-
declare class
|
|
392
|
+
declare class OpenApiGenerator {
|
|
301
393
|
private schemas;
|
|
302
394
|
private types;
|
|
303
395
|
private enums;
|
|
@@ -311,7 +403,8 @@ declare class ZodSchemaGenerator {
|
|
|
311
403
|
private requestOptions;
|
|
312
404
|
private responseOptions;
|
|
313
405
|
private needsZodImport;
|
|
314
|
-
|
|
406
|
+
private filterStats;
|
|
407
|
+
constructor(options: OpenApiGeneratorOptions);
|
|
315
408
|
/**
|
|
316
409
|
* Generate schemas as a string (without writing to file)
|
|
317
410
|
* @returns The generated TypeScript code as a string
|
|
@@ -377,6 +470,11 @@ declare class ZodSchemaGenerator {
|
|
|
377
470
|
* Generate query parameter schemas for each operation
|
|
378
471
|
*/
|
|
379
472
|
private generateQueryParameterSchemas;
|
|
473
|
+
/**
|
|
474
|
+
* Generate header parameter schemas for each operation
|
|
475
|
+
* Header parameters are always string type (HTTP header semantics)
|
|
476
|
+
*/
|
|
477
|
+
private generateHeaderParameterSchemas;
|
|
380
478
|
/**
|
|
381
479
|
* Generate Zod type for a query parameter schema
|
|
382
480
|
*/
|
|
@@ -412,4 +510,55 @@ declare class ZodSchemaGenerator {
|
|
|
412
510
|
private generateStats;
|
|
413
511
|
}
|
|
414
512
|
|
|
415
|
-
|
|
513
|
+
/**
|
|
514
|
+
* Filter statistics to track which operations were included/excluded
|
|
515
|
+
*/
|
|
516
|
+
interface FilterStatistics {
|
|
517
|
+
totalOperations: number;
|
|
518
|
+
includedOperations: number;
|
|
519
|
+
filteredByTags: number;
|
|
520
|
+
filteredByPaths: number;
|
|
521
|
+
filteredByMethods: number;
|
|
522
|
+
filteredByOperationIds: number;
|
|
523
|
+
filteredByDeprecated: number;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Create a new filter statistics object with all counters initialized to zero
|
|
527
|
+
*/
|
|
528
|
+
declare function createFilterStatistics(): FilterStatistics;
|
|
529
|
+
/**
|
|
530
|
+
* Determine if an operation should be included based on filter criteria
|
|
531
|
+
*
|
|
532
|
+
* Filter logic:
|
|
533
|
+
* 1. If no filters specified, include all operations
|
|
534
|
+
* 2. Empty arrays are treated as "no constraint" (not as "exclude all")
|
|
535
|
+
* 3. Include filters are applied first (allowlist)
|
|
536
|
+
* 4. Exclude filters are applied second (blocklist)
|
|
537
|
+
* 5. Exclude rules always win over include rules
|
|
538
|
+
*
|
|
539
|
+
* @param operation - The OpenAPI operation object
|
|
540
|
+
* @param path - The operation path (e.g., "/users/{id}")
|
|
541
|
+
* @param method - The HTTP method (e.g., "get", "post")
|
|
542
|
+
* @param filters - Optional filter configuration
|
|
543
|
+
* @param stats - Optional statistics object to track filtering reasons
|
|
544
|
+
* @returns true if the operation should be included, false otherwise
|
|
545
|
+
*/
|
|
546
|
+
declare function shouldIncludeOperation(operation: any, path: string, method: string, filters?: OperationFilters, stats?: FilterStatistics): boolean;
|
|
547
|
+
/**
|
|
548
|
+
* Validate filter statistics and emit warnings for filters that matched nothing
|
|
549
|
+
* Helps users debug filter configurations that might be too restrictive or contain typos
|
|
550
|
+
*
|
|
551
|
+
* @param stats - Filter statistics object
|
|
552
|
+
* @param filters - The filter configuration to validate
|
|
553
|
+
*/
|
|
554
|
+
declare function validateFilters(stats: FilterStatistics, filters?: OperationFilters): void;
|
|
555
|
+
/**
|
|
556
|
+
* Format filter statistics for display in generated output
|
|
557
|
+
* Returns a formatted string suitable for inclusion in comments
|
|
558
|
+
*
|
|
559
|
+
* @param stats - Filter statistics object
|
|
560
|
+
* @returns Formatted statistics string
|
|
561
|
+
*/
|
|
562
|
+
declare function formatFilterStatistics(stats: FilterStatistics): string;
|
|
563
|
+
|
|
564
|
+
export { CircularReferenceError, CliOptionsError, type ConfigFile, ConfigValidationError, type ExecutionMode, FileOperationError, type FilterStatistics, GeneratorError, type OpenAPISpec, OpenApiGenerator, type OpenApiGeneratorOptions, type OperationFilters, SchemaGenerationError, SpecValidationError, createFilterStatistics, defineConfig, formatFilterStatistics, shouldIncludeOperation, validateFilters };
|
package/dist/index.d.ts
CHANGED
|
@@ -113,7 +113,7 @@ interface RequestOptions extends CommonSchemaOptions {
|
|
|
113
113
|
*/
|
|
114
114
|
interface ResponseOptions extends CommonSchemaOptions {
|
|
115
115
|
}
|
|
116
|
-
interface
|
|
116
|
+
interface OpenApiGeneratorOptions {
|
|
117
117
|
/**
|
|
118
118
|
* Object validation mode
|
|
119
119
|
* - 'strict': Uses z.strictObject() - no additional properties allowed
|
|
@@ -187,6 +187,98 @@ interface GeneratorOptions {
|
|
|
187
187
|
* Always generates Zod schemas for runtime validation
|
|
188
188
|
*/
|
|
189
189
|
response?: ResponseOptions;
|
|
190
|
+
/**
|
|
191
|
+
* Filter which operations to include/exclude from generation
|
|
192
|
+
* Useful for generating separate schemas for different API subsets
|
|
193
|
+
*
|
|
194
|
+
* Filtering logic:
|
|
195
|
+
* 1. If no filters specified, all operations are included
|
|
196
|
+
* 2. Empty arrays are treated as "no constraint" (not as "exclude all")
|
|
197
|
+
* 3. Include filters are applied first (allowlist)
|
|
198
|
+
* 4. Exclude filters are applied second (blocklist)
|
|
199
|
+
* 5. Exclude rules always win over include rules
|
|
200
|
+
*
|
|
201
|
+
* Supports glob patterns for paths and operationIds (e.g., "/api/v1/**", "get*")
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* // Only generate schemas for user-related endpoints
|
|
205
|
+
* operationFilters: {
|
|
206
|
+
* includeTags: ["users"]
|
|
207
|
+
* }
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* // Generate only GET endpoints, excluding deprecated ones
|
|
211
|
+
* operationFilters: {
|
|
212
|
+
* includeMethods: ["get"],
|
|
213
|
+
* excludeDeprecated: true
|
|
214
|
+
* }
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* // Generate only v1 API endpoints
|
|
218
|
+
* operationFilters: {
|
|
219
|
+
* includePaths: ["/api/v1/**"]
|
|
220
|
+
* }
|
|
221
|
+
*/
|
|
222
|
+
operationFilters?: OperationFilters;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Operation filtering options
|
|
226
|
+
* Controls which operations from the OpenAPI spec are included in generation
|
|
227
|
+
*/
|
|
228
|
+
interface OperationFilters {
|
|
229
|
+
/**
|
|
230
|
+
* Include only operations with these tags
|
|
231
|
+
* If specified, only operations with at least one matching tag are included
|
|
232
|
+
* Empty array = no constraint
|
|
233
|
+
*/
|
|
234
|
+
includeTags?: string[];
|
|
235
|
+
/**
|
|
236
|
+
* Exclude operations with these tags
|
|
237
|
+
* Operations with any matching tag are excluded
|
|
238
|
+
* Empty array = no constraint
|
|
239
|
+
*/
|
|
240
|
+
excludeTags?: string[];
|
|
241
|
+
/**
|
|
242
|
+
* Include only operations matching these path patterns
|
|
243
|
+
* Supports glob patterns (e.g., "/users/**", "/api/v1/*")
|
|
244
|
+
* Empty array = no constraint
|
|
245
|
+
*/
|
|
246
|
+
includePaths?: string[];
|
|
247
|
+
/**
|
|
248
|
+
* Exclude operations matching these path patterns
|
|
249
|
+
* Supports glob patterns (e.g., "/internal/**", "/admin/*")
|
|
250
|
+
* Empty array = no constraint
|
|
251
|
+
*/
|
|
252
|
+
excludePaths?: string[];
|
|
253
|
+
/**
|
|
254
|
+
* Include only these HTTP methods
|
|
255
|
+
* Valid values: "get", "post", "put", "patch", "delete", "head", "options"
|
|
256
|
+
* Empty array = no constraint
|
|
257
|
+
*/
|
|
258
|
+
includeMethods?: string[];
|
|
259
|
+
/**
|
|
260
|
+
* Exclude these HTTP methods
|
|
261
|
+
* Valid values: "get", "post", "put", "patch", "delete", "head", "options"
|
|
262
|
+
* Empty array = no constraint
|
|
263
|
+
*/
|
|
264
|
+
excludeMethods?: string[];
|
|
265
|
+
/**
|
|
266
|
+
* Include only operations matching these operationId patterns
|
|
267
|
+
* Supports glob patterns (e.g., "getUser*", "*Admin")
|
|
268
|
+
* Empty array = no constraint
|
|
269
|
+
*/
|
|
270
|
+
includeOperationIds?: string[];
|
|
271
|
+
/**
|
|
272
|
+
* Exclude operations matching these operationId patterns
|
|
273
|
+
* Supports glob patterns (e.g., "deleteUser*", "*Internal")
|
|
274
|
+
* Empty array = no constraint
|
|
275
|
+
*/
|
|
276
|
+
excludeOperationIds?: string[];
|
|
277
|
+
/**
|
|
278
|
+
* Whether to exclude deprecated operations
|
|
279
|
+
* @default false
|
|
280
|
+
*/
|
|
281
|
+
excludeDeprecated?: boolean;
|
|
190
282
|
}
|
|
191
283
|
interface OpenAPISchema {
|
|
192
284
|
type?: string | string[];
|
|
@@ -263,12 +355,12 @@ interface ConfigFile {
|
|
|
263
355
|
* Global default options applied to all specs
|
|
264
356
|
* Can be overridden by individual spec configurations
|
|
265
357
|
*/
|
|
266
|
-
defaults?: Partial<Omit<
|
|
358
|
+
defaults?: Partial<Omit<OpenApiGeneratorOptions, "input" | "output">>;
|
|
267
359
|
/**
|
|
268
360
|
* Array of OpenAPI specifications to process
|
|
269
361
|
* Each spec must have input and output paths
|
|
270
362
|
*/
|
|
271
|
-
specs:
|
|
363
|
+
specs: OpenApiGeneratorOptions[];
|
|
272
364
|
/**
|
|
273
365
|
* Execution mode for batch processing
|
|
274
366
|
* @default "parallel"
|
|
@@ -297,7 +389,7 @@ interface ConfigFile {
|
|
|
297
389
|
*/
|
|
298
390
|
declare function defineConfig(config: ConfigFile): ConfigFile;
|
|
299
391
|
|
|
300
|
-
declare class
|
|
392
|
+
declare class OpenApiGenerator {
|
|
301
393
|
private schemas;
|
|
302
394
|
private types;
|
|
303
395
|
private enums;
|
|
@@ -311,7 +403,8 @@ declare class ZodSchemaGenerator {
|
|
|
311
403
|
private requestOptions;
|
|
312
404
|
private responseOptions;
|
|
313
405
|
private needsZodImport;
|
|
314
|
-
|
|
406
|
+
private filterStats;
|
|
407
|
+
constructor(options: OpenApiGeneratorOptions);
|
|
315
408
|
/**
|
|
316
409
|
* Generate schemas as a string (without writing to file)
|
|
317
410
|
* @returns The generated TypeScript code as a string
|
|
@@ -377,6 +470,11 @@ declare class ZodSchemaGenerator {
|
|
|
377
470
|
* Generate query parameter schemas for each operation
|
|
378
471
|
*/
|
|
379
472
|
private generateQueryParameterSchemas;
|
|
473
|
+
/**
|
|
474
|
+
* Generate header parameter schemas for each operation
|
|
475
|
+
* Header parameters are always string type (HTTP header semantics)
|
|
476
|
+
*/
|
|
477
|
+
private generateHeaderParameterSchemas;
|
|
380
478
|
/**
|
|
381
479
|
* Generate Zod type for a query parameter schema
|
|
382
480
|
*/
|
|
@@ -412,4 +510,55 @@ declare class ZodSchemaGenerator {
|
|
|
412
510
|
private generateStats;
|
|
413
511
|
}
|
|
414
512
|
|
|
415
|
-
|
|
513
|
+
/**
|
|
514
|
+
* Filter statistics to track which operations were included/excluded
|
|
515
|
+
*/
|
|
516
|
+
interface FilterStatistics {
|
|
517
|
+
totalOperations: number;
|
|
518
|
+
includedOperations: number;
|
|
519
|
+
filteredByTags: number;
|
|
520
|
+
filteredByPaths: number;
|
|
521
|
+
filteredByMethods: number;
|
|
522
|
+
filteredByOperationIds: number;
|
|
523
|
+
filteredByDeprecated: number;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Create a new filter statistics object with all counters initialized to zero
|
|
527
|
+
*/
|
|
528
|
+
declare function createFilterStatistics(): FilterStatistics;
|
|
529
|
+
/**
|
|
530
|
+
* Determine if an operation should be included based on filter criteria
|
|
531
|
+
*
|
|
532
|
+
* Filter logic:
|
|
533
|
+
* 1. If no filters specified, include all operations
|
|
534
|
+
* 2. Empty arrays are treated as "no constraint" (not as "exclude all")
|
|
535
|
+
* 3. Include filters are applied first (allowlist)
|
|
536
|
+
* 4. Exclude filters are applied second (blocklist)
|
|
537
|
+
* 5. Exclude rules always win over include rules
|
|
538
|
+
*
|
|
539
|
+
* @param operation - The OpenAPI operation object
|
|
540
|
+
* @param path - The operation path (e.g., "/users/{id}")
|
|
541
|
+
* @param method - The HTTP method (e.g., "get", "post")
|
|
542
|
+
* @param filters - Optional filter configuration
|
|
543
|
+
* @param stats - Optional statistics object to track filtering reasons
|
|
544
|
+
* @returns true if the operation should be included, false otherwise
|
|
545
|
+
*/
|
|
546
|
+
declare function shouldIncludeOperation(operation: any, path: string, method: string, filters?: OperationFilters, stats?: FilterStatistics): boolean;
|
|
547
|
+
/**
|
|
548
|
+
* Validate filter statistics and emit warnings for filters that matched nothing
|
|
549
|
+
* Helps users debug filter configurations that might be too restrictive or contain typos
|
|
550
|
+
*
|
|
551
|
+
* @param stats - Filter statistics object
|
|
552
|
+
* @param filters - The filter configuration to validate
|
|
553
|
+
*/
|
|
554
|
+
declare function validateFilters(stats: FilterStatistics, filters?: OperationFilters): void;
|
|
555
|
+
/**
|
|
556
|
+
* Format filter statistics for display in generated output
|
|
557
|
+
* Returns a formatted string suitable for inclusion in comments
|
|
558
|
+
*
|
|
559
|
+
* @param stats - Filter statistics object
|
|
560
|
+
* @returns Formatted statistics string
|
|
561
|
+
*/
|
|
562
|
+
declare function formatFilterStatistics(stats: FilterStatistics): string;
|
|
563
|
+
|
|
564
|
+
export { CircularReferenceError, CliOptionsError, type ConfigFile, ConfigValidationError, type ExecutionMode, FileOperationError, type FilterStatistics, GeneratorError, type OpenAPISpec, OpenApiGenerator, type OpenApiGeneratorOptions, type OperationFilters, SchemaGenerationError, SpecValidationError, createFilterStatistics, defineConfig, formatFilterStatistics, shouldIncludeOperation, validateFilters };
|