@aklinker1/zeta 2.1.1 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/zod-schema-adapter.d.mts +17 -0
- package/dist/adapters/zod-schema-adapter.mjs +726 -0
- package/dist/client.d.mts +71 -0
- package/dist/client.mjs +73 -0
- package/dist/index.d.mts +316 -0
- package/dist/index.mjs +1236 -0
- package/dist/schema-IKHh0I39.d.mts +168 -0
- package/dist/schema.d.mts +2 -0
- package/dist/schema.mjs +151 -0
- package/dist/serialization-C8e7ECQ2.mjs +56 -0
- package/dist/testing.d.mts +26 -0
- package/dist/testing.mjs +52 -0
- package/dist/transports/bun-transport.d.mts +7 -0
- package/dist/transports/bun-transport.mjs +14 -0
- package/dist/transports/deno-transport.d.mts +6 -0
- package/dist/transports/deno-transport.mjs +13 -0
- package/dist/types-D9oRVe1E.d.mts +698 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +1 -0
- package/package.json +39 -17
- package/src/adapters/zod-schema-adapter.ts +0 -29
- package/src/app.ts +0 -479
- package/src/client.ts +0 -184
- package/src/errors.ts +0 -529
- package/src/index.ts +0 -5
- package/src/internal/compile-fetch-function.ts +0 -166
- package/src/internal/compile-route-handler.ts +0 -194
- package/src/internal/context.ts +0 -65
- package/src/internal/serialization.ts +0 -91
- package/src/internal/utils.ts +0 -191
- package/src/meta.ts +0 -14
- package/src/open-api.ts +0 -273
- package/src/schema.ts +0 -271
- package/src/status.ts +0 -143
- package/src/testing.ts +0 -62
- package/src/transports/bun-transport.ts +0 -17
- package/src/transports/deno-transport.ts +0 -13
- package/src/types.ts +0 -1102
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
//#region node_modules/zod/v4/core/util.js
|
|
2
|
+
function getEnumValues(entries) {
|
|
3
|
+
const numericValues = Object.values(entries).filter((v) => typeof v === "number");
|
|
4
|
+
return Object.entries(entries).filter(([k, _]) => numericValues.indexOf(+k) === -1).map(([_, v]) => v);
|
|
5
|
+
}
|
|
6
|
+
function cached(getter) {
|
|
7
|
+
return { get value() {
|
|
8
|
+
{
|
|
9
|
+
const value = getter();
|
|
10
|
+
Object.defineProperty(this, "value", { value });
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
throw new Error("cached value already set");
|
|
14
|
+
} };
|
|
15
|
+
}
|
|
16
|
+
"captureStackTrace" in Error && Error.captureStackTrace;
|
|
17
|
+
cached(() => {
|
|
18
|
+
if (typeof navigator !== "undefined" && navigator?.userAgent?.includes("Cloudflare")) return false;
|
|
19
|
+
try {
|
|
20
|
+
new Function("");
|
|
21
|
+
return true;
|
|
22
|
+
} catch (_) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, -Number.MAX_VALUE, Number.MAX_VALUE;
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region node_modules/zod/v4/core/registries.js
|
|
29
|
+
var $ZodRegistry = class {
|
|
30
|
+
constructor() {
|
|
31
|
+
this._map = /* @__PURE__ */ new WeakMap();
|
|
32
|
+
this._idmap = /* @__PURE__ */ new Map();
|
|
33
|
+
}
|
|
34
|
+
add(schema, ..._meta) {
|
|
35
|
+
const meta = _meta[0];
|
|
36
|
+
this._map.set(schema, meta);
|
|
37
|
+
if (meta && typeof meta === "object" && "id" in meta) {
|
|
38
|
+
if (this._idmap.has(meta.id)) throw new Error(`ID ${meta.id} already exists in the registry`);
|
|
39
|
+
this._idmap.set(meta.id, schema);
|
|
40
|
+
}
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
clear() {
|
|
44
|
+
this._map = /* @__PURE__ */ new WeakMap();
|
|
45
|
+
this._idmap = /* @__PURE__ */ new Map();
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
remove(schema) {
|
|
49
|
+
const meta = this._map.get(schema);
|
|
50
|
+
if (meta && typeof meta === "object" && "id" in meta) this._idmap.delete(meta.id);
|
|
51
|
+
this._map.delete(schema);
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
get(schema) {
|
|
55
|
+
const p = schema._zod.parent;
|
|
56
|
+
if (p) {
|
|
57
|
+
const pm = { ...this.get(p) ?? {} };
|
|
58
|
+
delete pm.id;
|
|
59
|
+
const f = {
|
|
60
|
+
...pm,
|
|
61
|
+
...this._map.get(schema)
|
|
62
|
+
};
|
|
63
|
+
return Object.keys(f).length ? f : void 0;
|
|
64
|
+
}
|
|
65
|
+
return this._map.get(schema);
|
|
66
|
+
}
|
|
67
|
+
has(schema) {
|
|
68
|
+
return this._map.has(schema);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
function registry() {
|
|
72
|
+
return new $ZodRegistry();
|
|
73
|
+
}
|
|
74
|
+
const globalRegistry = /* @__PURE__ */ registry();
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region node_modules/zod/v4/core/to-json-schema.js
|
|
77
|
+
var JSONSchemaGenerator = class {
|
|
78
|
+
constructor(params) {
|
|
79
|
+
this.counter = 0;
|
|
80
|
+
this.metadataRegistry = params?.metadata ?? globalRegistry;
|
|
81
|
+
this.target = params?.target ?? "draft-2020-12";
|
|
82
|
+
this.unrepresentable = params?.unrepresentable ?? "throw";
|
|
83
|
+
this.override = params?.override ?? (() => {});
|
|
84
|
+
this.io = params?.io ?? "output";
|
|
85
|
+
this.seen = /* @__PURE__ */ new Map();
|
|
86
|
+
}
|
|
87
|
+
process(schema, _params = {
|
|
88
|
+
path: [],
|
|
89
|
+
schemaPath: []
|
|
90
|
+
}) {
|
|
91
|
+
var _a;
|
|
92
|
+
const def = schema._zod.def;
|
|
93
|
+
const formatMap = {
|
|
94
|
+
guid: "uuid",
|
|
95
|
+
url: "uri",
|
|
96
|
+
datetime: "date-time",
|
|
97
|
+
json_string: "json-string",
|
|
98
|
+
regex: ""
|
|
99
|
+
};
|
|
100
|
+
const seen = this.seen.get(schema);
|
|
101
|
+
if (seen) {
|
|
102
|
+
seen.count++;
|
|
103
|
+
if (_params.schemaPath.includes(schema)) seen.cycle = _params.path;
|
|
104
|
+
return seen.schema;
|
|
105
|
+
}
|
|
106
|
+
const result = {
|
|
107
|
+
schema: {},
|
|
108
|
+
count: 1,
|
|
109
|
+
cycle: void 0,
|
|
110
|
+
path: _params.path
|
|
111
|
+
};
|
|
112
|
+
this.seen.set(schema, result);
|
|
113
|
+
const overrideSchema = schema._zod.toJSONSchema?.();
|
|
114
|
+
if (overrideSchema) result.schema = overrideSchema;
|
|
115
|
+
else {
|
|
116
|
+
const params = {
|
|
117
|
+
..._params,
|
|
118
|
+
schemaPath: [..._params.schemaPath, schema],
|
|
119
|
+
path: _params.path
|
|
120
|
+
};
|
|
121
|
+
const parent = schema._zod.parent;
|
|
122
|
+
if (parent) {
|
|
123
|
+
result.ref = parent;
|
|
124
|
+
this.process(parent, params);
|
|
125
|
+
this.seen.get(parent).isParent = true;
|
|
126
|
+
} else {
|
|
127
|
+
const _json = result.schema;
|
|
128
|
+
switch (def.type) {
|
|
129
|
+
case "string": {
|
|
130
|
+
const json = _json;
|
|
131
|
+
json.type = "string";
|
|
132
|
+
const { minimum, maximum, format, patterns, contentEncoding } = schema._zod.bag;
|
|
133
|
+
if (typeof minimum === "number") json.minLength = minimum;
|
|
134
|
+
if (typeof maximum === "number") json.maxLength = maximum;
|
|
135
|
+
if (format) {
|
|
136
|
+
json.format = formatMap[format] ?? format;
|
|
137
|
+
if (json.format === "") delete json.format;
|
|
138
|
+
}
|
|
139
|
+
if (contentEncoding) json.contentEncoding = contentEncoding;
|
|
140
|
+
if (patterns && patterns.size > 0) {
|
|
141
|
+
const regexes = [...patterns];
|
|
142
|
+
if (regexes.length === 1) json.pattern = regexes[0].source;
|
|
143
|
+
else if (regexes.length > 1) result.schema.allOf = [...regexes.map((regex) => ({
|
|
144
|
+
...this.target === "draft-7" || this.target === "draft-4" || this.target === "openapi-3.0" ? { type: "string" } : {},
|
|
145
|
+
pattern: regex.source
|
|
146
|
+
}))];
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case "number": {
|
|
151
|
+
const json = _json;
|
|
152
|
+
const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum } = schema._zod.bag;
|
|
153
|
+
if (typeof format === "string" && format.includes("int")) json.type = "integer";
|
|
154
|
+
else json.type = "number";
|
|
155
|
+
if (typeof exclusiveMinimum === "number") if (this.target === "draft-4" || this.target === "openapi-3.0") {
|
|
156
|
+
json.minimum = exclusiveMinimum;
|
|
157
|
+
json.exclusiveMinimum = true;
|
|
158
|
+
} else json.exclusiveMinimum = exclusiveMinimum;
|
|
159
|
+
if (typeof minimum === "number") {
|
|
160
|
+
json.minimum = minimum;
|
|
161
|
+
if (typeof exclusiveMinimum === "number" && this.target !== "draft-4") if (exclusiveMinimum >= minimum) delete json.minimum;
|
|
162
|
+
else delete json.exclusiveMinimum;
|
|
163
|
+
}
|
|
164
|
+
if (typeof exclusiveMaximum === "number") if (this.target === "draft-4" || this.target === "openapi-3.0") {
|
|
165
|
+
json.maximum = exclusiveMaximum;
|
|
166
|
+
json.exclusiveMaximum = true;
|
|
167
|
+
} else json.exclusiveMaximum = exclusiveMaximum;
|
|
168
|
+
if (typeof maximum === "number") {
|
|
169
|
+
json.maximum = maximum;
|
|
170
|
+
if (typeof exclusiveMaximum === "number" && this.target !== "draft-4") if (exclusiveMaximum <= maximum) delete json.maximum;
|
|
171
|
+
else delete json.exclusiveMaximum;
|
|
172
|
+
}
|
|
173
|
+
if (typeof multipleOf === "number") json.multipleOf = multipleOf;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
case "boolean": {
|
|
177
|
+
const json = _json;
|
|
178
|
+
json.type = "boolean";
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
case "bigint":
|
|
182
|
+
if (this.unrepresentable === "throw") throw new Error("BigInt cannot be represented in JSON Schema");
|
|
183
|
+
break;
|
|
184
|
+
case "symbol":
|
|
185
|
+
if (this.unrepresentable === "throw") throw new Error("Symbols cannot be represented in JSON Schema");
|
|
186
|
+
break;
|
|
187
|
+
case "null":
|
|
188
|
+
if (this.target === "openapi-3.0") {
|
|
189
|
+
_json.type = "string";
|
|
190
|
+
_json.nullable = true;
|
|
191
|
+
_json.enum = [null];
|
|
192
|
+
} else _json.type = "null";
|
|
193
|
+
break;
|
|
194
|
+
case "any": break;
|
|
195
|
+
case "unknown": break;
|
|
196
|
+
case "undefined":
|
|
197
|
+
if (this.unrepresentable === "throw") throw new Error("Undefined cannot be represented in JSON Schema");
|
|
198
|
+
break;
|
|
199
|
+
case "void":
|
|
200
|
+
if (this.unrepresentable === "throw") throw new Error("Void cannot be represented in JSON Schema");
|
|
201
|
+
break;
|
|
202
|
+
case "never":
|
|
203
|
+
_json.not = {};
|
|
204
|
+
break;
|
|
205
|
+
case "date":
|
|
206
|
+
if (this.unrepresentable === "throw") throw new Error("Date cannot be represented in JSON Schema");
|
|
207
|
+
break;
|
|
208
|
+
case "array": {
|
|
209
|
+
const json = _json;
|
|
210
|
+
const { minimum, maximum } = schema._zod.bag;
|
|
211
|
+
if (typeof minimum === "number") json.minItems = minimum;
|
|
212
|
+
if (typeof maximum === "number") json.maxItems = maximum;
|
|
213
|
+
json.type = "array";
|
|
214
|
+
json.items = this.process(def.element, {
|
|
215
|
+
...params,
|
|
216
|
+
path: [...params.path, "items"]
|
|
217
|
+
});
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
case "object": {
|
|
221
|
+
const json = _json;
|
|
222
|
+
json.type = "object";
|
|
223
|
+
json.properties = {};
|
|
224
|
+
const shape = def.shape;
|
|
225
|
+
for (const key in shape) json.properties[key] = this.process(shape[key], {
|
|
226
|
+
...params,
|
|
227
|
+
path: [
|
|
228
|
+
...params.path,
|
|
229
|
+
"properties",
|
|
230
|
+
key
|
|
231
|
+
]
|
|
232
|
+
});
|
|
233
|
+
const allKeys = new Set(Object.keys(shape));
|
|
234
|
+
const requiredKeys = new Set([...allKeys].filter((key) => {
|
|
235
|
+
const v = def.shape[key]._zod;
|
|
236
|
+
if (this.io === "input") return v.optin === void 0;
|
|
237
|
+
else return v.optout === void 0;
|
|
238
|
+
}));
|
|
239
|
+
if (requiredKeys.size > 0) json.required = Array.from(requiredKeys);
|
|
240
|
+
if (def.catchall?._zod.def.type === "never") json.additionalProperties = false;
|
|
241
|
+
else if (!def.catchall) {
|
|
242
|
+
if (this.io === "output") json.additionalProperties = false;
|
|
243
|
+
} else if (def.catchall) json.additionalProperties = this.process(def.catchall, {
|
|
244
|
+
...params,
|
|
245
|
+
path: [...params.path, "additionalProperties"]
|
|
246
|
+
});
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
case "union": {
|
|
250
|
+
const json = _json;
|
|
251
|
+
json.anyOf = def.options.map((x, i) => this.process(x, {
|
|
252
|
+
...params,
|
|
253
|
+
path: [
|
|
254
|
+
...params.path,
|
|
255
|
+
"anyOf",
|
|
256
|
+
i
|
|
257
|
+
]
|
|
258
|
+
}));
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
case "intersection": {
|
|
262
|
+
const json = _json;
|
|
263
|
+
const a = this.process(def.left, {
|
|
264
|
+
...params,
|
|
265
|
+
path: [
|
|
266
|
+
...params.path,
|
|
267
|
+
"allOf",
|
|
268
|
+
0
|
|
269
|
+
]
|
|
270
|
+
});
|
|
271
|
+
const b = this.process(def.right, {
|
|
272
|
+
...params,
|
|
273
|
+
path: [
|
|
274
|
+
...params.path,
|
|
275
|
+
"allOf",
|
|
276
|
+
1
|
|
277
|
+
]
|
|
278
|
+
});
|
|
279
|
+
const isSimpleIntersection = (val) => "allOf" in val && Object.keys(val).length === 1;
|
|
280
|
+
json.allOf = [...isSimpleIntersection(a) ? a.allOf : [a], ...isSimpleIntersection(b) ? b.allOf : [b]];
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
case "tuple": {
|
|
284
|
+
const json = _json;
|
|
285
|
+
json.type = "array";
|
|
286
|
+
const prefixPath = this.target === "draft-2020-12" ? "prefixItems" : "items";
|
|
287
|
+
const restPath = this.target === "draft-2020-12" ? "items" : this.target === "openapi-3.0" ? "items" : "additionalItems";
|
|
288
|
+
const prefixItems = def.items.map((x, i) => this.process(x, {
|
|
289
|
+
...params,
|
|
290
|
+
path: [
|
|
291
|
+
...params.path,
|
|
292
|
+
prefixPath,
|
|
293
|
+
i
|
|
294
|
+
]
|
|
295
|
+
}));
|
|
296
|
+
const rest = def.rest ? this.process(def.rest, {
|
|
297
|
+
...params,
|
|
298
|
+
path: [
|
|
299
|
+
...params.path,
|
|
300
|
+
restPath,
|
|
301
|
+
...this.target === "openapi-3.0" ? [def.items.length] : []
|
|
302
|
+
]
|
|
303
|
+
}) : null;
|
|
304
|
+
if (this.target === "draft-2020-12") {
|
|
305
|
+
json.prefixItems = prefixItems;
|
|
306
|
+
if (rest) json.items = rest;
|
|
307
|
+
} else if (this.target === "openapi-3.0") {
|
|
308
|
+
json.items = { anyOf: prefixItems };
|
|
309
|
+
if (rest) json.items.anyOf.push(rest);
|
|
310
|
+
json.minItems = prefixItems.length;
|
|
311
|
+
if (!rest) json.maxItems = prefixItems.length;
|
|
312
|
+
} else {
|
|
313
|
+
json.items = prefixItems;
|
|
314
|
+
if (rest) json.additionalItems = rest;
|
|
315
|
+
}
|
|
316
|
+
const { minimum, maximum } = schema._zod.bag;
|
|
317
|
+
if (typeof minimum === "number") json.minItems = minimum;
|
|
318
|
+
if (typeof maximum === "number") json.maxItems = maximum;
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
case "record": {
|
|
322
|
+
const json = _json;
|
|
323
|
+
json.type = "object";
|
|
324
|
+
if (this.target === "draft-7" || this.target === "draft-2020-12") json.propertyNames = this.process(def.keyType, {
|
|
325
|
+
...params,
|
|
326
|
+
path: [...params.path, "propertyNames"]
|
|
327
|
+
});
|
|
328
|
+
json.additionalProperties = this.process(def.valueType, {
|
|
329
|
+
...params,
|
|
330
|
+
path: [...params.path, "additionalProperties"]
|
|
331
|
+
});
|
|
332
|
+
break;
|
|
333
|
+
}
|
|
334
|
+
case "map":
|
|
335
|
+
if (this.unrepresentable === "throw") throw new Error("Map cannot be represented in JSON Schema");
|
|
336
|
+
break;
|
|
337
|
+
case "set":
|
|
338
|
+
if (this.unrepresentable === "throw") throw new Error("Set cannot be represented in JSON Schema");
|
|
339
|
+
break;
|
|
340
|
+
case "enum": {
|
|
341
|
+
const json = _json;
|
|
342
|
+
const values = getEnumValues(def.entries);
|
|
343
|
+
if (values.every((v) => typeof v === "number")) json.type = "number";
|
|
344
|
+
if (values.every((v) => typeof v === "string")) json.type = "string";
|
|
345
|
+
json.enum = values;
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
case "literal": {
|
|
349
|
+
const json = _json;
|
|
350
|
+
const vals = [];
|
|
351
|
+
for (const val of def.values) if (val === void 0) {
|
|
352
|
+
if (this.unrepresentable === "throw") throw new Error("Literal `undefined` cannot be represented in JSON Schema");
|
|
353
|
+
} else if (typeof val === "bigint") if (this.unrepresentable === "throw") throw new Error("BigInt literals cannot be represented in JSON Schema");
|
|
354
|
+
else vals.push(Number(val));
|
|
355
|
+
else vals.push(val);
|
|
356
|
+
if (vals.length === 0) {} else if (vals.length === 1) {
|
|
357
|
+
const val = vals[0];
|
|
358
|
+
json.type = val === null ? "null" : typeof val;
|
|
359
|
+
if (this.target === "draft-4" || this.target === "openapi-3.0") json.enum = [val];
|
|
360
|
+
else json.const = val;
|
|
361
|
+
} else {
|
|
362
|
+
if (vals.every((v) => typeof v === "number")) json.type = "number";
|
|
363
|
+
if (vals.every((v) => typeof v === "string")) json.type = "string";
|
|
364
|
+
if (vals.every((v) => typeof v === "boolean")) json.type = "string";
|
|
365
|
+
if (vals.every((v) => v === null)) json.type = "null";
|
|
366
|
+
json.enum = vals;
|
|
367
|
+
}
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
case "file": {
|
|
371
|
+
const json = _json;
|
|
372
|
+
const file = {
|
|
373
|
+
type: "string",
|
|
374
|
+
format: "binary",
|
|
375
|
+
contentEncoding: "binary"
|
|
376
|
+
};
|
|
377
|
+
const { minimum, maximum, mime } = schema._zod.bag;
|
|
378
|
+
if (minimum !== void 0) file.minLength = minimum;
|
|
379
|
+
if (maximum !== void 0) file.maxLength = maximum;
|
|
380
|
+
if (mime) if (mime.length === 1) {
|
|
381
|
+
file.contentMediaType = mime[0];
|
|
382
|
+
Object.assign(json, file);
|
|
383
|
+
} else json.anyOf = mime.map((m) => {
|
|
384
|
+
return {
|
|
385
|
+
...file,
|
|
386
|
+
contentMediaType: m
|
|
387
|
+
};
|
|
388
|
+
});
|
|
389
|
+
else Object.assign(json, file);
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
case "transform":
|
|
393
|
+
if (this.unrepresentable === "throw") throw new Error("Transforms cannot be represented in JSON Schema");
|
|
394
|
+
break;
|
|
395
|
+
case "nullable": {
|
|
396
|
+
const inner = this.process(def.innerType, params);
|
|
397
|
+
if (this.target === "openapi-3.0") {
|
|
398
|
+
result.ref = def.innerType;
|
|
399
|
+
_json.nullable = true;
|
|
400
|
+
} else _json.anyOf = [inner, { type: "null" }];
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
case "nonoptional":
|
|
404
|
+
this.process(def.innerType, params);
|
|
405
|
+
result.ref = def.innerType;
|
|
406
|
+
break;
|
|
407
|
+
case "success": {
|
|
408
|
+
const json = _json;
|
|
409
|
+
json.type = "boolean";
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
case "default":
|
|
413
|
+
this.process(def.innerType, params);
|
|
414
|
+
result.ref = def.innerType;
|
|
415
|
+
_json.default = JSON.parse(JSON.stringify(def.defaultValue));
|
|
416
|
+
break;
|
|
417
|
+
case "prefault":
|
|
418
|
+
this.process(def.innerType, params);
|
|
419
|
+
result.ref = def.innerType;
|
|
420
|
+
if (this.io === "input") _json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
|
|
421
|
+
break;
|
|
422
|
+
case "catch": {
|
|
423
|
+
this.process(def.innerType, params);
|
|
424
|
+
result.ref = def.innerType;
|
|
425
|
+
let catchValue;
|
|
426
|
+
try {
|
|
427
|
+
catchValue = def.catchValue(void 0);
|
|
428
|
+
} catch {
|
|
429
|
+
throw new Error("Dynamic catch values are not supported in JSON Schema");
|
|
430
|
+
}
|
|
431
|
+
_json.default = catchValue;
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
case "nan":
|
|
435
|
+
if (this.unrepresentable === "throw") throw new Error("NaN cannot be represented in JSON Schema");
|
|
436
|
+
break;
|
|
437
|
+
case "template_literal": {
|
|
438
|
+
const json = _json;
|
|
439
|
+
const pattern = schema._zod.pattern;
|
|
440
|
+
if (!pattern) throw new Error("Pattern not found in template literal");
|
|
441
|
+
json.type = "string";
|
|
442
|
+
json.pattern = pattern.source;
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
case "pipe": {
|
|
446
|
+
const innerType = this.io === "input" ? def.in._zod.def.type === "transform" ? def.out : def.in : def.out;
|
|
447
|
+
this.process(innerType, params);
|
|
448
|
+
result.ref = innerType;
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
case "readonly":
|
|
452
|
+
this.process(def.innerType, params);
|
|
453
|
+
result.ref = def.innerType;
|
|
454
|
+
_json.readOnly = true;
|
|
455
|
+
break;
|
|
456
|
+
case "promise":
|
|
457
|
+
this.process(def.innerType, params);
|
|
458
|
+
result.ref = def.innerType;
|
|
459
|
+
break;
|
|
460
|
+
case "optional":
|
|
461
|
+
this.process(def.innerType, params);
|
|
462
|
+
result.ref = def.innerType;
|
|
463
|
+
break;
|
|
464
|
+
case "lazy": {
|
|
465
|
+
const innerType = schema._zod.innerType;
|
|
466
|
+
this.process(innerType, params);
|
|
467
|
+
result.ref = innerType;
|
|
468
|
+
break;
|
|
469
|
+
}
|
|
470
|
+
case "custom":
|
|
471
|
+
if (this.unrepresentable === "throw") throw new Error("Custom types cannot be represented in JSON Schema");
|
|
472
|
+
break;
|
|
473
|
+
case "function":
|
|
474
|
+
if (this.unrepresentable === "throw") throw new Error("Function types cannot be represented in JSON Schema");
|
|
475
|
+
break;
|
|
476
|
+
default:
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
const meta = this.metadataRegistry.get(schema);
|
|
481
|
+
if (meta) Object.assign(result.schema, meta);
|
|
482
|
+
if (this.io === "input" && isTransforming(schema)) {
|
|
483
|
+
delete result.schema.examples;
|
|
484
|
+
delete result.schema.default;
|
|
485
|
+
}
|
|
486
|
+
if (this.io === "input" && result.schema._prefault) (_a = result.schema).default ?? (_a.default = result.schema._prefault);
|
|
487
|
+
delete result.schema._prefault;
|
|
488
|
+
return this.seen.get(schema).schema;
|
|
489
|
+
}
|
|
490
|
+
emit(schema, _params) {
|
|
491
|
+
const params = {
|
|
492
|
+
cycles: _params?.cycles ?? "ref",
|
|
493
|
+
reused: _params?.reused ?? "inline",
|
|
494
|
+
external: _params?.external ?? void 0
|
|
495
|
+
};
|
|
496
|
+
const root = this.seen.get(schema);
|
|
497
|
+
if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
|
|
498
|
+
const makeURI = (entry) => {
|
|
499
|
+
const defsSegment = this.target === "draft-2020-12" ? "$defs" : "definitions";
|
|
500
|
+
if (params.external) {
|
|
501
|
+
const externalId = params.external.registry.get(entry[0])?.id;
|
|
502
|
+
const uriGenerator = params.external.uri ?? ((id) => id);
|
|
503
|
+
if (externalId) return { ref: uriGenerator(externalId) };
|
|
504
|
+
const id = entry[1].defId ?? entry[1].schema.id ?? `schema${this.counter++}`;
|
|
505
|
+
entry[1].defId = id;
|
|
506
|
+
return {
|
|
507
|
+
defId: id,
|
|
508
|
+
ref: `${uriGenerator("__shared")}#/${defsSegment}/${id}`
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
if (entry[1] === root) return { ref: "#" };
|
|
512
|
+
const defUriPrefix = `#/${defsSegment}/`;
|
|
513
|
+
const defId = entry[1].schema.id ?? `__schema${this.counter++}`;
|
|
514
|
+
return {
|
|
515
|
+
defId,
|
|
516
|
+
ref: defUriPrefix + defId
|
|
517
|
+
};
|
|
518
|
+
};
|
|
519
|
+
const extractToDef = (entry) => {
|
|
520
|
+
if (entry[1].schema.$ref) return;
|
|
521
|
+
const seen = entry[1];
|
|
522
|
+
const { ref, defId } = makeURI(entry);
|
|
523
|
+
seen.def = { ...seen.schema };
|
|
524
|
+
if (defId) seen.defId = defId;
|
|
525
|
+
const schema = seen.schema;
|
|
526
|
+
for (const key in schema) delete schema[key];
|
|
527
|
+
schema.$ref = ref;
|
|
528
|
+
};
|
|
529
|
+
if (params.cycles === "throw") for (const entry of this.seen.entries()) {
|
|
530
|
+
const seen = entry[1];
|
|
531
|
+
if (seen.cycle) throw new Error(`Cycle detected: #/${seen.cycle?.join("/")}/<root>
|
|
532
|
+
|
|
533
|
+
Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.`);
|
|
534
|
+
}
|
|
535
|
+
for (const entry of this.seen.entries()) {
|
|
536
|
+
const seen = entry[1];
|
|
537
|
+
if (schema === entry[0]) {
|
|
538
|
+
extractToDef(entry);
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
if (params.external) {
|
|
542
|
+
const ext = params.external.registry.get(entry[0])?.id;
|
|
543
|
+
if (schema !== entry[0] && ext) {
|
|
544
|
+
extractToDef(entry);
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
if (this.metadataRegistry.get(entry[0])?.id) {
|
|
549
|
+
extractToDef(entry);
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
if (seen.cycle) {
|
|
553
|
+
extractToDef(entry);
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
if (seen.count > 1) {
|
|
557
|
+
if (params.reused === "ref") {
|
|
558
|
+
extractToDef(entry);
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
const flattenRef = (zodSchema, params) => {
|
|
564
|
+
const seen = this.seen.get(zodSchema);
|
|
565
|
+
const schema = seen.def ?? seen.schema;
|
|
566
|
+
const _cached = { ...schema };
|
|
567
|
+
if (seen.ref === null) return;
|
|
568
|
+
const ref = seen.ref;
|
|
569
|
+
seen.ref = null;
|
|
570
|
+
if (ref) {
|
|
571
|
+
flattenRef(ref, params);
|
|
572
|
+
const refSchema = this.seen.get(ref).schema;
|
|
573
|
+
if (refSchema.$ref && (params.target === "draft-7" || params.target === "draft-4" || params.target === "openapi-3.0")) {
|
|
574
|
+
schema.allOf = schema.allOf ?? [];
|
|
575
|
+
schema.allOf.push(refSchema);
|
|
576
|
+
} else {
|
|
577
|
+
Object.assign(schema, refSchema);
|
|
578
|
+
Object.assign(schema, _cached);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (!seen.isParent) this.override({
|
|
582
|
+
zodSchema,
|
|
583
|
+
jsonSchema: schema,
|
|
584
|
+
path: seen.path ?? []
|
|
585
|
+
});
|
|
586
|
+
};
|
|
587
|
+
for (const entry of [...this.seen.entries()].reverse()) flattenRef(entry[0], { target: this.target });
|
|
588
|
+
const result = {};
|
|
589
|
+
if (this.target === "draft-2020-12") result.$schema = "https://json-schema.org/draft/2020-12/schema";
|
|
590
|
+
else if (this.target === "draft-7") result.$schema = "http://json-schema.org/draft-07/schema#";
|
|
591
|
+
else if (this.target === "draft-4") result.$schema = "http://json-schema.org/draft-04/schema#";
|
|
592
|
+
else if (this.target === "openapi-3.0") {} else console.warn(`Invalid target: ${this.target}`);
|
|
593
|
+
if (params.external?.uri) {
|
|
594
|
+
const id = params.external.registry.get(schema)?.id;
|
|
595
|
+
if (!id) throw new Error("Schema is missing an `id` property");
|
|
596
|
+
result.$id = params.external.uri(id);
|
|
597
|
+
}
|
|
598
|
+
Object.assign(result, root.def);
|
|
599
|
+
const defs = params.external?.defs ?? {};
|
|
600
|
+
for (const entry of this.seen.entries()) {
|
|
601
|
+
const seen = entry[1];
|
|
602
|
+
if (seen.def && seen.defId) defs[seen.defId] = seen.def;
|
|
603
|
+
}
|
|
604
|
+
if (params.external) {} else if (Object.keys(defs).length > 0) if (this.target === "draft-2020-12") result.$defs = defs;
|
|
605
|
+
else result.definitions = defs;
|
|
606
|
+
try {
|
|
607
|
+
return JSON.parse(JSON.stringify(result));
|
|
608
|
+
} catch (_err) {
|
|
609
|
+
throw new Error("Error converting schema to JSON.");
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
function toJSONSchema(input, _params) {
|
|
614
|
+
if (input instanceof $ZodRegistry) {
|
|
615
|
+
const gen = new JSONSchemaGenerator(_params);
|
|
616
|
+
const defs = {};
|
|
617
|
+
for (const entry of input._idmap.entries()) {
|
|
618
|
+
const [_, schema] = entry;
|
|
619
|
+
gen.process(schema);
|
|
620
|
+
}
|
|
621
|
+
const schemas = {};
|
|
622
|
+
const external = {
|
|
623
|
+
registry: input,
|
|
624
|
+
uri: _params?.uri,
|
|
625
|
+
defs
|
|
626
|
+
};
|
|
627
|
+
for (const entry of input._idmap.entries()) {
|
|
628
|
+
const [key, schema] = entry;
|
|
629
|
+
schemas[key] = gen.emit(schema, {
|
|
630
|
+
..._params,
|
|
631
|
+
external
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
if (Object.keys(defs).length > 0) schemas.__shared = { [gen.target === "draft-2020-12" ? "$defs" : "definitions"]: defs };
|
|
635
|
+
return { schemas };
|
|
636
|
+
}
|
|
637
|
+
const gen = new JSONSchemaGenerator(_params);
|
|
638
|
+
gen.process(input);
|
|
639
|
+
return gen.emit(input, _params);
|
|
640
|
+
}
|
|
641
|
+
function isTransforming(_schema, _ctx) {
|
|
642
|
+
const ctx = _ctx ?? { seen: /* @__PURE__ */ new Set() };
|
|
643
|
+
if (ctx.seen.has(_schema)) return false;
|
|
644
|
+
ctx.seen.add(_schema);
|
|
645
|
+
const def = _schema._zod.def;
|
|
646
|
+
switch (def.type) {
|
|
647
|
+
case "string":
|
|
648
|
+
case "number":
|
|
649
|
+
case "bigint":
|
|
650
|
+
case "boolean":
|
|
651
|
+
case "date":
|
|
652
|
+
case "symbol":
|
|
653
|
+
case "undefined":
|
|
654
|
+
case "null":
|
|
655
|
+
case "any":
|
|
656
|
+
case "unknown":
|
|
657
|
+
case "never":
|
|
658
|
+
case "void":
|
|
659
|
+
case "literal":
|
|
660
|
+
case "enum":
|
|
661
|
+
case "nan":
|
|
662
|
+
case "file":
|
|
663
|
+
case "template_literal": return false;
|
|
664
|
+
case "array": return isTransforming(def.element, ctx);
|
|
665
|
+
case "object":
|
|
666
|
+
for (const key in def.shape) if (isTransforming(def.shape[key], ctx)) return true;
|
|
667
|
+
return false;
|
|
668
|
+
case "union":
|
|
669
|
+
for (const option of def.options) if (isTransforming(option, ctx)) return true;
|
|
670
|
+
return false;
|
|
671
|
+
case "intersection": return isTransforming(def.left, ctx) || isTransforming(def.right, ctx);
|
|
672
|
+
case "tuple":
|
|
673
|
+
for (const item of def.items) if (isTransforming(item, ctx)) return true;
|
|
674
|
+
if (def.rest && isTransforming(def.rest, ctx)) return true;
|
|
675
|
+
return false;
|
|
676
|
+
case "record": return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
|
|
677
|
+
case "map": return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
|
|
678
|
+
case "set": return isTransforming(def.valueType, ctx);
|
|
679
|
+
case "promise":
|
|
680
|
+
case "optional":
|
|
681
|
+
case "nonoptional":
|
|
682
|
+
case "nullable":
|
|
683
|
+
case "readonly": return isTransforming(def.innerType, ctx);
|
|
684
|
+
case "lazy": return isTransforming(def.getter(), ctx);
|
|
685
|
+
case "default": return isTransforming(def.innerType, ctx);
|
|
686
|
+
case "prefault": return isTransforming(def.innerType, ctx);
|
|
687
|
+
case "custom": return false;
|
|
688
|
+
case "transform": return true;
|
|
689
|
+
case "pipe": return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
|
|
690
|
+
case "success": return false;
|
|
691
|
+
case "catch": return false;
|
|
692
|
+
case "function": return false;
|
|
693
|
+
default:
|
|
694
|
+
}
|
|
695
|
+
throw new Error(`Unknown schema type: ${def.type}`);
|
|
696
|
+
}
|
|
697
|
+
//#endregion
|
|
698
|
+
//#region src/adapters/zod-schema-adapter.ts
|
|
699
|
+
/**
|
|
700
|
+
* Contains a schema adapter for app's using [`zod`](https://npmjs.com/package/zod).
|
|
701
|
+
* @module
|
|
702
|
+
*/
|
|
703
|
+
/**
|
|
704
|
+
* Usage:
|
|
705
|
+
*
|
|
706
|
+
* ```ts
|
|
707
|
+
* import { zodSchemaAdapter } from "@aklinker1/zeta/adapters/zod-schema-adapter";
|
|
708
|
+
*
|
|
709
|
+
* const app = createApp({
|
|
710
|
+
* schemaAdapter: zodSchemaAdapter,
|
|
711
|
+
* });
|
|
712
|
+
* ```
|
|
713
|
+
*/
|
|
714
|
+
const zodSchemaAdapter = {
|
|
715
|
+
toJsonSchema: (schema) => {
|
|
716
|
+
if (!("_zod" in schema)) throw Error("input schema is not a Zod schema");
|
|
717
|
+
const res = toJSONSchema(schema, { target: "openapi-3.0" });
|
|
718
|
+
delete res.$schema;
|
|
719
|
+
return res;
|
|
720
|
+
},
|
|
721
|
+
getMeta: (schema) => {
|
|
722
|
+
return globalRegistry.get(schema);
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
//#endregion
|
|
726
|
+
export { zodSchemaAdapter };
|