@orpc/zod 2.0.0-beta.22 → 2.0.0-beta.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,6 +44,7 @@ You can read the documentation [here](https://orpc.dev).
44
44
 
45
45
  - [@orpc/publisher](https://www.npmjs.com/package/@orpc/publisher): Pub/Sub with memory, Redis, and Upstash adapters.
46
46
  - [@orpc/ratelimit](https://www.npmjs.com/package/@orpc/ratelimit): Rate limiting with memory, Redis, and Upstash adapters.
47
+ - [@orpc/hibernation](https://www.npmjs.com/package/@orpc/hibernation): Leverage Hibernation APIs like [Cloudflare's Hibernation WebSocket](https://developers.cloudflare.com/durable-objects/best-practices/websockets/#durable-objects-hibernation-websocket-api).
47
48
  - [@orpc/json-schema](https://www.npmjs.com/package/@orpc/json-schema): Smart coercion for OpenAPI requests.
48
49
 
49
50
  **Framework & ecosystem integrations**
@@ -114,6 +115,7 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
114
115
  </tr>
115
116
  <tr>
116
117
  <td align="center"><a href="https://github.com/itigoore01?ref=orpc" target="_blank" rel="noopener" title="shota"><img src="https://avatars.githubusercontent.com/u/11831107?u=c976a6dc7e055eb026304c46c99100ed22b0c8e0&amp;v=4" width="139" alt="shota"/><br />shota</a></td>
118
+ <td align="center"><a href="https://github.com/ellis-driscoll?ref=orpc" target="_blank" rel="noopener" title="Ellis Driscoll"><img src="https://avatars.githubusercontent.com/u/70685966?u=c5f95bc33b5991d9744abe00052542e4a2ed3cb9&amp;v=4" width="139" alt="Ellis Driscoll"/><br />Ellis Driscoll</a></td>
117
119
  </tr>
118
120
  </table>
119
121
 
@@ -139,8 +141,9 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
139
141
  <td align="center"><a href="https://github.com/mr-kelly?ref=orpc" target="_blank" rel="noopener" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&amp;v=4" width="119" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
140
142
  </tr>
141
143
  <tr>
144
+ <td align="center"><a href="https://github.com/guyariely?ref=orpc" target="_blank" rel="noopener" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&amp;v=4" width="119" alt="Guy Ariely"/><br />Guy Ariely</a></td>
142
145
  <td align="center"><a href="https://github.com/piscis?ref=orpc" target="_blank" rel="noopener" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&amp;v=4" width="119" alt="Alex"/><br />Alex</a></td>
143
- <td align="center"><a href="https://github.com/finom?ref=orpc" target="_blank" rel="noopener" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=29e91400dbd4a9c217048a8f59562c4f740498e6&amp;v=4" width="119" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
146
+ <td align="center"><a href="https://github.com/finom?ref=orpc" target="_blank" rel="noopener" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&amp;v=4" width="119" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
144
147
  </tr>
145
148
  </table>
146
149
 
package/dist/index.d.mts CHANGED
@@ -4,12 +4,30 @@ import { ToJSONSchemaParams, $input, $output } from 'zod/v4/core';
4
4
  import * as json_schema_typed from 'json-schema-typed';
5
5
 
6
6
  interface ZodToJsonSchemaConverterOptions extends Omit<ToJSONSchemaParams, 'target' | 'io'> {
7
+ /**
8
+ * Caches conversion results in a WeakMap keyed by the Zod schema instance,
9
+ * so converting the same schema again is free.
10
+ *
11
+ * When enabled, repeated conversions return the same JSON schema object,
12
+ * so treat returned schemas as immutable.
13
+ *
14
+ * @default false
15
+ */
16
+ cache?: boolean;
7
17
  }
18
+ /**
19
+ * Converts Zod schemas into JSON Schema using Zod's built-in `toJSONSchema`,
20
+ * with additional support for types such as `z.bigint()`, `z.date()`, `z.set()`, and `z.map()`.
21
+ *
22
+ * @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
23
+ */
8
24
  declare class ZodToJsonSchemaConverter implements JsonSchemaConverter {
9
- private readonly options;
10
- constructor(options?: ZodToJsonSchemaConverterOptions);
25
+ private readonly toJSONSchemaParams;
26
+ private readonly cache;
27
+ constructor({ cache, ...toJSONSchemaParams }?: ZodToJsonSchemaConverterOptions);
11
28
  condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
12
29
  convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
30
+ private convertUncached;
13
31
  private convertZod;
14
32
  private getCustomJsonSchema;
15
33
  }
@@ -30,6 +48,8 @@ declare class ZodToJsonSchemaConverter implements JsonSchemaConverter {
30
48
  * examples: [{ name: 'John', age: 20 }],
31
49
  * })
32
50
  * ```
51
+ *
52
+ * @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
33
53
  */
34
54
  declare const JSON_SCHEMA_REGISTRY: zod_v4_core.$ZodRegistry<{
35
55
  $anchor?: string;
@@ -109,6 +129,8 @@ declare const JSON_SCHEMA_REGISTRY: zod_v4_core.$ZodRegistry<{
109
129
  * examples: [{ name: 'John', age: "20" }],
110
130
  * })
111
131
  * ```
132
+ *
133
+ * @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
112
134
  */
113
135
  declare const JSON_SCHEMA_INPUT_REGISTRY: zod_v4_core.$ZodRegistry<{
114
136
  $anchor?: string;
@@ -188,6 +210,8 @@ declare const JSON_SCHEMA_INPUT_REGISTRY: zod_v4_core.$ZodRegistry<{
188
210
  * examples: [{ name: 'John', age: 20 }],
189
211
  * })
190
212
  * ```
213
+ *
214
+ * @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
191
215
  */
192
216
  declare const JSON_SCHEMA_OUTPUT_REGISTRY: zod_v4_core.$ZodRegistry<{
193
217
  $anchor?: string;
package/dist/index.d.ts CHANGED
@@ -4,12 +4,30 @@ import { ToJSONSchemaParams, $input, $output } from 'zod/v4/core';
4
4
  import * as json_schema_typed from 'json-schema-typed';
5
5
 
6
6
  interface ZodToJsonSchemaConverterOptions extends Omit<ToJSONSchemaParams, 'target' | 'io'> {
7
+ /**
8
+ * Caches conversion results in a WeakMap keyed by the Zod schema instance,
9
+ * so converting the same schema again is free.
10
+ *
11
+ * When enabled, repeated conversions return the same JSON schema object,
12
+ * so treat returned schemas as immutable.
13
+ *
14
+ * @default false
15
+ */
16
+ cache?: boolean;
7
17
  }
18
+ /**
19
+ * Converts Zod schemas into JSON Schema using Zod's built-in `toJSONSchema`,
20
+ * with additional support for types such as `z.bigint()`, `z.date()`, `z.set()`, and `z.map()`.
21
+ *
22
+ * @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
23
+ */
8
24
  declare class ZodToJsonSchemaConverter implements JsonSchemaConverter {
9
- private readonly options;
10
- constructor(options?: ZodToJsonSchemaConverterOptions);
25
+ private readonly toJSONSchemaParams;
26
+ private readonly cache;
27
+ constructor({ cache, ...toJSONSchemaParams }?: ZodToJsonSchemaConverterOptions);
11
28
  condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
12
29
  convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
30
+ private convertUncached;
13
31
  private convertZod;
14
32
  private getCustomJsonSchema;
15
33
  }
@@ -30,6 +48,8 @@ declare class ZodToJsonSchemaConverter implements JsonSchemaConverter {
30
48
  * examples: [{ name: 'John', age: 20 }],
31
49
  * })
32
50
  * ```
51
+ *
52
+ * @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
33
53
  */
34
54
  declare const JSON_SCHEMA_REGISTRY: zod_v4_core.$ZodRegistry<{
35
55
  $anchor?: string;
@@ -109,6 +129,8 @@ declare const JSON_SCHEMA_REGISTRY: zod_v4_core.$ZodRegistry<{
109
129
  * examples: [{ name: 'John', age: "20" }],
110
130
  * })
111
131
  * ```
132
+ *
133
+ * @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
112
134
  */
113
135
  declare const JSON_SCHEMA_INPUT_REGISTRY: zod_v4_core.$ZodRegistry<{
114
136
  $anchor?: string;
@@ -188,6 +210,8 @@ declare const JSON_SCHEMA_INPUT_REGISTRY: zod_v4_core.$ZodRegistry<{
188
210
  * examples: [{ name: 'John', age: 20 }],
189
211
  * })
190
212
  * ```
213
+ *
214
+ * @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
191
215
  */
192
216
  declare const JSON_SCHEMA_OUTPUT_REGISTRY: zod_v4_core.$ZodRegistry<{
193
217
  $anchor?: string;
package/dist/index.mjs CHANGED
@@ -6,14 +6,32 @@ const JSON_SCHEMA_INPUT_REGISTRY = registry();
6
6
  const JSON_SCHEMA_OUTPUT_REGISTRY = registry();
7
7
 
8
8
  class ZodToJsonSchemaConverter {
9
- constructor(options = {}) {
10
- this.options = options;
9
+ toJSONSchemaParams;
10
+ cache;
11
+ constructor({ cache, ...toJSONSchemaParams } = {}) {
12
+ this.toJSONSchemaParams = toJSONSchemaParams;
13
+ if (cache) {
14
+ this.cache = { input: /* @__PURE__ */ new WeakMap(), output: /* @__PURE__ */ new WeakMap() };
15
+ }
11
16
  }
12
17
  condition(schema, _direction) {
13
18
  return schema?.["~standard"].vendor === "zod";
14
19
  }
15
20
  convert(schema, direction) {
16
21
  const zodSchema = schema;
22
+ if (this.cache) {
23
+ const cached = this.cache[direction].get(zodSchema);
24
+ if (cached) {
25
+ return cached;
26
+ }
27
+ }
28
+ const result = this.convertUncached(zodSchema, direction);
29
+ if (this.cache) {
30
+ this.cache[direction].set(zodSchema, result);
31
+ }
32
+ return result;
33
+ }
34
+ convertUncached(zodSchema, direction) {
17
35
  const jsonSchema = this.convertZod(zodSchema, direction);
18
36
  let optional = false;
19
37
  try {
@@ -28,7 +46,7 @@ class ZodToJsonSchemaConverter {
28
46
  convertZod(schema, direction) {
29
47
  const jsonSchema = toJSONSchema(schema, {
30
48
  unrepresentable: "any",
31
- ...this.options,
49
+ ...this.toJSONSchemaParams,
32
50
  target: "draft-2020-12",
33
51
  io: direction,
34
52
  override: (ctx) => {
@@ -63,11 +81,11 @@ class ZodToJsonSchemaConverter {
63
81
  if (customJsonSchema) {
64
82
  Object.assign(ctx.jsonSchema, customJsonSchema);
65
83
  }
66
- this.options.override?.(ctx);
84
+ this.toJSONSchemaParams.override?.(ctx);
67
85
  }
68
86
  });
69
87
  const { $schema, ...rest } = jsonSchema;
70
- const registry = this.options.metadata ?? globalRegistry;
88
+ const registry = this.toJSONSchemaParams.metadata ?? globalRegistry;
71
89
  const { id } = registry.get(schema) || {};
72
90
  if (typeof id === "string" && rest.$ref === void 0) {
73
91
  const { $defs = {}, ...restWithoutDefs } = rest;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/zod",
3
3
  "type": "module",
4
- "version": "2.0.0-beta.22",
4
+ "version": "2.0.0-beta.24",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.dev",
7
7
  "repository": {
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "json-schema-typed": "^8.0.2",
33
- "@orpc/json-schema": "2.0.0-beta.22"
33
+ "@orpc/json-schema": "2.0.0-beta.24"
34
34
  },
35
35
  "devDependencies": {
36
36
  "zod": "^4.4.3"