@orpc/zod 0.0.0-next.d7b5662 → 0.0.0-next.da8ae32

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/index.js CHANGED
@@ -1,44 +1,19 @@
1
- // ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/getType.js
2
- function getType(payload) {
3
- return Object.prototype.toString.call(payload).slice(8, -1);
4
- }
5
-
6
- // ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/isPlainObject.js
7
- function isPlainObject(payload) {
8
- if (getType(payload) !== "Object")
9
- return false;
10
- const prototype = Object.getPrototypeOf(payload);
11
- return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
12
- }
13
-
14
- // ../../node_modules/.pnpm/radash@12.1.0/node_modules/radash/dist/esm/async.mjs
15
- var guard = (func, shouldGuard) => {
16
- const _guard = (err) => {
17
- if (shouldGuard && !shouldGuard(err))
18
- throw err;
19
- return void 0;
20
- };
21
- const isPromise2 = (result) => result instanceof Promise;
22
- try {
23
- const result = func();
24
- return isPromise2(result) ? result.catch(_guard) : result;
25
- } catch (err) {
26
- return _guard(err);
27
- }
28
- };
29
-
30
1
  // src/coercer.ts
2
+ import { guard, isObject } from "@orpc/shared";
31
3
  import {
32
4
  ZodFirstPartyTypeKind
33
5
  } from "zod";
