zod-openapi 5.0.0-beta.20 → 5.0.0-beta.4

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.
@@ -0,0 +1,782 @@
1
+ const require_zod = require('./zod-i2t01GF0.js');
2
+ const zod_v4_core = require_zod.__toESM(require("zod/v4/core"));
3
+ const zod_v4 = require_zod.__toESM(require("zod/v4"));
4
+ const src_create_componentsSideEffects = require_zod.__toESM(require("./create/componentsSideEffects"));
5
+
6
+ //#region src/create/object.ts
7
+ const unwrapZodObject = (zodType, io, path) => {
8
+ const def = zodType._zod.def;
9
+ switch (def.type) {
10
+ case "object": return zodType;
11
+ case "lazy": return unwrapZodObject(def.getter(), io, path);
12
+ case "pipe": {
13
+ if (io === "input") return unwrapZodObject(def.in, io, path);
14
+ return unwrapZodObject(def.out, io, path);
15
+ }
16
+ }
17
+ throw new Error(`Failed to unwrap ZodObject from type: ${zodType._zod.def.type} at ${path.join(" > ")}`);
18
+ };
19
+ const isRequired = (zodType, io) => {
20
+ if (io === "input") return zodType._zod.optin === void 0;
21
+ return zodType._zod.optout === void 0;
22
+ };
23
+
24
+ //#endregion
25
+ //#region src/create/parameters.ts
26
+ const createParameter = (parameter, location, ctx, path) => {
27
+ const seenParameter = ctx.registry.parameters.seen.get(parameter);
28
+ if (seenParameter) return seenParameter;
29
+ const meta = zod_v4.globalRegistry.get(parameter);
30
+ const name = location?.name ?? meta?.param?.name;
31
+ const inLocation = location?.in ?? meta?.param?.in;
32
+ if (!name || !inLocation) throw new Error(`Parameter at ${path.join(" > ")} is missing \`.meta({ param: { name, in } })\` information`);
33
+ const computedPath = [
34
+ ...path,
35
+ inLocation,
36
+ name
37
+ ].join(" > ");
38
+ const schemaObject = ctx.registry.schemas.setSchema(computedPath, parameter, ctx.io);
39
+ const { id,...rest } = meta?.param ?? {};
40
+ const parameterObject = {
41
+ ...rest,
42
+ name,
43
+ in: inLocation,
44
+ schema: schemaObject
45
+ };
46
+ if (isRequired(parameter, ctx.io)) parameterObject.required = true;
47
+ if (!parameterObject.description && meta?.description) parameterObject.description = meta.description;
48
+ if (id) {
49
+ const ref = { $ref: `#/components/parameters/${id}` };
50
+ ctx.registry.parameters.seen.set(parameter, ref);
51
+ ctx.registry.parameters.ids.set(id, parameterObject);
52
+ return ref;
53
+ }
54
+ ctx.registry.parameters.seen.set(parameter, parameterObject);
55
+ return parameterObject;
56
+ };
57
+ const createManualParameters = (parameters, ctx, path) => {
58
+ if (!parameters) return void 0;
59
+ const parameterObjects = [];
60
+ for (const parameter of parameters) {
61
+ if (require_zod.isAnyZodType(parameter)) {
62
+ const seenParameter = ctx.registry.parameters.seen.get(parameter);
63
+ if (seenParameter) {
64
+ parameterObjects.push(seenParameter);
65
+ continue;
66
+ }
67
+ const paramObject = createParameter(parameter, void 0, ctx, [...path, "parameters"]);
68
+ parameterObjects.push(paramObject);
69
+ continue;
70
+ }
71
+ parameterObjects.push(parameter);
72
+ }
73
+ return parameterObjects;
74
+ };
75
+ const createParameters = (requestParams, ctx, path) => {
76
+ if (!requestParams) return void 0;
77
+ const parameterObjects = [];
78
+ for (const [location, schema] of Object.entries(requestParams ?? {})) {
79
+ if (!schema) continue;
80
+ const zodObject = unwrapZodObject(schema, ctx.io, path);
81
+ for (const [name, zodSchema] of Object.entries(zodObject._zod.def.shape)) {
82
+ const seenParameter = ctx.registry.parameters.seen.get(zodSchema);
83
+ if (seenParameter) {
84
+ parameterObjects.push(seenParameter);
85
+ continue;
86
+ }
87
+ const paramObject = createParameter(zodSchema, {
88
+ in: location,
89
+ name
90
+ }, ctx, [
91
+ ...path,
92
+ location,
93
+ name
94
+ ]);
95
+ parameterObjects.push(paramObject);
96
+ }
97
+ }
98
+ return parameterObjects;
99
+ };
100
+
101
+ //#endregion
102
+ //#region src/create/content.ts
103
+ const createMediaTypeObject = (mediaTypeObject, ctx, path) => {
104
+ const computedPath = path.join(" > ");
105
+ if (require_zod.isAnyZodType(mediaTypeObject.schema)) {
106
+ const schemaObject = ctx.registry.schemas.setSchema(computedPath, mediaTypeObject.schema, ctx.io);
107
+ return {
108
+ ...mediaTypeObject,
109
+ schema: schemaObject
110
+ };
111
+ }
112
+ return mediaTypeObject;
113
+ };
114
+ const createContent = (content, ctx, path) => {
115
+ const contentObject = {};
116
+ for (const [mediaType, mediaTypeObject] of Object.entries(content)) if (mediaTypeObject) contentObject[mediaType] = createMediaTypeObject(mediaTypeObject, ctx, [...path, mediaType]);
117
+ return contentObject;
118
+ };
119
+
120
+ //#endregion
121
+ //#region src/create/requestBody.ts
122
+ const createRequestBody = (requestBody, ctx, path) => {
123
+ if (!requestBody) return void 0;
124
+ const seenRequestBody = ctx.registry.requestBodies.seen.get(requestBody);
125
+ if (seenRequestBody) return seenRequestBody;
126
+ const { content, id,...rest } = requestBody;
127
+ const requestBodyObject = {
128
+ ...rest,
129
+ content: createContent(content, ctx, [...path, "content"])
130
+ };
131
+ if (id) {
132
+ const ref = { $ref: `#/components/requestBodies/${id}` };
133
+ ctx.registry.requestBodies.ids.set(id, requestBodyObject);
134
+ ctx.registry.requestBodies.seen.set(requestBody, ref);
135
+ return ref;
136
+ }
137
+ ctx.registry.requestBodies.seen.set(requestBody, requestBodyObject);
138
+ return requestBodyObject;
139
+ };
140
+
141
+ //#endregion
142
+ //#region src/create/headers.ts
143
+ const createHeader = (header, ctx, path) => {
144
+ const seenHeader = ctx.registry.headers.seen.get(header);
145
+ if (seenHeader) return seenHeader;
146
+ const meta = zod_v4.globalRegistry.get(header);
147
+ const { id,...rest } = meta?.header ?? {};
148
+ const headerObject = rest;
149
+ if (isRequired(header, ctx.io)) headerObject.required = true;
150
+ if (!headerObject.description && meta?.description) headerObject.description = meta.description;
151
+ const computedPath = path.join(" > ");
152
+ headerObject.schema = ctx.registry.schemas.setSchema(computedPath, header, ctx.io);
153
+ if (id) {
154
+ const ref = { $ref: `#/components/headers/${id}` };
155
+ ctx.registry.headers.ids.set(id, headerObject);
156
+ ctx.registry.headers.seen.set(header, ref);
157
+ return ref;
158
+ }
159
+ ctx.registry.headers.seen.set(header, headerObject);
160
+ return headerObject;
161
+ };
162
+ const createHeaders = (headers, ctx, path) => {
163
+ if (!headers) return void 0;
164
+ if (require_zod.isAnyZodType(headers)) {
165
+ const zodObject = unwrapZodObject(headers, ctx.io, path);
166
+ const headersObject = {};
167
+ for (const [key, zodSchema] of Object.entries(zodObject._zod.def.shape)) {
168
+ const header = createHeader(zodSchema, ctx, [...path, key]);
169
+ headersObject[key] = header;
170
+ }
171
+ return headersObject;
172
+ }
173
+ return headers;
174
+ };
175
+
176
+ //#endregion
177
+ //#region src/create/specificationExtension.ts
178
+ const isISpecificationExtension = (key) => key.startsWith("x-");
179
+
180
+ //#endregion
181
+ //#region src/create/responses.ts
182
+ const createResponse = (response, ctx, path) => {
183
+ const seenResponse = ctx.registry.responses.seen.get(response);
184
+ if (seenResponse) return seenResponse;
185
+ const { content, headers, id,...rest } = response;
186
+ const responseObject = rest;
187
+ const maybeHeaders = createHeaders(headers, ctx, [...path, "headers"]);
188
+ if (maybeHeaders) responseObject.headers = maybeHeaders;
189
+ if (content) responseObject.content = createContent(content, ctx, [...path, "content"]);
190
+ if (id) {
191
+ const ref = { $ref: `#/components/responses/${id}` };
192
+ ctx.registry.responses.ids.set(id, responseObject);
193
+ ctx.registry.responses.seen.set(response, ref);
194
+ return ref;
195
+ }
196
+ ctx.registry.responses.seen.set(response, responseObject);
197
+ return responseObject;
198
+ };
199
+ const createResponses = (responses, ctx, path) => {
200
+ if (!responses) return void 0;
201
+ const responsesObject = {};
202
+ for (const [statusCode, response] of Object.entries(responses)) {
203
+ if (!response) continue;
204
+ if (isISpecificationExtension(statusCode)) {
205
+ responsesObject[statusCode] = response;
206
+ continue;
207
+ }
208
+ if ("$ref" in response) {
209
+ responsesObject[statusCode] = response;
210
+ continue;
211
+ }
212
+ const responseObject = createResponse(response, ctx, [...path, statusCode]);
213
+ responsesObject[statusCode] = responseObject;
214
+ }
215
+ return responsesObject;
216
+ };
217
+
218
+ //#endregion
219
+ //#region src/create/paths.ts
220
+ const createOperation = (operationObject, registry, path) => {
221
+ const { parameters, requestParams, requestBody, responses, callbacks,...rest } = operationObject;
222
+ const operation = rest;
223
+ const maybeManualParameters = createManualParameters(parameters, {
224
+ registry,
225
+ io: "input"
226
+ }, [...path, "parameters"]);
227
+ const maybeRequestParams = createParameters(requestParams, {
228
+ registry,
229
+ io: "input"
230
+ }, [...path, "requestParams"]);
231
+ if (maybeRequestParams || maybeManualParameters) operation.parameters = [...maybeRequestParams ?? [], ...maybeManualParameters ?? []];
232
+ const maybeRequestBody = createRequestBody(requestBody, {
233
+ registry,
234
+ io: "input"
235
+ }, [...path, "requestBody"]);
236
+ if (maybeRequestBody) operation.requestBody = maybeRequestBody;
237
+ const maybeResponses = createResponses(responses, {
238
+ registry,
239
+ io: "output"
240
+ }, [...path, "responses"]);
241
+ if (maybeResponses) operation.responses = maybeResponses;
242
+ const maybeCallbacks = createCallbacks(callbacks, registry, [...path, "callbacks"]);
243
+ if (maybeCallbacks) operation.callbacks = maybeCallbacks;
244
+ return operation;
245
+ };
246
+ const createPathItem = (pathItem, registry, path) => {
247
+ const pathItemObject = {};
248
+ const { id,...rest } = pathItem;
249
+ for (const [key, value] of Object.entries(rest)) {
250
+ if (key === "get" || key === "put" || key === "post" || key === "delete" || key === "options" || key === "head" || key === "patch" || key === "trace") {
251
+ pathItemObject[key] = createOperation(value, registry, [...path, key]);
252
+ continue;
253
+ }
254
+ if (key === "parameters") {
255
+ pathItemObject[key] = createManualParameters(value, {
256
+ registry,
257
+ io: "input"
258
+ }, [...path, key]);
259
+ continue;
260
+ }
261
+ pathItemObject[key] = value;
262
+ }
263
+ if (id) {
264
+ const ref = { $ref: `#/components/pathItems/${id}` };
265
+ registry.pathItems.ids.set(id, pathItemObject);
266
+ registry.pathItems.seen.set(pathItem, ref);
267
+ return ref;
268
+ }
269
+ registry.pathItems.seen.set(pathItem, pathItemObject);
270
+ return pathItemObject;
271
+ };
272
+ const createPaths = (paths, registry, path) => {
273
+ if (!paths) return void 0;
274
+ const pathsObject = {};
275
+ for (const [singlePath, pathItemObject] of Object.entries(paths)) {
276
+ if (isISpecificationExtension(singlePath)) {
277
+ pathsObject[singlePath] = pathItemObject;
278
+ continue;
279
+ }
280
+ pathsObject[singlePath] = createPathItem(pathItemObject, registry, [...path, singlePath]);
281
+ }
282
+ return pathsObject;
283
+ };
284
+
285
+ //#endregion
286
+ //#region src/create/callbacks.ts
287
+ const createCallback = (callbackObject, registry, path) => {
288
+ const seenCallback = registry.callbacks.seen.get(callbackObject);
289
+ if (seenCallback) return seenCallback;
290
+ const { id,...rest } = callbackObject;
291
+ const callback = {};
292
+ for (const [name, pathItem] of Object.entries(rest)) {
293
+ if (isISpecificationExtension(name)) {
294
+ callback[name] = pathItem;
295
+ continue;
296
+ }
297
+ callback[name] = createPathItem(pathItem, registry, [...path, name]);
298
+ }
299
+ if (id) {
300
+ const ref = { $ref: `#/components/callbacks/${id}` };
301
+ registry.callbacks.ids.set(id, callback);
302
+ registry.callbacks.seen.set(callbackObject, ref);
303
+ return ref;
304
+ }
305
+ registry.callbacks.seen.set(callbackObject, callback);
306
+ return callback;
307
+ };
308
+ const createCallbacks = (callbacks, registry, path) => {
309
+ if (!callbacks) return void 0;
310
+ const callbacksObject = {};
311
+ for (const [name, value] of Object.entries(callbacks)) {
312
+ if (isISpecificationExtension(name)) {
313
+ callbacksObject[name] = value;
314
+ continue;
315
+ }
316
+ callbacksObject[name] = createCallback(value, registry, [...path, name]);
317
+ }
318
+ return callbacksObject;
319
+ };
320
+
321
+ //#endregion
322
+ //#region src/create/schema/override.ts
323
+ const override = (ctx) => {
324
+ const def = ctx.zodSchema._zod.def;
325
+ switch (def.type) {
326
+ case "bigint": {
327
+ ctx.jsonSchema.type = "integer";
328
+ ctx.jsonSchema.format = "int64";
329
+ break;
330
+ }
331
+ case "union": {
332
+ if ("discriminator" in def && typeof def.discriminator === "string") {
333
+ ctx.jsonSchema.oneOf = ctx.jsonSchema.anyOf;
334
+ delete ctx.jsonSchema.anyOf;
335
+ ctx.jsonSchema.type = "object";
336
+ ctx.jsonSchema.discriminator = { propertyName: def.discriminator };
337
+ const mapping = {};
338
+ for (const [index, obj] of Object.entries(ctx.jsonSchema.oneOf)) {
339
+ const ref = obj.$ref;
340
+ if (!ref) return;
341
+ const discriminatorValues = def.options[Number(index)]._zod.propValues?.[def.discriminator];
342
+ if (!discriminatorValues?.size) return;
343
+ for (const value of [...discriminatorValues ?? []]) {
344
+ if (typeof value !== "string") return;
345
+ mapping[value] = ref;
346
+ }
347
+ }
348
+ ctx.jsonSchema.discriminator.mapping = mapping;
349
+ }
350
+ const meta = ctx.zodSchema.meta();
351
+ if (typeof meta?.unionOneOf === "boolean") {
352
+ if (meta.unionOneOf) {
353
+ ctx.jsonSchema.oneOf = ctx.jsonSchema.anyOf;
354
+ delete ctx.jsonSchema.anyOf;
355
+ }
356
+ delete ctx.jsonSchema.unionOneOf;
357
+ }
358
+ break;
359
+ }
360
+ case "date": {
361
+ ctx.jsonSchema.type = "string";
362
+ break;
363
+ }
364
+ case "literal": {
365
+ if (def.values.includes(void 0)) break;
366
+ break;
367
+ }
368
+ }
369
+ };
370
+ const validate = (ctx, opts) => {
371
+ if (Object.keys(ctx.jsonSchema).length) return;
372
+ const def = ctx.zodSchema._zod.def;
373
+ const allowEmptySchema = opts.allowEmptySchema?.[def.type];
374
+ if (allowEmptySchema === true || allowEmptySchema?.[ctx.io]) return;
375
+ switch (def.type) {
376
+ case "any": return;
377
+ case "unknown": return;
378
+ case "pipe": {
379
+ if (ctx.io === "output") throw new Error("Zod transform schemas are not supported in output schemas. Please use `.overwrite()` or wrap the schema in a `.pipe()`");
380
+ return;
381
+ }
382
+ case "transform": {
383
+ if (ctx.io === "output") return;
384
+ break;
385
+ }
386
+ case "literal": {
387
+ if (def.values.includes(void 0)) throw new Error("Zod literal schemas cannot include `undefined` as a value. Please use `z.undefined()` or `.optional()` instead.");
388
+ return;
389
+ }
390
+ }
391
+ throw new Error(`Zod schema of type \`${def.type}\` cannot be represented in OpenAPI. Please assign it metadata with \`.meta()\``);
392
+ };
393
+
394
+ //#endregion
395
+ //#region src/create/schema/rename.ts
396
+ const renameComponents = (components, outputIds, ctx) => {
397
+ const componentsToRename = /* @__PURE__ */ new Map();
398
+ const componentDependencies = /* @__PURE__ */ new Map();
399
+ const stringifiedComponents = /* @__PURE__ */ new Map();
400
+ for (const [key, value] of Object.entries(components)) {
401
+ const stringified = JSON.stringify(value);
402
+ const matches = stringified.matchAll(/"#\/components\/schemas\/([^"]+)"/g);
403
+ const dependencies = /* @__PURE__ */ new Set();
404
+ for (const match of matches) {
405
+ const dep = match[1];
406
+ if (dep !== key) dependencies.add(dep);
407
+ }
408
+ stringifiedComponents.set(key, stringified);
409
+ componentDependencies.set(key, { dependencies });
410
+ }
411
+ for (const [key] of stringifiedComponents) {
412
+ const registeredComponent = ctx.registry.schemas.ids.get(key);
413
+ if (!registeredComponent) continue;
414
+ if (isDependencyPure(componentDependencies, stringifiedComponents, ctx.registry, key)) continue;
415
+ const newName = outputIds.get(key) ?? `${key}Output`;
416
+ componentsToRename.set(key, newName);
417
+ components[newName] = components[key];
418
+ delete components[key];
419
+ continue;
420
+ }
421
+ return componentsToRename;
422
+ };
423
+ const isDependencyPure = (componentDependencies, stringifiedComponents, registry, key, visited = /* @__PURE__ */ new Set()) => {
424
+ if (visited.has(key)) return true;
425
+ const dependencies = componentDependencies.get(key);
426
+ if (dependencies.pure !== void 0) return dependencies.pure;
427
+ const stringified = stringifiedComponents.get(key);
428
+ const component = registry.schemas.ids.get(key);
429
+ if (component && stringified !== JSON.stringify(component)) {
430
+ dependencies.pure = false;
431
+ return false;
432
+ }
433
+ visited.add(key);
434
+ const result = [...dependencies.dependencies].every((dep) => isDependencyPure(componentDependencies, stringifiedComponents, registry, dep, new Set(visited)));
435
+ dependencies.pure = result;
436
+ return result;
437
+ };
438
+
439
+ //#endregion
440
+ //#region src/create/schema/schema.ts
441
+ const deleteZodOpenApiMeta = (jsonSchema) => {
442
+ delete jsonSchema.param;
443
+ delete jsonSchema.header;
444
+ delete jsonSchema.unusedIO;
445
+ delete jsonSchema.override;
446
+ delete jsonSchema.outputId;
447
+ };
448
+ const createSchema = (schema, ctx = {
449
+ registry: createRegistry(),
450
+ io: "output",
451
+ opts: {}
452
+ }) => {
453
+ ctx.registry ??= createRegistry();
454
+ ctx.opts ??= {};
455
+ ctx.io ??= "output";
456
+ const registrySchemas = Object.fromEntries(ctx.registry.schemas[ctx.io]);
457
+ const schemas = { zodOpenApiCreateSchema: { zodType: schema } };
458
+ Object.assign(schemas, registrySchemas);
459
+ const jsonSchemas = createSchemas(schemas, {
460
+ registry: ctx.registry,
461
+ io: ctx.io,
462
+ opts: ctx.opts
463
+ });
464
+ return {
465
+ schema: jsonSchemas.schemas.zodOpenApiCreateSchema,
466
+ components: jsonSchemas.components
467
+ };
468
+ };
469
+ const createSchemas = (schemas, ctx) => {
470
+ const entries = {};
471
+ for (const [name, { zodType }] of Object.entries(schemas)) entries[name] = zodType;
472
+ const schemaRegistry = (0, zod_v4.object)(entries);
473
+ const outputIds = /* @__PURE__ */ new Map();
474
+ const jsonSchema = (0, zod_v4.toJSONSchema)(schemaRegistry, {
475
+ override(context) {
476
+ const meta = context.zodSchema.meta();
477
+ if (meta?.outputId && meta?.id) outputIds.set(meta.id, meta.outputId);
478
+ if (context.jsonSchema.$ref) return;
479
+ const enrichedContext = {
480
+ ...context,
481
+ io: ctx.io
482
+ };
483
+ override(enrichedContext);
484
+ if (typeof ctx.opts.override === "function") ctx.opts.override(enrichedContext);
485
+ if (typeof meta?.override === "function") {
486
+ meta.override(enrichedContext);
487
+ delete context.jsonSchema.override;
488
+ }
489
+ if (typeof meta?.override === "object" && meta.override !== null) {
490
+ Object.assign(context.jsonSchema, meta.override);
491
+ delete context.jsonSchema.override;
492
+ }
493
+ delete context.jsonSchema.$schema;
494
+ delete context.jsonSchema.id;
495
+ deleteZodOpenApiMeta(context.jsonSchema);
496
+ validate(enrichedContext, ctx.opts);
497
+ },
498
+ io: ctx.io,
499
+ unrepresentable: "any",
500
+ reused: ctx.opts.reused,
501
+ cycles: ctx.opts.cycles
502
+ });
503
+ const components = jsonSchema.$defs ?? {};
504
+ const dynamicComponents = /* @__PURE__ */ new Map();
505
+ for (const [key, value] of Object.entries(components)) if (/^__schema\d+$/.test(key)) {
506
+ const newName = `__schema${ctx.registry.schemas.dynamicSchemaCount++}`;
507
+ dynamicComponents.set(key, `"#/components/schemas/${newName}"`);
508
+ if (newName !== key) {
509
+ components[newName] = value;
510
+ delete components[key];
511
+ }
512
+ }
513
+ const parsedJsonSchema = JSON.parse(JSON.stringify(jsonSchema).replace(/"#\/\$defs\/([^"]+)"/g, (_, match) => {
514
+ const dynamic = dynamicComponents.get(match);
515
+ if (dynamic) return dynamic;
516
+ const manualComponent = ctx.registry.schemas.manual.get(match);
517
+ if (manualComponent) manualComponent.io[ctx.io].used++;
518
+ return `"#/components/schemas/${match}"`;
519
+ }));
520
+ for (const [key, value] of ctx.registry.schemas.manual) if (value.io[ctx.io].used === 1 && parsedJsonSchema.properties?.[value.identifier] && parsedJsonSchema.$defs?.[key]) {
521
+ parsedJsonSchema.properties[value.identifier] = parsedJsonSchema.$defs[key];
522
+ delete parsedJsonSchema.$defs[key];
523
+ continue;
524
+ }
525
+ const renamedComponents = renameComponents(parsedJsonSchema.$defs ?? {}, outputIds, ctx);
526
+ if (!renamedComponents.size) return {
527
+ schemas: parsedJsonSchema.properties,
528
+ components: parsedJsonSchema.$defs ?? {}
529
+ };
530
+ const renamedJsonSchema = JSON.parse(JSON.stringify(parsedJsonSchema).replace(/"#\/components\/schemas\/([^"]+)"/g, (_, match) => {
531
+ const replacement = renamedComponents.get(match);
532
+ if (replacement) return `"#/components/schemas/${replacement}"`;
533
+ return `"#/components/schemas/${match}"`;
534
+ }));
535
+ return {
536
+ schemas: renamedJsonSchema.properties,
537
+ components: renamedJsonSchema.$defs ?? {}
538
+ };
539
+ };
540
+
541
+ //#endregion
542
+ //#region src/create/components.ts
543
+ const createRegistry = (components) => {
544
+ const registry = {
545
+ schemas: {
546
+ dynamicSchemaCount: 0,
547
+ input: /* @__PURE__ */ new Map(),
548
+ output: /* @__PURE__ */ new Map(),
549
+ ids: /* @__PURE__ */ new Map(),
550
+ manual: /* @__PURE__ */ new Map(),
551
+ setSchema: (key, schema, io) => {
552
+ const schemaObject = {};
553
+ registry.schemas[io].set(key, {
554
+ schemaObject,
555
+ zodType: schema
556
+ });
557
+ return schemaObject;
558
+ }
559
+ },
560
+ headers: {
561
+ ids: /* @__PURE__ */ new Map(),
562
+ seen: /* @__PURE__ */ new WeakMap()
563
+ },
564
+ requestBodies: {
565
+ ids: /* @__PURE__ */ new Map(),
566
+ seen: /* @__PURE__ */ new WeakMap()
567
+ },
568
+ responses: {
569
+ ids: /* @__PURE__ */ new Map(),
570
+ seen: /* @__PURE__ */ new WeakMap()
571
+ },
572
+ parameters: {
573
+ ids: /* @__PURE__ */ new Map(),
574
+ seen: /* @__PURE__ */ new WeakMap()
575
+ },
576
+ callbacks: {
577
+ ids: /* @__PURE__ */ new Map(),
578
+ seen: /* @__PURE__ */ new WeakMap()
579
+ },
580
+ pathItems: {
581
+ ids: /* @__PURE__ */ new Map(),
582
+ seen: /* @__PURE__ */ new WeakMap()
583
+ }
584
+ };
585
+ (0, src_create_componentsSideEffects.registerSchemas)(components?.schemas, registry);
586
+ registerParameters(components?.parameters, registry);
587
+ registerHeaders(components?.headers, registry);
588
+ registerResponses(components?.responses, registry);
589
+ registerPathItems(components?.pathItems, registry);
590
+ registerRequestBodies(components?.requestBodies, registry);
591
+ registerCallbacks(components?.callbacks, registry);
592
+ return registry;
593
+ };
594
+ const registerParameters = (parameters, registry) => {
595
+ if (!parameters) return;
596
+ for (const [key, schema] of Object.entries(parameters)) {
597
+ if (registry.parameters.ids.has(key)) throw new Error(`Parameter "${key}" is already registered`);
598
+ if (require_zod.isAnyZodType(schema)) {
599
+ const path = [
600
+ "components",
601
+ "parameters",
602
+ key
603
+ ];
604
+ const paramObject = createParameter(schema, void 0, {
605
+ registry,
606
+ io: "input"
607
+ }, path);
608
+ registry.parameters.ids.set(key, paramObject);
609
+ registry.parameters.seen.set(schema, paramObject);
610
+ continue;
611
+ }
612
+ registry.parameters.ids.set(key, schema);
613
+ }
614
+ };
615
+ const registerHeaders = (headers, registry) => {
616
+ if (!headers) return;
617
+ for (const [key, schema] of Object.entries(headers)) {
618
+ if (registry.headers.ids.has(key)) throw new Error(`Header "${key}" is already registered`);
619
+ if (require_zod.isAnyZodType(schema)) {
620
+ const path = [
621
+ "components",
622
+ "headers",
623
+ key
624
+ ];
625
+ const headerObject = createHeader(schema, {
626
+ registry,
627
+ io: "output"
628
+ }, path);
629
+ registry.headers.ids.set(key, headerObject);
630
+ registry.headers.seen.set(schema, headerObject);
631
+ continue;
632
+ }
633
+ registry.headers.ids.set(key, schema);
634
+ }
635
+ };
636
+ const registerResponses = (responses, registry) => {
637
+ if (!responses) return;
638
+ for (const [key, schema] of Object.entries(responses)) {
639
+ const path = [
640
+ "components",
641
+ "responses",
642
+ key
643
+ ];
644
+ if (registry.responses.ids.has(key)) throw new Error(`Response "${key}" is already registered`);
645
+ const responseObject = createResponse(schema, {
646
+ registry,
647
+ io: "output"
648
+ }, path);
649
+ registry.responses.ids.set(key, responseObject);
650
+ registry.responses.seen.set(schema, responseObject);
651
+ }
652
+ };
653
+ const registerRequestBodies = (requestBodies, registry) => {
654
+ if (!requestBodies) return;
655
+ for (const [key, schema] of Object.entries(requestBodies)) {
656
+ if (registry.requestBodies.ids.has(key)) throw new Error(`RequestBody "${key}" is already registered`);
657
+ if (require_zod.isAnyZodType(schema)) {
658
+ const path = [
659
+ "components",
660
+ "requestBodies",
661
+ key
662
+ ];
663
+ const requestBodyObject = createRequestBody(schema, {
664
+ registry,
665
+ io: "input"
666
+ }, path);
667
+ registry.requestBodies.ids.set(key, requestBodyObject);
668
+ continue;
669
+ }
670
+ registry.requestBodies.ids.set(key, schema);
671
+ }
672
+ };
673
+ const registerCallbacks = (callbacks, registry) => {
674
+ if (!callbacks) return;
675
+ for (const [key, schema] of Object.entries(callbacks)) {
676
+ if (registry.callbacks.ids.has(key)) throw new Error(`Callback "${key}" is already registered`);
677
+ const path = [
678
+ "components",
679
+ "callbacks",
680
+ key
681
+ ];
682
+ const callbackObject = createCallback(schema, registry, path);
683
+ registry.callbacks.ids.set(key, callbackObject);
684
+ registry.callbacks.seen.set(schema, callbackObject);
685
+ }
686
+ };
687
+ const registerPathItems = (pathItems, registry) => {
688
+ if (!pathItems) return;
689
+ for (const [key, schema] of Object.entries(pathItems)) {
690
+ if (registry.pathItems.ids.has(key)) throw new Error(`PathItem "${key}" is already registered`);
691
+ const path = [
692
+ "components",
693
+ "pathItems",
694
+ key
695
+ ];
696
+ const pathItemObject = createPathItem(schema, registry, path);
697
+ registry.pathItems.ids.set(key, pathItemObject);
698
+ registry.pathItems.seen.set(schema, pathItemObject);
699
+ continue;
700
+ }
701
+ };
702
+ const createIOSchemas = (ctx) => {
703
+ const { schemas, components } = createSchemas(Object.fromEntries(ctx.registry.schemas[ctx.io]), ctx);
704
+ for (const [key, schema] of Object.entries(components)) ctx.registry.schemas.ids.set(key, schema);
705
+ for (const [key, schema] of Object.entries(schemas)) {
706
+ const ioSchema = ctx.registry.schemas[ctx.io].get(key);
707
+ if (ioSchema) Object.assign(ioSchema.schemaObject, schema);
708
+ }
709
+ };
710
+ const createManualSchemas = (registry) => {
711
+ for (const [key, value] of registry.schemas.manual) if (value.io.input.used === 1) {
712
+ const io = zod_v4_core.globalRegistry.get(value.zodType)?.unusedIO ?? "output";
713
+ const schema = value.io[io].schemaObject;
714
+ registry.schemas.ids.set(key, schema);
715
+ }
716
+ };
717
+ const createComponents = (registry, opts) => {
718
+ createIOSchemas({
719
+ registry,
720
+ io: "input",
721
+ opts
722
+ });
723
+ createIOSchemas({
724
+ registry,
725
+ io: "output",
726
+ opts
727
+ });
728
+ createManualSchemas(registry);
729
+ const components = {};
730
+ if (registry.schemas.ids.size > 0) components.schemas = Object.fromEntries(registry.schemas.ids);
731
+ if (registry.headers.ids.size > 0) components.headers = Object.fromEntries(registry.headers.ids);
732
+ if (registry.requestBodies.ids.size > 0) components.requestBodies = Object.fromEntries(registry.requestBodies.ids);
733
+ if (registry.responses.ids.size > 0) components.responses = Object.fromEntries(registry.responses.ids);
734
+ if (registry.parameters.ids.size > 0) components.parameters = Object.fromEntries(registry.parameters.ids);
735
+ if (registry.callbacks.ids.size > 0) components.callbacks = Object.fromEntries(registry.callbacks.ids);
736
+ if (registry.pathItems.ids.size > 0) components.pathItems = Object.fromEntries(registry.pathItems.ids);
737
+ return components;
738
+ };
739
+
740
+ //#endregion
741
+ Object.defineProperty(exports, 'createComponents', {
742
+ enumerable: true,
743
+ get: function () {
744
+ return createComponents;
745
+ }
746
+ });
747
+ Object.defineProperty(exports, 'createMediaTypeObject', {
748
+ enumerable: true,
749
+ get: function () {
750
+ return createMediaTypeObject;
751
+ }
752
+ });
753
+ Object.defineProperty(exports, 'createParameter', {
754
+ enumerable: true,
755
+ get: function () {
756
+ return createParameter;
757
+ }
758
+ });
759
+ Object.defineProperty(exports, 'createPaths', {
760
+ enumerable: true,
761
+ get: function () {
762
+ return createPaths;
763
+ }
764
+ });
765
+ Object.defineProperty(exports, 'createRegistry', {
766
+ enumerable: true,
767
+ get: function () {
768
+ return createRegistry;
769
+ }
770
+ });
771
+ Object.defineProperty(exports, 'createSchema', {
772
+ enumerable: true,
773
+ get: function () {
774
+ return createSchema;
775
+ }
776
+ });
777
+ Object.defineProperty(exports, 'unwrapZodObject', {
778
+ enumerable: true,
779
+ get: function () {
780
+ return unwrapZodObject;
781
+ }
782
+ });