@orpc/zod 0.0.0-next.b4e6d3a → 0.0.0-next.b825e0c

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,19 +1,44 @@
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
+
1
30
  // src/coercer.ts
2
- import { guard, isObject } from "@orpc/shared";
3
31
  import {
4
32
  ZodFirstPartyTypeKind
5
33
  } from "zod";
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
- });
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;
17
42
  }
18
43
  };
19
44
  function zodCoerceInternal(schema, value, options) {
@@ -100,7 +125,7 @@ function zodCoerceInternal(schema, value, options) {
100
125
  if (value === void 0) {
101
126
  return [];
102
127
  }
103
- if (isObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
128
+ if (isPlainObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
104
129
  const indexes = Object.keys(value).map((k) => Number(k)).sort((a, b) => a - b);
105
130
  const arr = Array.from({ length: (indexes.at(-1) ?? -1) + 1 });
106
131
  for (const i of indexes) {
@@ -111,7 +136,7 @@ function zodCoerceInternal(schema, value, options) {
111
136
  }
112
137
  } else if (typeName === ZodFirstPartyTypeKind.ZodObject) {
113
138
  const schema_ = schema;
114
- if (isObject(value)) {
139
+ if (isPlainObject(value)) {
115
140
  const newObj = {};
116
141
  const keys = /* @__PURE__ */ new Set([
117
142
  ...Object.keys(value),
@@ -149,7 +174,7 @@ function zodCoerceInternal(schema, value, options) {
149
174
  if (value === void 0) {
150
175
  return /* @__PURE__ */ new Set();
151
176
  }
152
- if (isObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
177
+ if (isPlainObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
153
178
  const indexes = Object.keys(value).map((k) => Number(k)).sort((a, b) => a - b);
154
179
  const arr = Array.from({ length: (indexes.at(-1) ?? -1) + 1 });
155
180
  for (const i of indexes) {
@@ -172,9 +197,9 @@ function zodCoerceInternal(schema, value, options) {
172
197
  if (value === void 0) {
173
198
  return /* @__PURE__ */ new Map();
174
199
  }
175
- if (isObject(value)) {
200
+ if (isPlainObject(value)) {
176
201
  const arr = Array.from({ length: Object.keys(value).length }).fill(void 0).map(
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
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
178
203
  );
179
204
  if (arr.every((v) => !!v)) {
180
205
  return new Map(
@@ -188,7 +213,7 @@ function zodCoerceInternal(schema, value, options) {
188
213
  }
189
214
  } else if (typeName === ZodFirstPartyTypeKind.ZodRecord) {
190
215
  const schema_ = schema;
191
- if (isObject(value)) {
216
+ if (isPlainObject(value)) {
192
217
  const newObj = {};
193
218
  for (const [k, v] of Object.entries(value)) {
194
219
  const key = zodCoerceInternal(schema_._def.keyType, k, options_);
@@ -932,7 +957,7 @@ export {
932
957
  NON_LOGIC_KEYWORDS,
933
958
  UNDEFINED_JSON_SCHEMA,
934
959
  UNSUPPORTED_JSON_SCHEMA,
935
- ZodAutoCoercePlugin,
960
+ ZodCoercer,
936
961
  ZodToJsonSchemaConverter,
937
962
  blob,
938
963
  file,
@@ -1,7 +1,6 @@
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;
1
+ import type { Schema } from '@orpc/contract';
2
+ import type { SchemaCoercer } from '@orpc/openapi/standard';
3
+ export declare class ZodCoercer implements SchemaCoercer {
4
+ coerce(schema: Schema, value: unknown): unknown;
6
5
  }
7
6
  //# sourceMappingURL=coercer.d.ts.map
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.b4e6d3a",
4
+ "version": "0.0.0-next.b825e0c",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -29,15 +29,12 @@
29
29
  "dist"
30
30
  ],
31
31
  "peerDependencies": {
32
- "@orpc/server": "0.0.0-next.b4e6d3a",
33
- "@orpc/openapi": "0.0.0-next.b4e6d3a"
32
+ "@orpc/openapi": "0.0.0-next.b825e0c"
34
33
  },
35
34
  "dependencies": {
36
- "@standard-schema/spec": "^1.0.0",
37
35
  "json-schema-typed": "^8.0.1",
38
36
  "wildcard-match": "^5.1.3",
39
- "zod": "^3.24.1",
40
- "@orpc/shared": "0.0.0-next.b4e6d3a"
37
+ "zod": "^3.24.1"
41
38
  },
42
39
  "scripts": {
43
40
  "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",