34
- var ZodCoercer = class {
35
- coerce(schema, value) {
36
- if (!schema || schema["~standard"].vendor !== "zod") {
37
- return value;
38
- }
39
- const zodSchema = schema;
40
- const coerced = zodCoerceInternal(zodSchema, value, { bracketNotation: true });
41
- return coerced;
6
+ var ZodAutoCoercePlugin = class {
7
+ beforeCreateProcedureClient(clientOptions) {
8
+ clientOptions.interceptors ??= [];
9
+ clientOptions.interceptors.unshift((options) => {
10
+ const inputSchema = options.procedure["~orpc"].inputSchema;
11
+ if (!inputSchema || inputSchema["~standard"].vendor !== "zod") {
12
+ return options.next();
13
+ }
14
+ const coercedInput = zodCoerceInternal(inputSchema, options.input, { bracketNotation: true });
15
+ return options.next({ ...options, input: coercedInput });
16
+ });
42
17
  }
43
18
  };
44
19
  function zodCoerceInternal(schema, value, options) {
@@ -125,7 +100,7 @@ function zodCoerceInternal(schema, value, options) {
125
100
  if (value === void 0) {
126
101
  return [];
127
102
  }
128
- if (isPlainObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
103
+ if (isObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
129
104
  const indexes = Object.keys(value).map((k) => Number(k)).sort((a, b) => a - b);
130
105
  const arr = Array.from({ length: (indexes.at(-1) ?? -1) + 1 });
131
106
  for (const i of indexes) {
@@ -136,7 +111,7 @@ function zodCoerceInternal(schema, value, options) {
136
111
  }
137
112
  } else if (typeName === ZodFirstPartyTypeKind.ZodObject) {
138
113
  const schema_ = schema;
139
- if (isPlainObject(value)) {
114
+ if (isObject(value)) {
140
115
  const newObj = {};
141
116
  const keys = /* @__PURE__ */ new Set([
142
117
  ...Object.keys(value),
@@ -174,7 +149,7 @@ function zodCoerceInternal(schema, value, options) {
174
149
  if (value === void 0) {
175
150
  return /* @__PURE__ */ new Set();
176
151
  }
177
- if (isPlainObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
152
+ if (isObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
178
153
  const indexes = Object.keys(value).map((k) => Number(k)).sort((a, b) => a - b);
179
154
  const arr = Array.from({ length: (indexes.at(-1) ?? -1) + 1 });
180
155
  for (const i of indexes) {
@@ -197,9 +172,9 @@ function zodCoerceInternal(schema, value, options) {
197
172
  if (value === void 0) {
198
173
  return /* @__PURE__ */ new Map();
199
174
  }
200
- if (isPlainObject(value)) {
175
+ if (isObject(value)) {
201
176
  const arr = Array.from({ length: Object.keys(value).length }).fill(void 0).map(
202
- (_, i) => isPlainObject(value[i]) && Object.keys(value[i]).length === 2 && "0" in value[i] && "1" in value[i] ? [value[i]["0"], value[i]["1"]] : void 0
177
+ (_, i) => isObject(value[i]) && Object.keys(value[i]).length === 2 && "0" in value[i] && "1" in value[i] ? [value[i]["0"], value[i]["1"]] : void 0
203
178
  );
204
179
  if (arr.every((v) => !!v)) {
205
180
  return new Map(
@@ -213,7 +188,7 @@ function zodCoerceInternal(schema, value, options) {
213
188
  }
214
189
  } else if (typeName === ZodFirstPartyTypeKind.ZodRecord) {
215
190
  const schema_ = schema;
216
- if (isPlainObject(value)) {
191
+ if (isObject(value)) {
217
192
  const newObj = {};
218
193
  for (const [k, v] of Object.entries(value)) {
219
194
  const key = zodCoerceInternal(schema_._def.keyType, k, options_);
@@ -528,6 +503,16 @@ function zodToJsonSchema(schema, options) {
528
503
  return {};
529
504
  }
530
505
  const schema__ = schema;
506
+ if (!options?.isHandledZodDescription && "description" in schema__._def) {
507
+ const json = zodToJsonSchema(schema__, {
508
+ ...options,
509
+ isHandledZodDescription: true
510
+ });
511
+ return {
512
+ description: schema__._def.description,
513
+ ...json
514
+ };
515
+ }
531
516
  if (!options?.isHandledCustomJSONSchema) {
532
517
  const customJSONSchema = getCustomJSONSchema(schema__._def, options);
533
518
  if (customJSONSchema) {
@@ -541,7 +526,7 @@ function zodToJsonSchema(schema, options) {
541
526
  };
542
527
  }
543
528
  }
544
- const childOptions = { ...options, isHandledCustomJSONSchema: false };
529
+ const childOptions = { ...options, isHandledCustomJSONSchema: false, isHandledZodDescription: false };
545
530
  const customType = getCustomZodType(schema__._def);
546
531
  switch (customType) {
547
532
  case "Blob": {
@@ -714,6 +699,7 @@ function zodToJsonSchema(schema, options) {
714
699
  const schema_ = schema__;
715
700
  const def = schema_._def;
716
701
  const json = { type: "array" };
702
+ json.items = zodToJsonSchema(def.type, childOptions);
717
703
  if (def.exactLength) {
718
704
  json.maxItems = def.exactLength.value;
719
705
  json.minItems = def.exactLength.value;
@@ -946,7 +932,7 @@ export {
946
932
  NON_LOGIC_KEYWORDS,
947
933
  UNDEFINED_JSON_SCHEMA,
948
934
  UNSUPPORTED_JSON_SCHEMA,
949
- ZodCoercer,
935
+ ZodAutoCoercePlugin,
950
936
  ZodToJsonSchemaConverter,
951
937
  blob,
952
938
  file,
@@ -1,6 +1,7 @@
1
- import type { Schema } from '@orpc/contract';
2
- import type { SchemaCoercer } from '@orpc/openapi/fetch';
3
- export declare class ZodCoercer implements SchemaCoercer {
4
- coerce(schema: Schema, value: unknown): unknown;
1
+ import type { Context } from '@orpc/server';
2
+ import type { Plugin } from '@orpc/server/plugins';
3
+ import type { WellCreateProcedureClientOptions } from '@orpc/server/standard';
4
+ export declare class ZodAutoCoercePlugin<TContext extends Context> implements Plugin<TContext> {
5
+ beforeCreateProcedureClient(clientOptions: WellCreateProcedureClientOptions<TContext>): void;
5
6
  }
6
7
  //# sourceMappingURL=coercer.d.ts.map
@@ -35,6 +35,12 @@ export interface ZodToJsonSchemaOptions {
35
35
  * @internal
36
36
  */
37
37
  isHandledCustomJSONSchema?: boolean;
38
+ /**
39
+ * Track if current level schema is handled zod description to prevent recursive
40
+ *
41
+ * @internal
42
+ */
43
+ isHandledZodDescription?: boolean;
38
44
  }
39
45
  export declare function zodToJsonSchema(schema: StandardSchemaV1, options?: ZodToJsonSchemaOptions): Exclude<JSONSchema.JSONSchema, boolean>;
40
46
  export declare class ZodToJsonSchemaConverter implements SchemaConverter {
@@ -10,7 +10,7 @@ export declare function getCustomJSONSchema(def: ZodTypeDef, options?: {
10
10
  mode?: 'input' | 'output';
11
11
  }): Exclude<JSONSchema, boolean> | undefined;
12
12
  export declare function file(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>> & {
13
- type: (mimeType: string, params?: string | CustomParams | ((input: unknown) => CustomParams)) => ZodEffects<ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>>, InstanceType<typeof File>, InstanceType<typeof File>>;
13
+ type(mimeType: string, params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodEffects<ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>>, InstanceType<typeof File>, InstanceType<typeof File>>;
14
14
  };
15
15
  export declare function blob(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<InstanceType<typeof Blob>, ZodTypeDef, InstanceType<typeof Blob>>;
16
16
  export declare function invalidDate(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<Date, ZodTypeDef, Date>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/zod",
3
3
  "type": "module",
4
- "version": "0.0.0-next.d7b5662",
4
+ "version": "0.0.0-next.da8ae32",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -29,12 +29,15 @@
29
29
  "dist"
30
30
  ],
31
31
  "peerDependencies": {
32
- "@orpc/openapi": "0.0.0-next.d7b5662"
32
+ "@orpc/openapi": "0.0.0-next.da8ae32",
33
+ "@orpc/server": "0.0.0-next.da8ae32"
33
34
  },
34
35
  "dependencies": {
36
+ "@standard-schema/spec": "^1.0.0",
35
37
  "json-schema-typed": "^8.0.1",
36
38
  "wildcard-match": "^5.1.3",
37
- "zod": "^3.24.1"
39
+ "zod": "^3.24.1",
40
+ "@orpc/shared": "0.0.0-next.da8ae32"
38
41
  },
39
42
  "scripts": {
40
43
  "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",