zod-openapi 5.0.0-beta.5 → 5.0.0-beta.6

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.
Files changed (50) hide show
  1. package/README.md +71 -208
  2. package/api/index.d.ts +1 -1
  3. package/dist/api.d.mts +22 -3
  4. package/dist/api.d.ts +22 -3
  5. package/dist/api.js +8 -0
  6. package/dist/api.mjs +4 -8
  7. package/dist/components-5_CJdR73.d.ts +543 -0
  8. package/dist/components-CXjVnBr-.js +782 -0
  9. package/dist/components-CvutxtFV.mjs +741 -0
  10. package/dist/components-DAYTA1Um.d.mts +543 -0
  11. package/dist/create/componentsSideEffects.d.mts +6 -0
  12. package/dist/create/componentsSideEffects.d.ts +6 -0
  13. package/dist/create/componentsSideEffects.js +48 -0
  14. package/dist/create/componentsSideEffects.mjs +48 -0
  15. package/dist/index.d.mts +23 -7
  16. package/dist/index.d.ts +23 -7
  17. package/dist/index.js +20 -0
  18. package/dist/index.mjs +18 -59
  19. package/dist/zod-BvA30wad.mjs +5 -0
  20. package/dist/zod-i2t01GF0.js +40 -0
  21. package/package.json +14 -32
  22. package/dist/api.cjs +0 -8
  23. package/dist/components.chunk.cjs +0 -2463
  24. package/dist/components.chunk.mjs +0 -2464
  25. package/dist/create/components.d.ts +0 -125
  26. package/dist/create/content.d.ts +0 -8
  27. package/dist/create/document.d.ts +0 -91
  28. package/dist/create/parameters.d.ts +0 -9
  29. package/dist/create/schema/single.d.ts +0 -33
  30. package/dist/extend.cjs +0 -4
  31. package/dist/extend.d.mts +0 -1
  32. package/dist/extend.d.ts +0 -1
  33. package/dist/extend.mjs +0 -3
  34. package/dist/extendZod.chunk.cjs +0 -95
  35. package/dist/extendZod.chunk.mjs +0 -96
  36. package/dist/extendZod.d.ts +0 -6
  37. package/dist/extendZodSymbols.chunk.cjs +0 -5
  38. package/dist/extendZodSymbols.chunk.mjs +0 -6
  39. package/dist/extendZodSymbols.d.ts +0 -4
  40. package/dist/extendZodTypes.d.ts +0 -91
  41. package/dist/index.cjs +0 -60
  42. package/dist/openapi.d.ts +0 -5
  43. package/dist/openapi3-ts/dist/model/oas-common.d.ts +0 -16
  44. package/dist/openapi3-ts/dist/model/openapi30.d.ts +0 -291
  45. package/dist/openapi3-ts/dist/model/openapi31.d.ts +0 -298
  46. package/dist/openapi3-ts/dist/model/specification-extension.d.ts +0 -7
  47. package/dist/openapi3-ts/dist/oas30.d.ts +0 -3
  48. package/dist/openapi3-ts/dist/oas31.d.ts +0 -3
  49. package/extend/index.d.ts +0 -1
  50. package/extend/package.json +0 -5
