zod-openapi 5.0.0-beta.9 → 5.0.0

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