@@ -1,2463 +0,0 @@
1
- "use strict";
2
- const extendZodSymbols = require("./extendZodSymbols.chunk.cjs");
3
- const isZodType = (zodType, typeName) => {
4
- var _a;
5
- return ((_a = zodType == null ? void 0 : zodType._def) == null ? void 0 : _a.typeName) === typeName;
6
- };
7
- const isAnyZodType = (zodType) => {
8
- var _a;
9
- return Boolean(
10
- (_a = zodType == null ? void 0 : zodType._def) == null ? void 0 : _a.typeName
11
- );
12
- };
13
- const openApiVersions = [
14
- "3.0.0",
15
- "3.0.1",
16
- "3.0.2",
17
- "3.0.3",
18
- "3.1.0",
19
- "3.1.1"
20
- ];
21
- const satisfiesVersion = (test, against) => openApiVersions.indexOf(test) >= openApiVersions.indexOf(against);
22
- const createDescriptionMetadata = (schema, description, state) => {
23
- if (satisfiesVersion(state.components.openapi, "3.1.0")) {
24
- return {
25
- type: "ref",
26
- schema: {
27
- $ref: schema.schema.$ref,
28
- description
29
- },
30
- zodType: schema.zodType,
31
- effects: schema.effects,
32
- schemaObject: schema.schemaObject
33
- };
34
- }
35
- return {
36
- type: "schema",
37
- schema: {
38
- description,
39
- allOf: [schema.schema]
40
- },
41
- effects: schema.effects
42
- };
43
- };
44
- const isValueEqual = (value, previous) => {
45
- if (typeof value !== typeof previous) {
46
- return false;
47
- }
48
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
49
- return value === previous;
50
- }
51
- if (Array.isArray(value) && Array.isArray(previous)) {
52
- const sorted = [...value].sort();
53
- const previousSorted = [...previous].sort();
54
- return sorted.every((v, i) => isValueEqual(v, previousSorted[i]));
55
- }
56
- if (value === null || previous === null) {
57
- return value === previous;
58
- }
59
- if (typeof value === "object" && typeof previous === "object") {
60
- const keys = Object.keys(value);
61
- return keys.every(
62
- (key) => isValueEqual(
63
- value[key],
64
- previous[key]
65
- )
66
- );
67
- }
68
- return value === previous;
69
- };
70
- const enhanceWithMetadata = (schema, metadata, state, previous) => {
71
- const values = Object.entries(metadata).reduce(
72
- (acc, [key, value]) => {
73
- if (value === void 0) {
74
- return acc;
75
- }
76
- acc[key] = value;
77
- return acc;
78
- },
79
- {}
80
- );
81
- const length = Object.values(values).length;
82
- if (schema.type === "ref") {
83
- if (length === 0) {
84
- return schema;
85
- }
86
- if (length === 1 && metadata.description) {
87
- return createDescriptionMetadata(schema, metadata.description, state);
88
- }
89
- return {
90
- type: "schema",
91
- schema: {
92
- allOf: [schema.schema],
93
- ...metadata
94
- },
95
- effects: schema.effects
96
- };
97
- }
98
- if (previous && schema.schema.type !== "object") {
99
- const diff = Object.entries({ ...schema.schema, ...values }).reduce(
100
- (acc, [key, value]) => {
101
- if (previous.schemaObject && isValueEqual(
102
- previous.schemaObject[key],
103
- value
104
- )) {
105
- return acc;
106
- }
107
- acc[key] = value;
108
- return acc;
109
- },
110
- {}
111
- );
112
- const diffLength = Object.values(diff).length;
113
- if (diffLength === 0) {
114
- return {
115
- type: "ref",
116
- schema: {
117
- $ref: previous.schema.$ref
118
- },
119
- effects: schema.effects,
120
- schemaObject: previous.schemaObject,
121
- zodType: previous.zodType
122
- };
123
- }
124
- if (diffLength === 1 && typeof diff.description === "string") {
125
- return createDescriptionMetadata(previous, diff.description, state);
126
- }
127
- return {
128
- type: "schema",
129
- schema: { allOf: [previous.schema], ...diff },
130
- effects: schema.effects
131
- };
132
- }
133
- return {
134
- type: "schema",
135
- schema: {
136
- ...schema.schema,
137
- ...metadata
138
- },
139
- effects: schema.effects
140
- };
141
- };
142
- const createArraySchema = (zodArray, state) => {
143
- var _a, _b, _c, _d;
144
- const zodType = zodArray._def.type;
145
- const minItems = ((_a = zodArray._def.exactLength) == null ? void 0 : _a.value) ?? ((_b = zodArray._def.minLength) == null ? void 0 : _b.value);
146
- const maxItems = ((_c = zodArray._def.exactLength) == null ? void 0 : _c.value) ?? ((_d = zodArray._def.maxLength) == null ? void 0 : _d.value);
147
- const items = createSchemaObject(zodType, state, ["array items"]);
148
- return {
149
- type: "schema",
150
- schema: {
151
- type: "array",
152
- items: items.schema,
153
- ...minItems !== void 0 && { minItems },
154
- ...maxItems !== void 0 && { maxItems }
155
- },
156
- effects: items.effects
157
- };
158
- };
159
- const createBigIntSchema = (_zodBigInt) => ({
160
- type: "schema",
161
- schema: {
162
- type: "integer",
163
- format: "int64"
164
- }
165
- });
166
- const createBooleanSchema = (_zodBoolean) => ({
167
- type: "schema",
168
- schema: {
169
- type: "boolean"
170
- }
171
- });
172
- const createBrandedSchema = (zodBranded, state) => createSchemaObject(zodBranded._def.type, state, ["brand"]);
173
- const createCatchSchema = (zodCatch, state, previous) => {
174
- const schemaObject = createSchemaObject(zodCatch._def.innerType, state, [
175
- "default"
176
- ]);
177
- const catchResult = zodCatch.safeParse(void 0);
178
- const maybeDefaultValue = catchResult.success ? {
179
- default: catchResult.data
180
- } : {};
181
- return enhanceWithMetadata(schemaObject, maybeDefaultValue, state, previous);
182
- };
183
- const createDateSchema = (_zodDate, state) => {
184
- var _a;
185
- return {
186
- type: "schema",
187
- schema: ((_a = state.documentOptions) == null ? void 0 : _a.defaultDateSchema) ?? {
188
- type: "string"
189
- }
190
- };
191
- };
192
- const createDefaultSchema = (zodDefault, state, previous) => {
193
- const schemaObject = createSchemaObject(zodDefault._def.innerType, state, [
194
- "default"
195
- ]);
196
- return enhanceWithMetadata(
197
- schemaObject,
198
- {
199
- default: zodDefault._def.defaultValue()
200
- },
201
- state,
202
- previous
203
- );
204
- };
205
- const createNativeEnumSchema = (zodEnum, state) => {
206
- const enumValues = getValidEnumValues(zodEnum._def.values);
207
- const { numbers, strings } = sortStringsAndNumbers(enumValues);
208
- if (strings.length && numbers.length) {
209
- if (satisfiesVersion(state.components.openapi, "3.1.0")) {
210
- return {
211
- type: "schema",
212
- schema: {
213
- type: ["string", "number"],
214
- enum: [...strings, ...numbers]
215
- }
216
- };
217
- }
218
- return {
219
- type: "schema",
220
- schema: {
221
- oneOf: [
222
- { type: "string", enum: strings },
223
- { type: "number", enum: numbers }
224
- ]
225
- }
226
- };
227
- }
228
- if (strings.length) {
229
- return {
230
- type: "schema",
231
- schema: {
232
- type: "string",
233
- enum: strings
234
- }
235
- };
236
- }
237
- return {
238
- type: "schema",
239
- schema: {
240
- type: "number",
241
- enum: numbers
242
- }
243
- };
244
- };
245
- const getValidEnumValues = (enumValues) => {
246
- const keys = Object.keys(enumValues).filter(
247
- (key) => typeof enumValues[enumValues[key]] !== "number"
248
- );
249
- return keys.map((key) => enumValues[key]);
250
- };
251
- const sortStringsAndNumbers = (values) => ({
252
- strings: values.filter((value) => typeof value === "string"),
253
- numbers: values.filter((value) => typeof value === "number")
254
- });
255
- const createTransformSchema = (zodTransform, state) => {
256
- var _a, _b, _c, _d, _e, _f;
257
- if (((_b = (_a = zodTransform._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.effectType) === "output") {
258
- return {
259
- type: "schema",
260
- schema: createManualOutputTransformSchema(zodTransform, state)
261
- };
262
- }
263
- if (((_d = (_c = zodTransform._def.zodOpenApi) == null ? void 0 : _c.openapi) == null ? void 0 : _d.effectType) === "input" || ((_f = (_e = zodTransform._def.zodOpenApi) == null ? void 0 : _e.openapi) == null ? void 0 : _f.effectType) === "same") {
264
- return createSchemaObject(zodTransform._def.schema, state, [
265
- "transform input"
266
- ]);
267
- }
268
- if (state.type === "output") {
269
- return {
270
- type: "schema",
271
- schema: createManualOutputTransformSchema(zodTransform, state)
272
- };
273
- }
274
- const schema = createSchemaObject(zodTransform._def.schema, state, [
275
- "transform input"
276
- ]);
277
- return {
278
- ...schema,
279
- effects: flattenEffects([
280
- [
281
- {
282
- type: "schema",
283
- creationType: "input",
284
- zodType: zodTransform,
285
- path: [...state.path]
286
- }
287
- ],
288
- schema.effects
289
- ])
290
- };
291
- };
292
- const createManualOutputTransformSchema = (zodTransform, state) => {
293
- var _a, _b, _c;
294
- if (!((_b = (_a = zodTransform._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.type)) {
295
- const zodType = zodTransform.constructor.name;
296
- const schemaName = `${zodType} - ${zodTransform._def.effect.type}`;
297
- throw new Error(
298
- `Failed to determine a type for ${schemaName} at ${state.path.join(
299
- " > "
300
- )}. Please change the 'effectType' to 'same' or 'input', wrap it in a ZodPipeline or assign it a manual 'type'.`
301
- );
302
- }
303
- return {
304
- type: (_c = zodTransform._def.zodOpenApi) == null ? void 0 : _c.openapi.type
305
- };
306
- };
307
- const getZodTypeName = (zodType) => {
308
- if (isZodType(zodType, "ZodEffects")) {
309
- return `${zodType._def.typeName} - ${zodType._def.effect.type}`;
310
- }
311
- return zodType._def.typeName;
312
- };
313
- const throwTransformError = (effect) => {
314
- const typeName = getZodTypeName(effect.zodType);
315
- const input = effect.creationType;
316
- const opposite = input === "input" ? "output" : "input";
317
- throw new Error(
318
- `The ${typeName} at ${effect.path.join(
319
- " > "
320
- )} is used within a registered compoment schema${effect.component ? ` (${effect.component.ref})` : ""} and contains an ${input} transformation${effect.component ? ` (${getZodTypeName(
321
- effect.component.zodType
322
- )}) defined at ${effect.component.path.join(" > ")}` : ""} which is also used in an ${opposite} schema.
323
-
324
- This may cause the schema to render incorrectly and is most likely a mistake. You can resolve this by:
325
-
326
- 1. Setting an \`effectType\` on one of the transformations to \`same\` (Not applicable for ZodDefault), \`input\` or \`output\` eg. \`.openapi({type: 'same'})\`
327
- 2. Wrapping the transformation in a ZodPipeline
328
- 3. Assigning a manual type to the transformation eg. \`.openapi({type: 'string'})\`
329
- 4. Removing the transformation
330
- 5. Deregister the component containing the transformation`
331
- );
332
- };
333
- const resolveSingleEffect = (effect, state) => {
334
- if (effect.type === "schema") {
335
- return {
336
- creationType: effect.creationType,
337
- path: effect.path,
338
- zodType: effect.zodType
339
- };
340
- }
341
- if (effect.type === "component") {
342
- if (state.visited.has(effect.zodType)) {
343
- return;
344
- }
345
- const component = state.components.schemas.get(effect.zodType);
346
- if ((component == null ? void 0 : component.type) !== "complete") {
347
- throw new Error("Something went wrong, component schema is not complete");
348
- }
349
- if (component.resolvedEffect) {
350
- return {
351
- creationType: component.resolvedEffect.creationType,
352
- path: effect.path,
353
- zodType: effect.zodType,
354
- component: {
355
- ref: component.ref,
356
- zodType: component.resolvedEffect.zodType,
357
- path: component.resolvedEffect.path
358
- }
359
- };
360
- }
361
- if (!component.effects) {
362
- return void 0;
363
- }
364
- state.visited.add(effect.zodType);
365
- const resolved = resolveEffect(component.effects, state);
366
- state.visited.delete(effect.zodType);
367
- if (!resolved) {
368
- return void 0;
369
- }
370
- component.resolvedEffect = resolved;
371
- return resolved;
372
- }
373
- return void 0;
374
- };
375
- const resolveEffect = (effects, state) => {
376
- const { input, output } = effects.reduce(
377
- (acc, effect) => {
378
- const resolvedSchemaEffect = resolveSingleEffect(effect, state);
379
- if ((resolvedSchemaEffect == null ? void 0 : resolvedSchemaEffect.creationType) === "input") {
380
- acc.input.push(resolvedSchemaEffect);
381
- }
382
- if ((resolvedSchemaEffect == null ? void 0 : resolvedSchemaEffect.creationType) === "output") {
383
- acc.output.push(resolvedSchemaEffect);
384
- }
385
- if (resolvedSchemaEffect && acc.input.length > 1 && acc.output.length > 1) {
386
- throwTransformError(resolvedSchemaEffect);
387
- }
388
- return acc;
389
- },
390
- { input: [], output: [] }
391
- );
392
- if (input.length > 0) {
393
- return input[0];
394
- }
395
- if (output.length > 0) {
396
- return output[0];
397
- }
398
- return void 0;
399
- };
400
- const verifyEffects = (effects, state) => {
401
- const resolved = resolveEffect(effects, state);
402
- if ((resolved == null ? void 0 : resolved.creationType) && resolved.creationType !== state.type) {
403
- throwTransformError(resolved);
404
- }
405
- };
406
- const flattenEffects = (effects) => {
407
- const allEffects = effects.reduce((acc, effect) => {
408
- if (effect) {
409
- return acc.concat(effect);
410
- }
411
- return acc;
412
- }, []);
413
- return allEffects.length ? allEffects : void 0;
414
- };
415
- const createDiscriminatedUnionSchema = (zodDiscriminatedUnion, state) => {
416
- const options = zodDiscriminatedUnion.options;
417
- const schemas = options.map(
418
- (option, index) => createSchemaObject(option, state, [`discriminated union option ${index}`])
419
- );
420
- const schemaObjects = schemas.map((schema) => schema.schema);
421
- const discriminator = mapDiscriminator(
422
- schemaObjects,
423
- options,
424
- zodDiscriminatedUnion.discriminator,
425
- state
426
- );
427
- return {
428
- type: "schema",
429
- schema: {
430
- oneOf: schemaObjects,
431
- ...discriminator && { discriminator }
432
- },
433
- effects: flattenEffects(schemas.map((schema) => schema.effects))
434
- };
435
- };
436
- const unwrapLiterals = (zodType, state) => {
437
- if (isZodType(zodType, "ZodLiteral")) {
438
- if (typeof zodType._def.value !== "string") {
439
- return void 0;
440
- }
441
- return [zodType._def.value];
442
- }
443
- if (isZodType(zodType, "ZodNativeEnum")) {
444
- const schema = createNativeEnumSchema(zodType, state);
445
- if (schema.type === "schema" && schema.schema.type === "string") {
446
- return schema.schema.enum;
447
- }
448
- }
449
- if (isZodType(zodType, "ZodEnum")) {
450
- return zodType._def.values;
451
- }
452
- if (isZodType(zodType, "ZodBranded")) {
453
- return unwrapLiterals(zodType._def.type, state);
454
- }
455
- if (isZodType(zodType, "ZodReadonly")) {
456
- return unwrapLiterals(zodType._def.innerType, state);
457
- }
458
- if (isZodType(zodType, "ZodCatch")) {
459
- return unwrapLiterals(zodType._def.innerType, state);
460
- }
461
- return void 0;
462
- };
463
- const mapDiscriminator = (schemas, zodObjects, discriminator, state) => {
464
- var _a;
465
- if (typeof discriminator !== "string") {
466
- return void 0;
467
- }
468
- const mapping = {};
469
- for (const [index, zodObject] of zodObjects.entries()) {
470
- const schema = schemas[index];
471
- const componentSchemaRef = "$ref" in schema ? schema == null ? void 0 : schema.$ref : void 0;
472
- if (!componentSchemaRef) {
473
- if ((_a = state.documentOptions) == null ? void 0 : _a.enforceDiscriminatedUnionComponents) {
474
- throw new Error(
475
- `Discriminated Union member ${index} at ${state.path.join(" > ")} is not registered as a component`
476
- );
477
- }
478
- return void 0;
479
- }
480
- const value = zodObject.shape[discriminator];
481
- const literals = unwrapLiterals(value, state);
482
- if (!literals) {
483
- return void 0;
484
- }
485
- for (const enumValue of literals) {
486
- mapping[enumValue] = componentSchemaRef;
487
- }
488
- }
489
- return {
490
- propertyName: discriminator,
491
- mapping
492
- };
493
- };
494
- const createEnumSchema = (zodEnum) => ({
495
- type: "schema",
496
- schema: {
497
- type: "string",
498
- enum: zodEnum._def.values
499
- }
500
- });
501
- const createIntersectionSchema = (zodIntersection, state) => {
502
- const schemas = flattenIntersection(zodIntersection);
503
- const allOfs = schemas.map(
504
- (schema, index) => createSchemaObject(schema, state, [`intersection ${index}`])
505
- );
506
- return {
507
- type: "schema",
508
- schema: {
509
- allOf: allOfs.map((schema) => schema.schema)
510
- },
511
- effects: flattenEffects(allOfs.map((schema) => schema.effects))
512
- };
513
- };
514
- const flattenIntersection = (zodType) => {
515
- if (!isZodType(zodType, "ZodIntersection")) {
516
- return [zodType];
517
- }
518
- const leftSchemas = flattenIntersection(zodType._def.left);
519
- const rightSchemas = flattenIntersection(zodType._def.right);
520
- return [...leftSchemas, ...rightSchemas];
521
- };
522
- const createLazySchema = (zodLazy, state) => {
523
- const innerSchema = zodLazy._def.getter();
524
- return createSchemaObject(innerSchema, state, ["lazy schema"]);
525
- };
526
- const createNullSchema = () => ({
527
- type: "schema",
528
- schema: {
529
- type: "null"
530
- }
531
- });
532
- const createLiteralSchema = (zodLiteral, state) => {
533
- if (zodLiteral.value === null) {
534
- return createNullSchema();
535
- }
536
- if (satisfiesVersion(state.components.openapi, "3.1.0")) {
537
- return {
538
- type: "schema",
539
- schema: {
540
- type: typeof zodLiteral.value,
541
- const: zodLiteral.value
542
- }
543
- };
544
- }
545
- return {
546
- type: "schema",
547
- schema: {
548
- type: typeof zodLiteral.value,
549
- enum: [zodLiteral.value]
550
- }
551
- };
552
- };
553
- const createManualTypeSchema = (zodSchema, state) => {
554
- var _a, _b, _c;
555
- if (!((_b = (_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.type)) {
556
- const schemaName = zodSchema.constructor.name;
557
- throw new Error(
558
- `Unknown schema ${schemaName} at ${state.path.join(
559
- " > "
560
- )}. Please assign it a manual 'type'.`
561
- );
562
- }
563
- return {
564
- type: "schema",
565
- schema: {
566
- type: (_c = zodSchema._def.zodOpenApi) == null ? void 0 : _c.openapi.type
567
- }
568
- };
569
- };
570
- const createNullableSchema = (zodNullable, state) => {
571
- const schemaObject = createSchemaObject(zodNullable.unwrap(), state, [
572
- "nullable"
573
- ]);
574
- if (satisfiesVersion(state.components.openapi, "3.1.0")) {
575
- if (schemaObject.type === "ref" || schemaObject.schema.allOf) {
576
- return {
577
- type: "schema",
578
- schema: {
579
- oneOf: mapNullOf([schemaObject.schema], state.components.openapi)
580
- },
581
- effects: schemaObject.effects
582
- };
583
- }
584
- if (schemaObject.schema.oneOf) {
585
- const { oneOf, ...schema3 } = schemaObject.schema;
586
- return {
587
- type: "schema",
588
- schema: {
589
- oneOf: mapNullOf(oneOf, state.components.openapi),
590
- ...schema3
591
- },
592
- effects: schemaObject.effects
593
- };
594
- }
595
- if (schemaObject.schema.anyOf) {
596
- const { anyOf, ...schema3 } = schemaObject.schema;
597
- return {
598
- type: "schema",
599
- schema: {
600
- anyOf: mapNullOf(anyOf, state.components.openapi),
601
- ...schema3
602
- },
603
- effects: schemaObject.effects
604
- };
605
- }
606
- const { type: type2, const: schemaConst, ...schema2 } = schemaObject.schema;
607
- if (schemaConst) {
608
- return {
609
- type: "schema",
610
- schema: {
611
- type: mapNullType(type2),
612
- enum: [schemaConst, null],
613
- ...schema2
614
- },
615
- effects: schemaObject.effects
616
- };
617
- }
618
- return {
619
- type: "schema",
620
- schema: {
621
- type: mapNullType(type2),
622
- ...schema2,
623
- // https://github.com/json-schema-org/json-schema-spec/issues/258
624
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
625
- ...schema2.enum && { enum: [...schema2.enum, null] }
626
- },
627
- effects: schemaObject.effects
628
- };
629
- }
630
- if (schemaObject.type === "ref") {
631
- return {
632
- type: "schema",
633
- schema: {
634
- allOf: [schemaObject.schema],
635
- nullable: true
636
- },
637
- effects: schemaObject.effects
638
- };
639
- }
640
- const { type, ...schema } = schemaObject.schema;
641
- return {
642
- type: "schema",
643
- schema: {
644
- ...type && { type },
645
- nullable: true,
646
- ...schema,
647
- // https://github.com/OAI/OpenAPI-Specification/blob/main/proposals/2019-10-31-Clarify-Nullable.md#if-a-schema-specifies-nullable-true-and-enum-1-2-3-does-that-schema-allow-null-values-see-1900
648
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
649
- ...schema.enum && { enum: [...schema.enum, null] }
650
- },
651
- effects: schemaObject.effects
652
- };
653
- };
654
- const mapNullType = (type) => {
655
- if (!type) {
656
- return "null";
657
- }
658
- if (Array.isArray(type)) {
659
- return [...type, "null"];
660
- }
661
- return [type, "null"];
662
- };
663
- const mapNullOf = (ofSchema, openapi) => {
664
- if (satisfiesVersion(openapi, "3.1.0")) {
665
- return [...ofSchema, { type: "null" }];
666
- }
667
- return [...ofSchema, { nullable: true }];
668
- };
669
- const createNumberSchema = (zodNumber, state) => {
670
- const zodNumberChecks = getZodNumberChecks(zodNumber);
671
- const minimum = mapMinimum(zodNumberChecks, state.components.openapi);
672
- const maximum = mapMaximum(zodNumberChecks, state.components.openapi);
673
- const multipleOf = mapMultipleOf(zodNumberChecks);
674
- return {
675
- type: "schema",
676
- schema: {
677
- type: mapNumberType(zodNumberChecks),
678
- ...multipleOf && multipleOf,
679
- ...minimum && minimum,
680
- // Union types are not easy to tame
681
- ...maximum && maximum
682
- }
683
- };
684
- };
685
- const mapMultipleOf = (zodNumberCheck) => zodNumberCheck.multipleOf ? { multipleOf: zodNumberCheck.multipleOf.value } : void 0;
686
- const mapMaximum = (zodNumberCheck, openapi) => {
687
- if (!zodNumberCheck.max) {
688
- return void 0;
689
- }
690
- const maximum = zodNumberCheck.max.value;
691
- if (zodNumberCheck.max.inclusive) {
692
- return { ...maximum !== void 0 && { maximum } };
693
- }
694
- if (satisfiesVersion(openapi, "3.1.0")) {
695
- return { exclusiveMaximum: maximum };
696
- }
697
- return { maximum, exclusiveMaximum: true };
698
- };
699
- const mapMinimum = (zodNumberCheck, openapi) => {
700
- if (!zodNumberCheck.min) {
701
- return void 0;
702
- }
703
- const minimum = zodNumberCheck.min.value;
704
- if (zodNumberCheck.min.inclusive) {
705
- return { ...minimum !== void 0 && { minimum } };
706
- }
707
- if (satisfiesVersion(openapi, "3.1.0")) {
708
- return { exclusiveMinimum: minimum };
709
- }
710
- return { minimum, exclusiveMinimum: true };
711
- };
712
- const getZodNumberChecks = (zodNumber) => zodNumber._def.checks.reduce((acc, check) => {
713
- acc[check.kind] = check;
714
- return acc;
715
- }, {});
716
- const mapNumberType = (zodNumberChecks) => zodNumberChecks.int ? "integer" : "number";
717
- const createOptionalSchema = (zodOptional, state) => createSchemaObject(zodOptional.unwrap(), state, ["optional"]);
718
- const isOptionalObjectKey = (zodSchema) => isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined") || isZodType(zodSchema, "ZodOptional") && isOptionalObjectKey(zodSchema.unwrap()) || isZodType(zodSchema, "ZodLiteral") && zodSchema._def.value === void 0;
719
- const createObjectSchema = (zodObject, previous, state) => {
720
- const extendedSchema = createExtendedSchema(
721
- zodObject,
722
- previous == null ? void 0 : previous.zodType,
723
- state
724
- );
725
- if (extendedSchema) {
726
- return extendedSchema;
727
- }
728
- return createObjectSchemaFromShape(
729
- zodObject.shape,
730
- {
731
- unknownKeys: zodObject._def.unknownKeys,
732
- catchAll: zodObject._def.catchall
733
- },
734
- state
735
- );
736
- };
737
- const createExtendedSchema = (zodObject, baseZodObject, state) => {
738
- var _a, _b, _c, _d, _e;
739
- if (!baseZodObject) {
740
- return void 0;
741
- }
742
- const component = state.components.schemas.get(baseZodObject);
743
- if (component ?? ((_b = (_a = baseZodObject._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.ref)) {
744
- createSchemaObject(baseZodObject, state, ["extended schema"]);
745
- }
746
- const completeComponent = state.components.schemas.get(baseZodObject);
747
- if (!completeComponent) {
748
- return void 0;
749
- }
750
- const diffOpts = createDiffOpts(
751
- {
752
- unknownKeys: baseZodObject._def.unknownKeys,
753
- catchAll: baseZodObject._def.catchall
754
- },
755
- {
756
- unknownKeys: zodObject._def.unknownKeys,
757
- catchAll: zodObject._def.catchall
758
- }
759
- );
760
- if (!diffOpts) {
761
- return void 0;
762
- }
763
- const diffShape = createShapeDiff(
764
- baseZodObject._def.shape(),
765
- zodObject._def.shape()
766
- );
767
- if (!diffShape) {
768
- return void 0;
769
- }
770
- const extendedSchema = createObjectSchemaFromShape(
771
- diffShape,
772
- diffOpts,
773
- state,
774
- true
775
- );
776
- const schemaLength = Object.keys(extendedSchema.schema).length;
777
- const effects = flattenEffects([
778
- completeComponent.type === "complete" ? completeComponent.effects : [],
779
- completeComponent.type === "in-progress" ? [
780
- {
781
- type: "component",
782
- zodType: zodObject,
783
- path: [...state.path]
784
- }
785
- ] : [],
786
- extendedSchema.effects
787
- ]);
788
- if (schemaLength === 0) {
789
- return {
790
- type: "ref",
791
- schema: {
792
- $ref: createComponentSchemaRef(
793
- completeComponent.ref,
794
- (_c = state.documentOptions) == null ? void 0 : _c.componentRefPath
795
- )
796
- },
797
- schemaObject: completeComponent.type === "complete" ? completeComponent.schemaObject : void 0,
798
- zodType: zodObject,
799
- effects
800
- };
801
- }
802
- if (schemaLength === 1 && extendedSchema.schema.description) {
803
- return createDescriptionMetadata(
804
- {
805
- schema: {
806
- $ref: createComponentSchemaRef(
807
- completeComponent.ref,
808
- (_d = state.documentOptions) == null ? void 0 : _d.componentRefPath
809
- )
810
- },
811
- schemaObject: completeComponent.type === "complete" ? completeComponent.schemaObject : void 0,
812
- zodType: zodObject,
813
- effects
814
- },
815
- extendedSchema.schema.description,
816
- state
817
- );
818
- }
819
- return {
820
- type: "schema",
821
- schema: {
822
- allOf: [
823
- {
824
- $ref: createComponentSchemaRef(
825
- completeComponent.ref,
826
- (_e = state.documentOptions) == null ? void 0 : _e.componentRefPath
827
- )
828
- }
829
- ],
830
- ...extendedSchema.schema
831
- },
832
- effects: flattenEffects([
833
- completeComponent.type === "complete" ? completeComponent.effects : [],
834
- completeComponent.type === "in-progress" ? [
835
- {
836
- type: "component",
837
- zodType: zodObject,
838
- path: [...state.path]
839
- }
840
- ] : [],
841
- extendedSchema.effects
842
- ])
843
- };
844
- };
845
- const createDiffOpts = (baseOpts, extendedOpts) => {
846
- if (baseOpts.unknownKeys === "strict" || !isZodType(baseOpts.catchAll, "ZodNever")) {
847
- return void 0;
848
- }
849
- return {
850
- catchAll: extendedOpts.catchAll,
851
- unknownKeys: extendedOpts.unknownKeys
852
- };
853
- };
854
- const createShapeDiff = (baseObj, extendedObj) => {
855
- const acc = {};
856
- for (const [key, val] of Object.entries(extendedObj)) {
857
- const baseValue = baseObj[key];
858
- if (val === baseValue) {
859
- continue;
860
- }
861
- if (baseValue === void 0) {
862
- acc[key] = extendedObj[key];
863
- continue;
864
- }
865
- return null;
866
- }
867
- return acc;
868
- };
869
- const mapAdditionalProperties = ({ unknownKeys, catchAll }, state) => {
870
- if (!isZodType(catchAll, "ZodNever")) {
871
- return createSchemaObject(catchAll, state, ["additional properties"]);
872
- }
873
- if (unknownKeys === "strict") {
874
- return false;
875
- }
876
- if (unknownKeys === "passthrough") {
877
- return true;
878
- }
879
- return void 0;
880
- };
881
- const createObjectSchemaFromShape = (shape, { unknownKeys, catchAll }, state, omitType) => {
882
- const properties = mapProperties(shape, state);
883
- const required = mapRequired(properties, shape, state);
884
- const additionalProperties = mapAdditionalProperties(
885
- { catchAll, unknownKeys },
886
- state
887
- );
888
- return {
889
- type: "schema",
890
- schema: {
891
- ...!omitType && { type: "object" },
892
- ...properties && { properties: properties.properties },
893
- ...(required == null ? void 0 : required.required.length) && { required: required.required },
894
- ...additionalProperties !== void 0 && {
895
- additionalProperties: typeof additionalProperties === "object" ? additionalProperties.schema : additionalProperties
896
- }
897
- },
898
- effects: flattenEffects([
899
- ...(properties == null ? void 0 : properties.effects) ?? [],
900
- typeof additionalProperties === "object" && (additionalProperties == null ? void 0 : additionalProperties.effects),
901
- required == null ? void 0 : required.effects
902
- ])
903
- };
904
- };
905
- const mapRequired = (properties, shape, state) => {
906
- if (!properties) {
907
- return void 0;
908
- }
909
- const { required, effects } = Object.entries(properties.schemas).reduce(
910
- (acc, [key, schemaOrRef]) => {
911
- const zodSchema = shape[key];
912
- if (!zodSchema) {
913
- throw new Error("Property somehow doesn't exist in shape");
914
- }
915
- const result = zodSchema.safeParse(void 0);
916
- if (!result.success) {
917
- acc.required.push(key);
918
- return acc;
919
- }
920
- if (result.data !== void 0) {
921
- const baseEffect = {
922
- zodType: zodSchema,
923
- path: [...state.path, `property: ${key}`]
924
- };
925
- const effect = schemaOrRef.type === "ref" ? {
926
- ...baseEffect,
927
- type: "component"
928
- } : {
929
- ...baseEffect,
930
- type: "schema",
931
- creationType: state.type
932
- };
933
- acc.effects.push(effect);
934
- if (state.type === "output") {
935
- acc.required.push(key);
936
- }
937
- }
938
- return acc;
939
- },
940
- {
941
- required: [],
942
- effects: []
943
- }
944
- );
945
- return { required, effects };
946
- };
947
- const mapProperties = (shape, state) => {
948
- const shapeEntries = Object.entries(shape);
949
- if (!shapeEntries.length) {
950
- return void 0;
951
- }
952
- return shapeEntries.reduce(
953
- (acc, [key, zodSchema]) => {
954
- if (isOptionalObjectKey(zodSchema)) {
955
- return acc;
956
- }
957
- const schema = createSchemaObject(zodSchema, state, [`property: ${key}`]);
958
- acc.schemas[key] = schema;
959
- acc.properties[key] = schema.schema;
960
- acc.effects.push(schema.effects);
961
- return acc;
962
- },
963
- {
964
- schemas: {},
965
- properties: {},
966
- effects: []
967
- }
968
- );
969
- };
970
- const createPipelineSchema = (zodPipeline, state) => {
971
- var _a, _b, _c, _d, _e, _f;
972
- if (((_b = (_a = zodPipeline._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.effectType) === "input" || ((_d = (_c = zodPipeline._def.zodOpenApi) == null ? void 0 : _c.openapi) == null ? void 0 : _d.effectType) === "same") {
973
- return createSchemaObject(zodPipeline._def.in, state, ["pipeline input"]);
974
- }
975
- if (((_f = (_e = zodPipeline._def.zodOpenApi) == null ? void 0 : _e.openapi) == null ? void 0 : _f.effectType) === "output") {
976
- return createSchemaObject(zodPipeline._def.out, state, ["pipeline output"]);
977
- }
978
- if (state.type === "input") {
979
- const schema2 = createSchemaObject(zodPipeline._def.in, state, [
980
- "pipeline input"
981
- ]);
982
- return {
983
- ...schema2,
984
- effects: flattenEffects([
985
- [
986
- {
987
- type: "schema",
988
- creationType: "input",
989
- path: [...state.path],
990
- zodType: zodPipeline
991
- }
992
- ],
993
- schema2.effects
994
- ])
995
- };
996
- }
997
- const schema = createSchemaObject(zodPipeline._def.out, state, [
998
- "pipeline output"
999
- ]);
1000
- return {
1001
- ...schema,
1002
- effects: flattenEffects([
1003
- [
1004
- {
1005
- type: "schema",
1006
- creationType: "output",
1007
- path: [...state.path],
1008
- zodType: zodPipeline
1009
- }
1010
- ],
1011
- schema.effects
1012
- ])
1013
- };
1014
- };
1015
- const createPreprocessSchema = (zodPreprocess, state) => createSchemaObject(zodPreprocess._def.schema, state, ["preprocess schema"]);
1016
- const createReadonlySchema = (zodReadonly, state) => (
1017
- // Readonly doesn't change OpenAPI schema
1018
- createSchemaObject(zodReadonly._def.innerType, state, ["readonly"])
1019
- );
1020
- const createRecordSchema = (zodRecord, state) => {
1021
- const additionalProperties = createSchemaObject(
1022
- zodRecord.valueSchema,
1023
- state,
1024
- ["record value"]
1025
- );
1026
- const keySchema = createSchemaObject(zodRecord.keySchema, state, [
1027
- "record key"
1028
- ]);
1029
- const maybeComponent = state.components.schemas.get(zodRecord.keySchema);
1030
- const isComplete = maybeComponent && maybeComponent.type === "complete";
1031
- const maybeSchema = isComplete && maybeComponent.schemaObject;
1032
- const maybeEffects = isComplete && maybeComponent.effects || void 0;
1033
- const renderedKeySchema = maybeSchema || keySchema.schema;
1034
- if ("enum" in renderedKeySchema && renderedKeySchema.enum) {
1035
- return {
1036
- type: "schema",
1037
- schema: {
1038
- type: "object",
1039
- properties: renderedKeySchema.enum.reduce((acc, key) => {
1040
- acc[key] = additionalProperties.schema;
1041
- return acc;
1042
- }, {}),
1043
- additionalProperties: false
1044
- },
1045
- effects: flattenEffects([
1046
- keySchema.effects,
1047
- additionalProperties.effects,
1048
- maybeEffects
1049
- ])
1050
- };
1051
- }
1052
- if (satisfiesVersion(state.components.openapi, "3.1.0") && "type" in renderedKeySchema && renderedKeySchema.type === "string" && Object.keys(renderedKeySchema).length > 1) {
1053
- return {
1054
- type: "schema",
1055
- schema: {
1056
- type: "object",
1057
- propertyNames: keySchema.schema,
1058
- additionalProperties: additionalProperties.schema
1059
- },
1060
- effects: flattenEffects([
1061
- keySchema.effects,
1062
- additionalProperties.effects
1063
- ])
1064
- };
1065
- }
1066
- return {
1067
- type: "schema",
1068
- schema: {
1069
- type: "object",
1070
- additionalProperties: additionalProperties.schema
1071
- },
1072
- effects: additionalProperties.effects
1073
- };
1074
- };
1075
- const createRefineSchema = (zodRefine, state) => createSchemaObject(zodRefine._def.schema, state, ["refine schema"]);
1076
- const createSetSchema = (zodSet, state) => {
1077
- var _a, _b;
1078
- const schema = zodSet._def.valueType;
1079
- const minItems = (_a = zodSet._def.minSize) == null ? void 0 : _a.value;
1080
- const maxItems = (_b = zodSet._def.maxSize) == null ? void 0 : _b.value;
1081
- const itemSchema = createSchemaObject(schema, state, ["set items"]);
1082
- return {
1083
- type: "schema",
1084
- schema: {
1085
- type: "array",
1086
- items: itemSchema.schema,
1087
- uniqueItems: true,
1088
- ...minItems !== void 0 && { minItems },
1089
- ...maxItems !== void 0 && { maxItems }
1090
- },
1091
- effects: itemSchema.effects
1092
- };
1093
- };
1094
- const createStringSchema = (zodString, state) => {
1095
- var _a, _b, _c, _d, _e, _f, _g, _h;
1096
- const zodStringChecks = getZodStringChecks(zodString);
1097
- const format = mapStringFormat(zodStringChecks);
1098
- const patterns = mapPatterns(zodStringChecks);
1099
- const minLength = ((_b = (_a = zodStringChecks.length) == null ? void 0 : _a[0]) == null ? void 0 : _b.value) ?? ((_d = (_c = zodStringChecks.min) == null ? void 0 : _c[0]) == null ? void 0 : _d.value);
1100
- const maxLength = ((_f = (_e = zodStringChecks.length) == null ? void 0 : _e[0]) == null ? void 0 : _f.value) ?? ((_h = (_g = zodStringChecks.max) == null ? void 0 : _g[0]) == null ? void 0 : _h.value);
1101
- const contentEncoding = satisfiesVersion(state.components.openapi, "3.1.0") ? mapContentEncoding(zodStringChecks) : void 0;
1102
- if (patterns.length <= 1) {
1103
- return {
1104
- type: "schema",
1105
- schema: {
1106
- type: "string",
1107
- ...format && { format },
1108
- ...patterns[0] && { pattern: patterns[0] },
1109
- ...minLength !== void 0 && { minLength },
1110
- ...maxLength !== void 0 && { maxLength },
1111
- ...contentEncoding && { contentEncoding }
1112
- }
1113
- };
1114
- }
1115
- return {
1116
- type: "schema",
1117
- schema: {
1118
- allOf: [
1119
- {
1120
- type: "string",
1121
- ...format && { format },
1122
- ...patterns[0] && { pattern: patterns[0] },
1123
- ...minLength !== void 0 && { minLength },
1124
- ...maxLength !== void 0 && { maxLength },
1125
- ...contentEncoding && { contentEncoding }
1126
- },
1127
- ...patterns.slice(1).map(
1128
- (pattern) => ({
1129
- type: "string",
1130
- pattern
1131
- })
1132
- )
1133
- ]
1134
- }
1135
- };
1136
- };
1137
- const getZodStringChecks = (zodString) => zodString._def.checks.reduce(
1138
- (acc, check) => {
1139
- const mapping = acc[check.kind];
1140
- if (mapping) {
1141
- mapping.push(check);
1142
- return acc;
1143
- }
1144
- acc[check.kind] = [check];
1145
- return acc;
1146
- },
1147
- {}
1148
- );
1149
- const mapPatterns = (zodStringChecks) => {
1150
- const startsWith = mapStartsWith(zodStringChecks);
1151
- const endsWith = mapEndsWith(zodStringChecks);
1152
- const regex = mapRegex(zodStringChecks);
1153
- const includes = mapIncludes(zodStringChecks);
1154
- const patterns = [
1155
- ...regex ?? [],
1156
- ...startsWith ? [startsWith] : [],
1157
- ...endsWith ? [endsWith] : [],
1158
- ...includes ?? []
1159
- ];
1160
- return patterns;
1161
- };
1162
- const mapStartsWith = (zodStringChecks) => {
1163
- var _a, _b;
1164
- if ((_b = (_a = zodStringChecks.startsWith) == null ? void 0 : _a[0]) == null ? void 0 : _b.value) {
1165
- return `^${zodStringChecks.startsWith[0].value}`;
1166
- }
1167
- return void 0;
1168
- };
1169
- const mapEndsWith = (zodStringChecks) => {
1170
- var _a, _b;
1171
- if ((_b = (_a = zodStringChecks.endsWith) == null ? void 0 : _a[0]) == null ? void 0 : _b.value) {
1172
- return `${zodStringChecks.endsWith[0].value}$`;
1173
- }
1174
- return void 0;
1175
- };
1176
- const mapRegex = (zodStringChecks) => {
1177
- var _a;
1178
- return (_a = zodStringChecks.regex) == null ? void 0 : _a.map((regexCheck) => regexCheck.regex.source);
1179
- };
1180
- const mapIncludes = (zodStringChecks) => {
1181
- var _a;
1182
- return (_a = zodStringChecks.includes) == null ? void 0 : _a.map((includeCheck) => {
1183
- if (includeCheck.position === 0) {
1184
- return `^${includeCheck.value}`;
1185
- }
1186
- if (includeCheck.position) {
1187
- return `^.{${includeCheck.position}}${includeCheck.value}`;
1188
- }
1189
- return includeCheck.value;
1190
- });
1191
- };
1192
- const mapStringFormat = (zodStringChecks) => {
1193
- var _a, _b, _c, _d;
1194
- if (zodStringChecks.uuid) {
1195
- return "uuid";
1196
- }
1197
- if (zodStringChecks.datetime) {
1198
- return "date-time";
1199
- }
1200
- if (zodStringChecks.date) {
1201
- return "date";
1202
- }
1203
- if (zodStringChecks.time) {
1204
- return "time";
1205
- }
1206
- if (zodStringChecks.duration) {
1207
- return "duration";
1208
- }
1209
- if (zodStringChecks.email) {
1210
- return "email";
1211
- }
1212
- if (zodStringChecks.url) {
1213
- return "uri";
1214
- }
1215
- if ((_a = zodStringChecks.ip) == null ? void 0 : _a.every((ip) => ip.version === "v4")) {
1216
- return "ipv4";
1217
- }
1218
- if ((_b = zodStringChecks.ip) == null ? void 0 : _b.every((ip) => ip.version === "v6")) {
1219
- return "ipv6";
1220
- }
1221
- if ((_c = zodStringChecks.cidr) == null ? void 0 : _c.every((ip) => ip.version === "v4")) {
1222
- return "ipv4";
1223
- }
1224
- if ((_d = zodStringChecks.cidr) == null ? void 0 : _d.every((ip) => ip.version === "v6")) {
1225
- return "ipv6";
1226
- }
1227
- return void 0;
1228
- };
1229
- const mapContentEncoding = (zodStringChecks) => {
1230
- if (zodStringChecks.base64) {
1231
- return "base64";
1232
- }
1233
- return void 0;
1234
- };
1235
- const createTupleSchema = (zodTuple, state) => {
1236
- const items = zodTuple.items;
1237
- const rest = zodTuple._def.rest;
1238
- const prefixItems = mapPrefixItems(items, state);
1239
- if (satisfiesVersion(state.components.openapi, "3.1.0")) {
1240
- if (!rest) {
1241
- return {
1242
- type: "schema",
1243
- schema: {
1244
- type: "array",
1245
- maxItems: items.length,
1246
- minItems: items.length,
1247
- ...prefixItems && {
1248
- prefixItems: prefixItems.schemas.map((item) => item.schema)
1249
- }
1250
- },
1251
- effects: prefixItems == null ? void 0 : prefixItems.effects
1252
- };
1253
- }
1254
- const itemSchema = createSchemaObject(rest, state, ["tuple items"]);
1255
- return {
1256
- type: "schema",
1257
- schema: {
1258
- type: "array",
1259
- items: itemSchema.schema,
1260
- ...prefixItems && {
1261
- prefixItems: prefixItems.schemas.map((item) => item.schema)
1262
- }
1263
- },
1264
- effects: flattenEffects([prefixItems == null ? void 0 : prefixItems.effects, itemSchema.effects])
1265
- };
1266
- }
1267
- if (!rest) {
1268
- return {
1269
- type: "schema",
1270
- schema: {
1271
- type: "array",
1272
- maxItems: items.length,
1273
- minItems: items.length,
1274
- ...prefixItems && {
1275
- items: { oneOf: prefixItems.schemas.map((item) => item.schema) }
1276
- }
1277
- },
1278
- effects: prefixItems == null ? void 0 : prefixItems.effects
1279
- };
1280
- }
1281
- if (prefixItems) {
1282
- const restSchema = createSchemaObject(rest, state, ["tuple items"]);
1283
- return {
1284
- type: "schema",
1285
- schema: {
1286
- type: "array",
1287
- items: {
1288
- oneOf: [
1289
- ...prefixItems.schemas.map((item) => item.schema),
1290
- restSchema.schema
1291
- ]
1292
- }
1293
- },
1294
- effects: flattenEffects([restSchema.effects, prefixItems.effects])
1295
- };
1296
- }
1297
- return {
1298
- type: "schema",
1299
- schema: {
1300
- type: "array"
1301
- }
1302
- };
1303
- };
1304
- const mapPrefixItems = (items, state) => {
1305
- if (items.length) {
1306
- const schemas = items.map(
1307
- (item, index) => createSchemaObject(item, state, [`tuple item ${index}`])
1308
- );
1309
- return {
1310
- effects: flattenEffects(schemas.map((s) => s.effects)),
1311
- schemas
1312
- };
1313
- }
1314
- return void 0;
1315
- };
1316
- const createUnionSchema = (zodUnion, state) => {
1317
- var _a, _b, _c;
1318
- const schemas = zodUnion.options.reduce((acc, option, index) => {
1319
- if (!isOptionalObjectKey(option)) {
1320
- acc.push(createSchemaObject(option, state, [`union option ${index}`]));
1321
- }
1322
- return acc;
1323
- }, []);
1324
- if (((_b = (_a = zodUnion._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.unionOneOf) ?? ((_c = state.documentOptions) == null ? void 0 : _c.unionOneOf)) {
1325
- return {
1326
- type: "schema",
1327
- schema: {
1328
- oneOf: schemas.map((s) => s.schema)
1329
- },
1330
- effects: flattenEffects(schemas.map((s) => s.effects))
1331
- };
1332
- }
1333
- return {
1334
- type: "schema",
1335
- schema: {
1336
- anyOf: schemas.map((s) => s.schema)
1337
- },
1338
- effects: flattenEffects(schemas.map((s) => s.effects))
1339
- };
1340
- };
1341
- const createUnknownSchema = (_zodUnknown) => ({
1342
- type: "schema",
1343
- schema: {}
1344
- });
1345
- const createSchemaSwitch = (zodSchema, previous, state) => {
1346
- var _a, _b;
1347
- if ((_b = (_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.type) {
1348
- return createManualTypeSchema(zodSchema, state);
1349
- }
1350
- if (isZodType(zodSchema, "ZodString")) {
1351
- return createStringSchema(zodSchema, state);
1352
- }
1353
- if (isZodType(zodSchema, "ZodNumber")) {
1354
- return createNumberSchema(zodSchema, state);
1355
- }
1356
- if (isZodType(zodSchema, "ZodBoolean")) {
1357
- return createBooleanSchema();
1358
- }
1359
- if (isZodType(zodSchema, "ZodEnum")) {
1360
- return createEnumSchema(zodSchema);
1361
- }
1362
- if (isZodType(zodSchema, "ZodLiteral")) {
1363
- return createLiteralSchema(zodSchema, state);
1364
- }
1365
- if (isZodType(zodSchema, "ZodNativeEnum")) {
1366
- return createNativeEnumSchema(zodSchema, state);
1367
- }
1368
- if (isZodType(zodSchema, "ZodArray")) {
1369
- return createArraySchema(zodSchema, state);
1370
- }
1371
- if (isZodType(zodSchema, "ZodObject")) {
1372
- return createObjectSchema(zodSchema, previous, state);
1373
- }
1374
- if (isZodType(zodSchema, "ZodUnion")) {
1375
- return createUnionSchema(zodSchema, state);
1376
- }
1377
- if (isZodType(zodSchema, "ZodDiscriminatedUnion")) {
1378
- return createDiscriminatedUnionSchema(zodSchema, state);
1379
- }
1380
- if (isZodType(zodSchema, "ZodNull")) {
1381
- return createNullSchema();
1382
- }
1383
- if (isZodType(zodSchema, "ZodNullable")) {
1384
- return createNullableSchema(zodSchema, state);
1385
- }
1386
- if (isZodType(zodSchema, "ZodOptional")) {
1387
- return createOptionalSchema(zodSchema, state);
1388
- }
1389
- if (isZodType(zodSchema, "ZodReadonly")) {
1390
- return createReadonlySchema(zodSchema, state);
1391
- }
1392
- if (isZodType(zodSchema, "ZodDefault")) {
1393
- return createDefaultSchema(zodSchema, state, previous);
1394
- }
1395
- if (isZodType(zodSchema, "ZodRecord")) {
1396
- return createRecordSchema(zodSchema, state);
1397
- }
1398
- if (isZodType(zodSchema, "ZodTuple")) {
1399
- return createTupleSchema(zodSchema, state);
1400
- }
1401
- if (isZodType(zodSchema, "ZodDate")) {
1402
- return createDateSchema(zodSchema, state);
1403
- }
1404
- if (isZodType(zodSchema, "ZodPipeline")) {
1405
- return createPipelineSchema(zodSchema, state);
1406
- }
1407
- if (isZodType(zodSchema, "ZodEffects") && zodSchema._def.effect.type === "transform") {
1408
- return createTransformSchema(zodSchema, state);
1409
- }
1410
- if (isZodType(zodSchema, "ZodEffects") && zodSchema._def.effect.type === "preprocess") {
1411
- return createPreprocessSchema(zodSchema, state);
1412
- }
1413
- if (isZodType(zodSchema, "ZodEffects") && zodSchema._def.effect.type === "refinement") {
1414
- return createRefineSchema(zodSchema, state);
1415
- }
1416
- if (isZodType(zodSchema, "ZodNativeEnum")) {
1417
- return createNativeEnumSchema(zodSchema, state);
1418
- }
1419
- if (isZodType(zodSchema, "ZodIntersection")) {
1420
- return createIntersectionSchema(zodSchema, state);
1421
- }
1422
- if (isZodType(zodSchema, "ZodCatch")) {
1423
- return createCatchSchema(zodSchema, state, previous);
1424
- }
1425
- if (isZodType(zodSchema, "ZodUnknown") || isZodType(zodSchema, "ZodAny")) {
1426
- return createUnknownSchema();
1427
- }
1428
- if (isZodType(zodSchema, "ZodLazy")) {
1429
- return createLazySchema(zodSchema, state);
1430
- }
1431
- if (isZodType(zodSchema, "ZodBranded")) {
1432
- return createBrandedSchema(zodSchema, state);
1433
- }
1434
- if (isZodType(zodSchema, "ZodSet")) {
1435
- return createSetSchema(zodSchema, state);
1436
- }
1437
- if (isZodType(zodSchema, "ZodBigInt")) {
1438
- return createBigIntSchema();
1439
- }
1440
- return createManualTypeSchema(zodSchema, state);
1441
- };
1442
- const createNewSchema = ({
1443
- zodSchema,
1444
- previous,
1445
- state
1446
- }) => {
1447
- var _a;
1448
- if (state.visited.has(zodSchema)) {
1449
- throw new Error(
1450
- `The schema at ${state.path.join(
1451
- " > "
1452
- )} needs to be registered because it's circularly referenced`
1453
- );
1454
- }
1455
- state.visited.add(zodSchema);
1456
- const {
1457
- effectType,
1458
- param,
1459
- header,
1460
- ref,
1461
- refType,
1462
- unionOneOf,
1463
- ...additionalMetadata
1464
- } = ((_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.openapi) ?? {};
1465
- const schema = createSchemaSwitch(zodSchema, previous, state);
1466
- const schemaWithMetadata = enhanceWithMetadata(
1467
- schema,
1468
- additionalMetadata,
1469
- state,
1470
- previous
1471
- );
1472
- state.visited.delete(zodSchema);
1473
- return schemaWithMetadata;
1474
- };
1475
- const createNewRef = ({
1476
- previous,
1477
- ref,
1478
- zodSchema,
1479
- state
1480
- }) => {
1481
- var _a;
1482
- state.components.schemas.set(zodSchema, {
1483
- type: "in-progress",
1484
- ref
1485
- });
1486
- const newSchema = createNewSchema({
1487
- zodSchema,
1488
- previous,
1489
- state: {
1490
- ...state,
1491
- visited: /* @__PURE__ */ new Set()
1492
- }
1493
- });
1494
- state.components.schemas.set(zodSchema, {
1495
- type: "complete",
1496
- ref,
1497
- schemaObject: newSchema.schema,
1498
- effects: newSchema.effects
1499
- });
1500
- return {
1501
- type: "ref",
1502
- schema: {
1503
- $ref: createComponentSchemaRef(
1504
- ref,
1505
- (_a = state.documentOptions) == null ? void 0 : _a.componentRefPath
1506
- )
1507
- },
1508
- schemaObject: newSchema.schema,
1509
- effects: newSchema.effects ? [
1510
- {
1511
- type: "component",
1512
- zodType: zodSchema,
1513
- path: [...state.path]
1514
- }
1515
- ] : void 0,
1516
- zodType: zodSchema
1517
- };
1518
- };
1519
- const createExistingRef = (zodSchema, component, state) => {
1520
- var _a, _b;
1521
- if (component && component.type === "complete") {
1522
- return {
1523
- type: "ref",
1524
- schema: {
1525
- $ref: createComponentSchemaRef(
1526
- component.ref,
1527
- (_a = state.documentOptions) == null ? void 0 : _a.componentRefPath
1528
- )
1529
- },
1530
- schemaObject: component.schemaObject,
1531
- effects: component.effects ? [
1532
- {
1533
- type: "component",
1534
- zodType: zodSchema,
1535
- path: [...state.path]
1536
- }
1537
- ] : void 0,
1538
- zodType: zodSchema
1539
- };
1540
- }
1541
- if (component && component.type === "in-progress") {
1542
- return {
1543
- type: "ref",
1544
- schema: {
1545
- $ref: createComponentSchemaRef(
1546
- component.ref,
1547
- (_b = state.documentOptions) == null ? void 0 : _b.componentRefPath
1548
- )
1549
- },
1550
- schemaObject: void 0,
1551
- effects: [
1552
- {
1553
- type: "component",
1554
- zodType: zodSchema,
1555
- path: [...state.path]
1556
- }
1557
- ],
1558
- zodType: zodSchema
1559
- };
1560
- }
1561
- return;
1562
- };
1563
- const createSchemaOrRef = (zodSchema, state, onlyRef) => {
1564
- var _a, _b, _c, _d;
1565
- const component = state.components.schemas.get(zodSchema);
1566
- const existingRef = createExistingRef(zodSchema, component, state);
1567
- if (existingRef) {
1568
- return existingRef;
1569
- }
1570
- const previous = ((_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a[extendZodSymbols.previousSymbol]) ? createSchemaOrRef(
1571
- zodSchema._def.zodOpenApi[extendZodSymbols.previousSymbol],
1572
- state,
1573
- true
1574
- ) : void 0;
1575
- const current = ((_b = zodSchema._def.zodOpenApi) == null ? void 0 : _b[extendZodSymbols.currentSymbol]) && zodSchema._def.zodOpenApi[extendZodSymbols.currentSymbol] !== zodSchema ? createSchemaOrRef(
1576
- zodSchema._def.zodOpenApi[extendZodSymbols.currentSymbol],
1577
- state,
1578
- true
1579
- ) : void 0;
1580
- const ref = ((_d = (_c = zodSchema._def.zodOpenApi) == null ? void 0 : _c.openapi) == null ? void 0 : _d.ref) ?? (component == null ? void 0 : component.ref);
1581
- if (ref) {
1582
- return current ? createNewSchema({ zodSchema, previous: current, state }) : createNewRef({ ref, zodSchema, previous, state });
1583
- }
1584
- if (onlyRef) {
1585
- return previous ?? current;
1586
- }
1587
- return createNewSchema({ zodSchema, previous: previous ?? current, state });
1588
- };
1589
- const createSchemaObject = (zodSchema, state, subpath) => {
1590
- state.path.push(...subpath);
1591
- const schema = createSchemaOrRef(zodSchema, state);
1592
- if (!schema) {
1593
- throw new Error("Schema does not exist");
1594
- }
1595
- state.path.pop();
1596
- return schema;
1597
- };
1598
- const createSchema = (zodSchema, state, subpath) => {
1599
- const schema = createSchemaObject(zodSchema, state, subpath);
1600
- if (schema.effects) {
1601
- verifyEffects(schema.effects, state);
1602
- }
1603
- return schema.schema;
1604
- };
1605
- const createMediaTypeSchema = (schemaObject, components, type, subpath, documentOptions) => {
1606
- if (!schemaObject) {
1607
- return void 0;
1608
- }
1609
- if (!isAnyZodType(schemaObject)) {
1610
- return schemaObject;
1611
- }
1612
- return createSchema(
1613
- schemaObject,
1614
- {
1615
- components,
1616
- type,
1617
- path: [],
1618
- visited: /* @__PURE__ */ new Set(),
1619
- documentOptions
1620
- },
1621
- subpath
1622
- );
1623
- };
1624
- const createMediaTypeObject = (mediaTypeObject, components, type, subpath, documentOptions) => {
1625
- if (!mediaTypeObject) {
1626
- return void 0;
1627
- }
1628
- return {
1629
- ...mediaTypeObject,
1630
- schema: createMediaTypeSchema(
1631
- mediaTypeObject.schema,
1632
- components,
1633
- type,
1634
- [...subpath, "schema"],
1635
- documentOptions
1636
- )
1637
- };
1638
- };
1639
- const createContent = (contentObject, components, type, subpath, documentOptions) => Object.entries(contentObject).reduce(
1640
- (acc, [mediaType, zodOpenApiMediaTypeObject]) => {
1641
- const mediaTypeObject = createMediaTypeObject(
1642
- zodOpenApiMediaTypeObject,
1643
- components,
1644
- type,
1645
- [...subpath, mediaType],
1646
- documentOptions
1647
- );
1648
- if (mediaTypeObject) {
1649
- acc[mediaType] = mediaTypeObject;
1650
- }
1651
- return acc;
1652
- },
1653
- {}
1654
- );
1655
- const createComponentParamRef = (ref) => `#/components/parameters/${ref}`;
1656
- const createBaseParameter = (schema, components, subpath, documentOptions) => {
1657
- var _a, _b, _c, _d;
1658
- const { ref, ...rest } = ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) ?? {};
1659
- const state = {
1660
- components,
1661
- type: "input",
1662
- path: [],
1663
- visited: /* @__PURE__ */ new Set(),
1664
- documentOptions
1665
- };
1666
- const schemaObject = createSchema(schema, state, [...subpath, "schema"]);
1667
- const required = !schema.isOptional();
1668
- const description = ((_d = (_c = schema._def.zodOpenApi) == null ? void 0 : _c.openapi) == null ? void 0 : _d.description) ?? schema._def.description;
1669
- return {
1670
- ...description && { description },
1671
- ...rest,
1672
- ...schema && { schema: schemaObject },
1673
- ...required && { required }
1674
- };
1675
- };
1676
- const createParamOrRef = (zodSchema, components, subpath, type, name, documentOptions) => {
1677
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1678
- const component = components.parameters.get(zodSchema);
1679
- const paramType = ((_c = (_b = (_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) == null ? void 0 : _c.in) ?? (component == null ? void 0 : component.in) ?? type;
1680
- const paramName = ((_f = (_e = (_d = zodSchema._def.zodOpenApi) == null ? void 0 : _d.openapi) == null ? void 0 : _e.param) == null ? void 0 : _f.name) ?? (component == null ? void 0 : component.name) ?? name;
1681
- if (!paramType) {
1682
- throw new Error("Parameter type missing");
1683
- }
1684
- if (!paramName) {
1685
- throw new Error("Parameter name missing");
1686
- }
1687
- if (component && component.type === "complete") {
1688
- if (!("$ref" in component.paramObject) && (component.in !== paramType || component.name !== paramName)) {
1689
- throw new Error(`parameterRef "${component.ref}" is already registered`);
1690
- }
1691
- return {
1692
- $ref: createComponentParamRef(component.ref)
1693
- };
1694
- }
1695
- const baseParamOrRef = createBaseParameter(
1696
- zodSchema,
1697
- components,
1698
- subpath,
1699
- documentOptions
1700
- );
1701
- if ("$ref" in baseParamOrRef) {
1702
- throw new Error("Unexpected Error: received a reference object");
1703
- }
1704
- const ref = ((_i = (_h = (_g = zodSchema == null ? void 0 : zodSchema._def.zodOpenApi) == null ? void 0 : _g.openapi) == null ? void 0 : _h.param) == null ? void 0 : _i.ref) ?? (component == null ? void 0 : component.ref);
1705
- const paramObject = {
1706
- in: paramType,
1707
- name: paramName,
1708
- ...baseParamOrRef
1709
- };
1710
- if (ref) {
1711
- components.parameters.set(zodSchema, {
1712
- type: "complete",
1713
- paramObject,
1714
- ref,
1715
- in: paramType,
1716
- name: paramName
1717
- });
1718
- return {
1719
- $ref: createComponentParamRef(ref)
1720
- };
1721
- }
1722
- return paramObject;
1723
- };
1724
- const createParameters = (type, zodObjectType, components, subpath, documentOptions) => {
1725
- if (!zodObjectType) {
1726
- return [];
1727
- }
1728
- const zodObject = getZodObject(zodObjectType, "input").shape;
1729
- return Object.entries(zodObject).map(
1730
- ([key, zodSchema]) => createParamOrRef(
1731
- zodSchema,
1732
- components,
1733
- [...subpath, key],
1734
- type,
1735
- key,
1736
- documentOptions
1737
- )
1738
- );
1739
- };
1740
- const createRequestParams = (requestParams, components, subpath, documentOptions) => {
1741
- if (!requestParams) {
1742
- return [];
1743
- }
1744
- const pathParams = createParameters(
1745
- "path",
1746
- requestParams.path,
1747
- components,
1748
- [...subpath, "path"],
1749
- documentOptions
1750
- );
1751
- const queryParams = createParameters(
1752
- "query",
1753
- requestParams.query,
1754
- components,
1755
- [...subpath, "query"],
1756
- documentOptions
1757
- );
1758
- const cookieParams = createParameters(
1759
- "cookie",
1760
- requestParams.cookie,
1761
- components,
1762
- [...subpath, "cookie"],
1763
- documentOptions
1764
- );
1765
- const headerParams = createParameters(
1766
- "header",
1767
- requestParams.header,
1768
- components,
1769
- [...subpath, "header"],
1770
- documentOptions
1771
- );
1772
- return [...pathParams, ...queryParams, ...cookieParams, ...headerParams];
1773
- };
1774
- const createManualParameters = (parameters, components, subpath, documentOptions) => (parameters == null ? void 0 : parameters.map((param, index) => {
1775
- if (isAnyZodType(param)) {
1776
- return createParamOrRef(
1777
- param,
1778
- components,
1779
- [...subpath, `param index ${index}`],
1780
- void 0,
1781
- void 0,
1782
- documentOptions
1783
- );
1784
- }
1785
- return param;
1786
- })) ?? [];
1787
- const createParametersObject = (parameters, requestParams, components, subpath, documentOptions) => {
1788
- const manualParameters = createManualParameters(
1789
- parameters,
1790
- components,
1791
- subpath,
1792
- documentOptions
1793
- );
1794
- const createdParams = createRequestParams(
1795
- requestParams,
1796
- components,
1797
- subpath,
1798
- documentOptions
1799
- );
1800
- const combinedParameters = [
1801
- ...manualParameters,
1802
- ...createdParams
1803
- ];
1804
- return combinedParameters.length ? combinedParameters : void 0;
1805
- };
1806
- const getZodObject = (schema, type) => {
1807
- if (isZodType(schema, "ZodObject")) {
1808
- return schema;
1809
- }
1810
- if (isZodType(schema, "ZodLazy")) {
1811
- return getZodObject(schema.schema, type);
1812
- }
1813
- if (isZodType(schema, "ZodEffects")) {
1814
- return getZodObject(schema.innerType(), type);
1815
- }
1816
- if (isZodType(schema, "ZodBranded")) {
1817
- return getZodObject(schema.unwrap(), type);
1818
- }
1819
- if (isZodType(schema, "ZodPipeline")) {
1820
- if (type === "input") {
1821
- return getZodObject(schema._def.in, type);
1822
- }
1823
- return getZodObject(schema._def.out, type);
1824
- }
1825
- throw new Error("failed to find ZodObject in schema");
1826
- };
1827
- const isISpecificationExtension = (key) => key.startsWith("x-");
1828
- const createResponseHeaders = (responseHeaders, components, documentOptions) => {
1829
- if (!responseHeaders) {
1830
- return void 0;
1831
- }
1832
- if (isAnyZodType(responseHeaders)) {
1833
- return Object.entries(responseHeaders.shape).reduce((acc, [key, zodSchema]) => {
1834
- acc[key] = createHeaderOrRef(zodSchema, components, documentOptions);
1835
- return acc;
1836
- }, {});
1837
- }
1838
- return responseHeaders;
1839
- };
1840
- const createHeaderOrRef = (schema, components, documentOptions) => {
1841
- var _a, _b, _c;
1842
- const component = components.headers.get(schema);
1843
- if (component && component.type === "complete") {
1844
- return {
1845
- $ref: createComponentHeaderRef(component.ref)
1846
- };
1847
- }
1848
- const baseHeader = createBaseHeader(schema, components, documentOptions);
1849
- if ("$ref" in baseHeader) {
1850
- throw new Error("Unexpected Error: received a reference object");
1851
- }
1852
- const ref = ((_c = (_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.header) == null ? void 0 : _c.ref) ?? (component == null ? void 0 : component.ref);
1853
- if (ref) {
1854
- components.headers.set(schema, {
1855
- type: "complete",
1856
- headerObject: baseHeader,
1857
- ref
1858
- });
1859
- return {
1860
- $ref: createComponentHeaderRef(ref)
1861
- };
1862
- }
1863
- return baseHeader;
1864
- };
1865
- const createBaseHeader = (schema, components, documentOptions) => {
1866
- var _a, _b;
1867
- const { ref, ...rest } = ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.header) ?? {};
1868
- const state = {
1869
- components,
1870
- type: "output",
1871
- path: [],
1872
- visited: /* @__PURE__ */ new Set(),
1873
- documentOptions
1874
- };
1875
- const schemaObject = createSchema(schema, state, ["header"]);
1876
- const optionalResult = schema.safeParse(void 0);
1877
- const required = !optionalResult.success || optionalResult !== void 0;
1878
- return {
1879
- ...rest,
1880
- ...schema && { schema: schemaObject },
1881
- ...required && { required }
1882
- };
1883
- };
1884
- const createComponentHeaderRef = (ref) => `#/components/headers/${ref}`;
1885
- const createResponse = (responseObject, components, subpath, documentOptions) => {
1886
- if ("$ref" in responseObject) {
1887
- return responseObject;
1888
- }
1889
- const component = components.responses.get(responseObject);
1890
- if (component && component.type === "complete") {
1891
- return { $ref: createComponentResponseRef(component.ref) };
1892
- }
1893
- const { content, headers, ref, ...rest } = responseObject;
1894
- const maybeHeaders = createResponseHeaders(
1895
- headers,
1896
- components,
1897
- documentOptions
1898
- );
1899
- const response = {
1900
- ...rest,
1901
- ...maybeHeaders && { headers: maybeHeaders },
1902
- ...content && {
1903
- content: createContent(
1904
- content,
1905
- components,
1906
- "output",
1907
- [...subpath, "content"],
1908
- documentOptions
1909
- )
1910
- }
1911
- };
1912
- const responseRef = ref ?? (component == null ? void 0 : component.ref);
1913
- if (responseRef) {
1914
- components.responses.set(responseObject, {
1915
- responseObject: response,
1916
- ref: responseRef,
1917
- type: "complete"
1918
- });
1919
- return {
1920
- $ref: createComponentResponseRef(responseRef)
1921
- };
1922
- }
1923
- return response;
1924
- };
1925
- const createResponses = (responsesObject, components, subpath, documentOptions) => Object.entries(responsesObject).reduce(
1926
- (acc, [statusCode, responseObject]) => {
1927
- if (isISpecificationExtension(statusCode)) {
1928
- acc[statusCode] = responseObject;
1929
- return acc;
1930
- }
1931
- acc[statusCode] = createResponse(
1932
- responseObject,
1933
- components,
1934
- [...subpath, statusCode],
1935
- documentOptions
1936
- );
1937
- return acc;
1938
- },
1939
- {}
1940
- );
1941
- const createRequestBody = (requestBodyObject, components, subpath, documentOptions) => {
1942
- if (!requestBodyObject) {
1943
- return void 0;
1944
- }
1945
- const component = components.requestBodies.get(requestBodyObject);
1946
- if (component && component.type === "complete") {
1947
- return {
1948
- $ref: createComponentRequestBodyRef(component.ref)
1949
- };
1950
- }
1951
- const { ref: reqBodyRef, ...cleanRequestBody } = requestBodyObject;
1952
- const ref = reqBodyRef ?? (component == null ? void 0 : component.ref);
1953
- const requestBody = {
1954
- ...cleanRequestBody,
1955
- content: createContent(
1956
- cleanRequestBody.content,
1957
- components,
1958
- "input",
1959
- [...subpath, "content"],
1960
- documentOptions
1961
- )
1962
- };
1963
- if (ref) {
1964
- components.requestBodies.set(requestBodyObject, {
1965
- type: "complete",
1966
- ref,
1967
- requestBodyObject: requestBody
1968
- });
1969
- return {
1970
- $ref: createComponentRequestBodyRef(ref)
1971
- };
1972
- }
1973
- return requestBody;
1974
- };
1975
- const createOperation = (operationObject, components, subpath, documentOptions) => {
1976
- const { parameters, requestParams, requestBody, responses, ...rest } = operationObject;
1977
- const maybeParameters = createParametersObject(
1978
- parameters,
1979
- requestParams,
1980
- components,
1981
- [...subpath, "parameters"],
1982
- documentOptions
1983
- );
1984
- const maybeRequestBody = createRequestBody(
1985
- operationObject.requestBody,
1986
- components,
1987
- [...subpath, "request body"],
1988
- documentOptions
1989
- );
1990
- const maybeResponses = createResponses(
1991
- operationObject.responses,
1992
- components,
1993
- [...subpath, "responses"],
1994
- documentOptions
1995
- );
1996
- const maybeCallbacks = createCallbacks(
1997
- operationObject.callbacks,
1998
- components,
1999
- [...subpath, "callbacks"],
2000
- documentOptions
2001
- );
2002
- return {
2003
- ...rest,
2004
- ...maybeParameters && { parameters: maybeParameters },
2005
- ...maybeRequestBody && { requestBody: maybeRequestBody },
2006
- ...maybeResponses && { responses: maybeResponses },
2007
- ...maybeCallbacks && { callbacks: maybeCallbacks }
2008
- };
2009
- };
2010
- const createPathItem = (pathObject, components, path, documentOptions) => Object.entries(pathObject).reduce(
2011
- (acc, [key, value]) => {
2012
- if (!value) {
2013
- return acc;
2014
- }
2015
- if (key === "get" || key === "put" || key === "post" || key === "delete" || key === "options" || key === "head" || key === "patch" || key === "trace") {
2016
- acc[key] = createOperation(
2017
- value,
2018
- components,
2019
- [...path, key],
2020
- documentOptions
2021
- );
2022
- return acc;
2023
- }
2024
- acc[key] = value;
2025
- return acc;
2026
- },
2027
- {}
2028
- );
2029
- const createPaths = (pathsObject, components, documentOptions) => {
2030
- if (!pathsObject) {
2031
- return void 0;
2032
- }
2033
- return Object.entries(pathsObject).reduce(
2034
- (acc, [path, pathItemObject]) => {
2035
- if (isISpecificationExtension(path)) {
2036
- acc[path] = pathItemObject;
2037
- return acc;
2038
- }
2039
- acc[path] = createPathItem(
2040
- pathItemObject,
2041
- components,
2042
- [path],
2043
- documentOptions
2044
- );
2045
- return acc;
2046
- },
2047
- {}
2048
- );
2049
- };
2050
- const createCallback = (callbackObject, components, subpath, documentOptions) => {
2051
- const { ref, ...callbacks } = callbackObject;
2052
- const callback = Object.entries(
2053
- callbacks
2054
- ).reduce((acc, [callbackName, pathItemObject]) => {
2055
- if (isISpecificationExtension(callbackName)) {
2056
- acc[callbackName] = pathItemObject;
2057
- return acc;
2058
- }
2059
- acc[callbackName] = createPathItem(
2060
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
2061
- pathItemObject,
2062
- components,
2063
- [...subpath, callbackName],
2064
- documentOptions
2065
- );
2066
- return acc;
2067
- }, {});
2068
- if (ref) {
2069
- components.callbacks.set(callbackObject, {
2070
- type: "complete",
2071
- ref,
2072
- callbackObject: callback
2073
- });
2074
- return {
2075
- $ref: createComponentCallbackRef(ref)
2076
- };
2077
- }
2078
- return callback;
2079
- };
2080
- const createCallbacks = (callbacksObject, components, subpath, documentOptions) => {
2081
- if (!callbacksObject) {
2082
- return void 0;
2083
- }
2084
- return Object.entries(callbacksObject).reduce(
2085
- (acc, [callbackName, callbackObject]) => {
2086
- if (isISpecificationExtension(callbackName)) {
2087
- acc[callbackName] = callbackObject;
2088
- return acc;
2089
- }
2090
- acc[callbackName] = createCallback(
2091
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
2092
- callbackObject,
2093
- components,
2094
- [...subpath, callbackName],
2095
- documentOptions
2096
- );
2097
- return acc;
2098
- },
2099
- {}
2100
- );
2101
- };
2102
- const getDefaultComponents = (componentsObject, openapi = "3.1.0") => {
2103
- const defaultComponents = {
2104
- schemas: /* @__PURE__ */ new Map(),
2105
- parameters: /* @__PURE__ */ new Map(),
2106
- headers: /* @__PURE__ */ new Map(),
2107
- requestBodies: /* @__PURE__ */ new Map(),
2108
- responses: /* @__PURE__ */ new Map(),
2109
- callbacks: /* @__PURE__ */ new Map(),
2110
- openapi
2111
- };
2112
- if (!componentsObject) {
2113
- return defaultComponents;
2114
- }
2115
- getSchemas(componentsObject.schemas, defaultComponents);
2116
- getParameters(componentsObject.parameters, defaultComponents);
2117
- getRequestBodies(componentsObject.requestBodies, defaultComponents);
2118
- getHeaders(componentsObject.headers, defaultComponents);
2119
- getResponses(componentsObject.responses, defaultComponents);
2120
- getCallbacks(componentsObject.callbacks, defaultComponents);
2121
- return defaultComponents;
2122
- };
2123
- const getSchemas = (schemas, components) => {
2124
- if (!schemas) {
2125
- return;
2126
- }
2127
- Object.entries(schemas).forEach(([key, schema]) => {
2128
- var _a, _b;
2129
- if (isAnyZodType(schema)) {
2130
- if (components.schemas.has(schema)) {
2131
- throw new Error(
2132
- `Schema ${JSON.stringify(schema._def)} is already registered`
2133
- );
2134
- }
2135
- const ref = ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.ref) ?? key;
2136
- components.schemas.set(schema, {
2137
- type: "manual",
2138
- ref
2139
- });
2140
- }
2141
- });
2142
- };
2143
- const getParameters = (parameters, components) => {
2144
- if (!parameters) {
2145
- return;
2146
- }
2147
- Object.entries(parameters).forEach(([key, schema]) => {
2148
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2149
- if (isAnyZodType(schema)) {
2150
- if (components.parameters.has(schema)) {
2151
- throw new Error(
2152
- `Parameter ${JSON.stringify(schema._def)} is already registered`
2153
- );
2154
- }
2155
- const ref = ((_c = (_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) == null ? void 0 : _c.ref) ?? key;
2156
- const name = (_f = (_e = (_d = schema._def.zodOpenApi) == null ? void 0 : _d.openapi) == null ? void 0 : _e.param) == null ? void 0 : _f.name;
2157
- const location = (_i = (_h = (_g = schema._def.zodOpenApi) == null ? void 0 : _g.openapi) == null ? void 0 : _h.param) == null ? void 0 : _i.in;
2158
- if (!name || !location) {
2159
- throw new Error("`name` or `in` missing in .openapi()");
2160
- }
2161
- components.parameters.set(schema, {
2162
- type: "manual",
2163
- ref,
2164
- in: location,
2165
- name
2166
- });
2167
- }
2168
- });
2169
- };
2170
- const getHeaders = (responseHeaders, components) => {
2171
- if (!responseHeaders) {
2172
- return;
2173
- }
2174
- Object.entries(responseHeaders).forEach(([key, schema]) => {
2175
- var _a, _b, _c;
2176
- if (isAnyZodType(schema)) {
2177
- if (components.parameters.has(schema)) {
2178
- throw new Error(
2179
- `Header ${JSON.stringify(schema._def)} is already registered`
2180
- );
2181
- }
2182
- const ref = ((_c = (_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) == null ? void 0 : _c.ref) ?? key;
2183
- components.headers.set(schema, {
2184
- type: "manual",
2185
- ref
2186
- });
2187
- }
2188
- });
2189
- };
2190
- const getResponses = (responses, components) => {
2191
- if (!responses) {
2192
- return;
2193
- }
2194
- Object.entries(responses).forEach(([key, responseObject]) => {
2195
- if (components.responses.has(responseObject)) {
2196
- throw new Error(
2197
- `Header ${JSON.stringify(responseObject)} is already registered`
2198
- );
2199
- }
2200
- const ref = (responseObject == null ? void 0 : responseObject.ref) ?? key;
2201
- components.responses.set(responseObject, {
2202
- type: "manual",
2203
- ref
2204
- });
2205
- });
2206
- };
2207
- const getRequestBodies = (requestBodies, components) => {
2208
- if (!requestBodies) {
2209
- return;
2210
- }
2211
- Object.entries(requestBodies).forEach(([key, requestBody]) => {
2212
- if (components.requestBodies.has(requestBody)) {
2213
- throw new Error(
2214
- `Header ${JSON.stringify(requestBody)} is already registered`
2215
- );
2216
- }
2217
- const ref = (requestBody == null ? void 0 : requestBody.ref) ?? key;
2218
- components.requestBodies.set(requestBody, {
2219
- type: "manual",
2220
- ref
2221
- });
2222
- });
2223
- };
2224
- const getCallbacks = (callbacks, components) => {
2225
- if (!callbacks) {
2226
- return;
2227
- }
2228
- Object.entries(callbacks).forEach(([key, callback]) => {
2229
- if (components.callbacks.has(callback)) {
2230
- throw new Error(
2231
- `Callback ${JSON.stringify(callback)} is already registered`
2232
- );
2233
- }
2234
- const ref = (callback == null ? void 0 : callback.ref) ?? key;
2235
- components.callbacks.set(callback, {
2236
- type: "manual",
2237
- ref
2238
- });
2239
- });
2240
- };
2241
- const createComponentSchemaRef = (schemaRef, componentPath) => `${componentPath ?? "#/components/schemas/"}${schemaRef}`;
2242
- const createComponentResponseRef = (responseRef) => `#/components/responses/${responseRef}`;
2243
- const createComponentRequestBodyRef = (requestBodyRef) => `#/components/requestBodies/${requestBodyRef}`;
2244
- const createComponentCallbackRef = (callbackRef) => `#/components/callbacks/${callbackRef}`;
2245
- const createComponents = (componentsObject, components, documentOptions) => {
2246
- const combinedSchemas = createSchemaComponents(
2247
- componentsObject,
2248
- components,
2249
- documentOptions
2250
- );
2251
- const combinedParameters = createParamComponents(
2252
- componentsObject,
2253
- components,
2254
- documentOptions
2255
- );
2256
- const combinedHeaders = createHeaderComponents(
2257
- componentsObject,
2258
- components,
2259
- documentOptions
2260
- );
2261
- const combinedResponses = createResponseComponents(
2262
- components,
2263
- documentOptions
2264
- );
2265
- const combinedRequestBodies = createRequestBodiesComponents(
2266
- components,
2267
- documentOptions
2268
- );
2269
- const combinedCallbacks = createCallbackComponents(
2270
- components,
2271
- documentOptions
2272
- );
2273
- const { schemas, parameters, headers, responses, requestBodies, ...rest } = componentsObject;
2274
- const finalComponents = {
2275
- ...rest,
2276
- ...combinedSchemas && { schemas: combinedSchemas },
2277
- ...combinedParameters && { parameters: combinedParameters },
2278
- ...combinedRequestBodies && { requestBodies: combinedRequestBodies },
2279
- ...combinedHeaders && { headers: combinedHeaders },
2280
- ...combinedResponses && { responses: combinedResponses },
2281
- ...combinedCallbacks && { callbacks: combinedCallbacks }
2282
- };
2283
- return Object.keys(finalComponents).length ? finalComponents : void 0;
2284
- };
2285
- const createSchemaComponents = (componentsObject, components, documentOptions) => {
2286
- Array.from(components.schemas).forEach(([schema, { type }], index) => {
2287
- var _a, _b;
2288
- if (type === "manual") {
2289
- const state = {
2290
- components,
2291
- type: ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.refType) ?? "output",
2292
- path: [],
2293
- visited: /* @__PURE__ */ new Set(),
2294
- documentOptions
2295
- };
2296
- createSchema(schema, state, [`component schema index ${index}`]);
2297
- }
2298
- });
2299
- const customComponents = Object.entries(
2300
- componentsObject.schemas ?? {}
2301
- ).reduce(
2302
- (acc, [key, value]) => {
2303
- if (isAnyZodType(value)) {
2304
- return acc;
2305
- }
2306
- if (acc[key]) {
2307
- throw new Error(`Schema "${key}" is already registered`);
2308
- }
2309
- acc[key] = value;
2310
- return acc;
2311
- },
2312
- {}
2313
- );
2314
- const finalComponents = Array.from(components.schemas).reduce((acc, [_zodType, component]) => {
2315
- if (component.type === "complete") {
2316
- if (acc[component.ref]) {
2317
- throw new Error(`Schema "${component.ref}" is already registered`);
2318
- }
2319
- acc[component.ref] = component.schemaObject;
2320
- }
2321
- return acc;
2322
- }, customComponents);
2323
- return Object.keys(finalComponents).length ? finalComponents : void 0;
2324
- };
2325
- const createParamComponents = (componentsObject, components, documentOptions) => {
2326
- Array.from(components.parameters).forEach(([schema, component], index) => {
2327
- if (component.type === "manual") {
2328
- createParamOrRef(
2329
- schema,
2330
- components,
2331
- [`component parameter index ${index}`],
2332
- component.in,
2333
- component.ref,
2334
- documentOptions
2335
- );
2336
- }
2337
- });
2338
- const customComponents = Object.entries(
2339
- componentsObject.parameters ?? {}
2340
- ).reduce(
2341
- (acc, [key, value]) => {
2342
- if (!isAnyZodType(value)) {
2343
- if (acc[key]) {
2344
- throw new Error(`Parameter "${key}" is already registered`);
2345
- }
2346
- acc[key] = value;
2347
- }
2348
- return acc;
2349
- },
2350
- {}
2351
- );
2352
- const finalComponents = Array.from(components.parameters).reduce((acc, [_zodType, component]) => {
2353
- if (component.type === "complete") {
2354
- if (acc[component.ref]) {
2355
- throw new Error(`Parameter "${component.ref}" is already registered`);
2356
- }
2357
- acc[component.ref] = component.paramObject;
2358
- }
2359
- return acc;
2360
- }, customComponents);
2361
- return Object.keys(finalComponents).length ? finalComponents : void 0;
2362
- };
2363
- const createHeaderComponents = (componentsObject, components, documentOptions) => {
2364
- Array.from(components.headers).forEach(([schema, component]) => {
2365
- if (component.type === "manual") {
2366
- createHeaderOrRef(schema, components, documentOptions);
2367
- }
2368
- });
2369
- const headers = componentsObject.headers ?? {};
2370
- const customComponents = Object.entries(headers).reduce((acc, [key, value]) => {
2371
- if (!isAnyZodType(value)) {
2372
- if (acc[key]) {
2373
- throw new Error(`Header Ref "${key}" is already registered`);
2374
- }
2375
- acc[key] = value;
2376
- }
2377
- return acc;
2378
- }, {});
2379
- const finalComponents = Array.from(components.headers).reduce((acc, [_zodType, component]) => {
2380
- if (component.type === "complete") {
2381
- if (acc[component.ref]) {
2382
- throw new Error(`Header "${component.ref}" is already registered`);
2383
- }
2384
- acc[component.ref] = component.headerObject;
2385
- }
2386
- return acc;
2387
- }, customComponents);
2388
- return Object.keys(finalComponents).length ? finalComponents : void 0;
2389
- };
2390
- const createResponseComponents = (components, documentOptions) => {
2391
- Array.from(components.responses).forEach(([schema, component], index) => {
2392
- if (component.type === "manual") {
2393
- createResponse(
2394
- schema,
2395
- components,
2396
- [`component response index ${index}`],
2397
- documentOptions
2398
- );
2399
- }
2400
- });
2401
- const finalComponents = Array.from(components.responses).reduce((acc, [_zodType, component]) => {
2402
- if (component.type === "complete") {
2403
- if (acc[component.ref]) {
2404
- throw new Error(`Response "${component.ref}" is already registered`);
2405
- }
2406
- acc[component.ref] = component.responseObject;
2407
- }
2408
- return acc;
2409
- }, {});
2410
- return Object.keys(finalComponents).length ? finalComponents : void 0;
2411
- };
2412
- const createRequestBodiesComponents = (components, documentOptions) => {
2413
- Array.from(components.requestBodies).forEach(([schema, component], index) => {
2414
- if (component.type === "manual") {
2415
- createRequestBody(
2416
- schema,
2417
- components,
2418
- [`component request body ${index}`],
2419
- documentOptions
2420
- );
2421
- }
2422
- });
2423
- const finalComponents = Array.from(components.requestBodies).reduce((acc, [_zodType, component]) => {
2424
- if (component.type === "complete") {
2425
- if (acc[component.ref]) {
2426
- throw new Error(`RequestBody "${component.ref}" is already registered`);
2427
- }
2428
- acc[component.ref] = component.requestBodyObject;
2429
- }
2430
- return acc;
2431
- }, {});
2432
- return Object.keys(finalComponents).length ? finalComponents : void 0;
2433
- };
2434
- const createCallbackComponents = (components, documentOptions) => {
2435
- Array.from(components.callbacks).forEach(([schema, component], index) => {
2436
- if (component.type === "manual") {
2437
- createCallback(
2438
- schema,
2439
- components,
2440
- [`component callback ${index}`],
2441
- documentOptions
2442
- );
2443
- }
2444
- });
2445
- const finalComponents = Array.from(components.callbacks).reduce((acc, [_zodType, component]) => {
2446
- if (component.type === "complete") {
2447
- if (acc[component.ref]) {
2448
- throw new Error(`Callback "${component.ref}" is already registered`);
2449
- }
2450
- acc[component.ref] = component.callbackObject;
2451
- }
2452
- return acc;
2453
- }, {});
2454
- return Object.keys(finalComponents).length ? finalComponents : void 0;
2455
- };
2456
- exports.createComponents = createComponents;
2457
- exports.createMediaTypeSchema = createMediaTypeSchema;
2458
- exports.createParamOrRef = createParamOrRef;
2459
- exports.createPaths = createPaths;
2460
- exports.createSchema = createSchema;
2461
- exports.createSchemaComponents = createSchemaComponents;
2462
- exports.getDefaultComponents = getDefaultComponents;
2463
- exports.getZodObject = getZodObject;