@orpc/client 0.0.0-next.b36125c → 0.0.0-next.b3afdeb

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.
Files changed (43) hide show
  1. package/README.md +14 -2
  2. package/dist/adapters/fetch/index.d.mts +30 -0
  3. package/dist/adapters/fetch/index.d.ts +30 -0
  4. package/dist/adapters/fetch/index.mjs +37 -0
  5. package/dist/adapters/standard/index.d.mts +105 -0
  6. package/dist/adapters/standard/index.d.ts +105 -0
  7. package/dist/adapters/standard/index.mjs +5 -0
  8. package/dist/index.d.mts +150 -0
  9. package/dist/index.d.ts +150 -0
  10. package/dist/{index.js → index.mjs} +7 -38
  11. package/dist/plugins/index.d.mts +62 -0
  12. package/dist/plugins/index.d.ts +62 -0
  13. package/dist/plugins/index.mjs +127 -0
  14. package/dist/shared/client.3Q53fveR.mjs +334 -0
  15. package/dist/{chunk-7F3XVLRJ.js → shared/client.BacCdg3F.mjs} +8 -117
  16. package/dist/shared/client.CupM8eRP.d.mts +30 -0
  17. package/dist/shared/client.CupM8eRP.d.ts +30 -0
  18. package/dist/shared/client.CvnV7_uV.mjs +12 -0
  19. package/dist/shared/client.DrOAzyMB.d.mts +45 -0
  20. package/dist/shared/client.aGal-uGY.d.ts +45 -0
  21. package/package.json +20 -25
  22. package/dist/chunk-I4MCMTJ2.js +0 -207
  23. package/dist/fetch.js +0 -127
  24. package/dist/openapi.js +0 -226
  25. package/dist/rpc.js +0 -10
  26. package/dist/src/adapters/fetch/index.d.ts +0 -3
  27. package/dist/src/adapters/fetch/rpc-link.d.ts +0 -98
  28. package/dist/src/adapters/fetch/types.d.ts +0 -5
  29. package/dist/src/client.d.ts +0 -9
  30. package/dist/src/dynamic-link.d.ts +0 -12
  31. package/dist/src/error.d.ts +0 -106
  32. package/dist/src/event-iterator-state.d.ts +0 -9
  33. package/dist/src/event-iterator.d.ts +0 -12
  34. package/dist/src/index.d.ts +0 -10
  35. package/dist/src/openapi/bracket-notation.d.ts +0 -9
  36. package/dist/src/openapi/index.d.ts +0 -4
  37. package/dist/src/openapi/json-serializer.d.ts +0 -7
  38. package/dist/src/openapi/serializer.d.ts +0 -11
  39. package/dist/src/rpc/index.d.ts +0 -3
  40. package/dist/src/rpc/json-serializer.d.ts +0 -12
  41. package/dist/src/rpc/serializer.d.ts +0 -9
  42. package/dist/src/types.d.ts +0 -29
  43. package/dist/src/utils.d.ts +0 -17
@@ -1,6 +1,7 @@
1
- // src/error.ts
2
- import { isObject } from "@orpc/shared";
3
- var COMMON_ORPC_ERROR_DEFS = {
1
+ import { isObject, isTypescriptObject } from '@orpc/shared';
2
+ import { getEventMeta, withEventMeta } from '@orpc/standard-server';
3
+
4
+ const COMMON_ORPC_ERROR_DEFS = {
4
5
  BAD_REQUEST: {
5
6
  status: 400,
6
7
  message: "Bad Request"
@@ -84,7 +85,7 @@ function fallbackORPCErrorStatus(code, status) {
84
85
  function fallbackORPCErrorMessage(code, message) {
85
86
  return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
86
87
  }
87
- var ORPCError = class _ORPCError extends Error {
88
+ class ORPCError extends Error {
88
89
  defined;
89
90
  code;
90
91
  status;
@@ -110,7 +111,7 @@ var ORPCError = class _ORPCError extends Error {
110
111
  };
111
112
  }
112
113
  static fromJSON(json, options) {
113
- return new _ORPCError(json.code, {
114
+ return new ORPCError(json.code, {
114
115
  ...options,
115
116
  ...json
116
117
  });
@@ -125,7 +126,7 @@ var ORPCError = class _ORPCError extends Error {
125
126
  }
126
127
  return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string";
127
128
  }
128
- };
129
+ }
129
130
  function isDefinedError(error) {
130
131
  return error instanceof ORPCError && error.defined;
131
132
  }
@@ -136,37 +137,6 @@ function toORPCError(error) {
136
137
  });
137
138
  }
138
139
 
139
- // src/event-iterator-state.ts
140
- var iteratorStates = /* @__PURE__ */ new WeakMap();
141
- function registerEventIteratorState(iterator, state) {
142
- iteratorStates.set(iterator, state);
143
- }
144
- function updateEventIteratorStatus(state, status) {
145
- if (state.status !== status) {
146
- state.status = status;
147
- state.listeners.forEach((cb) => cb(status));
148
- }
149
- }
150
- function onEventIteratorStatusChange(iterator, callback, notifyImmediately = true) {
151
- const state = iteratorStates.get(iterator);
152
- if (!state) {
153
- throw new Error("Iterator is not registered.");
154
- }
155
- if (notifyImmediately) {
156
- callback(state.status);
157
- }
158
- state.listeners.push(callback);
159
- return () => {
160
- const index = state.listeners.indexOf(callback);
161
- if (index !== -1) {
162
- state.listeners.splice(index, 1);
163
- }
164
- };
165
- }
166
-
167
- // src/event-iterator.ts
168
- import { isTypescriptObject, retry } from "@orpc/shared";
169
- import { getEventMeta, withEventMeta } from "@orpc/standard-server";
170
140
  function mapEventIterator(iterator, maps) {
171
141
  return async function* () {
172
142
  try {
@@ -198,84 +168,5 @@ function mapEventIterator(iterator, maps) {
198
168
  }
199
169
  }();
200
170
  }
201
- var MAX_ALLOWED_RETRY_TIMES = 99;
202
- function createAutoRetryEventIterator(initial, reconnect, initialLastEventId) {
203
- const state = {
204
- status: "connected",
205
- listeners: []
206
- };
207
- const iterator = async function* () {
208
- let current = initial;
209
- let lastEventId = initialLastEventId;
210
- let lastRetry;
211
- let retryTimes = 0;
212
- try {
213
- while (true) {
214
- try {
215
- updateEventIteratorStatus(state, "connected");
216
- const { done, value } = await current.next();
217
- const meta = getEventMeta(value);
218
- lastEventId = meta?.id ?? lastEventId;
219
- lastRetry = meta?.retry ?? lastRetry;
220
- retryTimes = 0;
221
- if (done) {
222
- return value;
223
- }
224
- yield value;
225
- } catch (e) {
226
- updateEventIteratorStatus(state, "reconnecting");
227
- const meta = getEventMeta(e);
228
- lastEventId = meta?.id ?? lastEventId;
229
- lastRetry = meta?.retry ?? lastRetry;
230
- let currentError = e;
231
- current = await retry({ times: MAX_ALLOWED_RETRY_TIMES }, async (exit) => {
232
- retryTimes += 1;
233
- if (retryTimes > MAX_ALLOWED_RETRY_TIMES) {
234
- throw exit(new Error(
235
- `Exceeded maximum retry attempts (${MAX_ALLOWED_RETRY_TIMES}) for event source. Possible infinite retry loop detected. Please review the retry logic.`,
236
- { cause: currentError }
237
- ));
238
- }
239
- const reconnected = await (async () => {
240
- try {
241
- return await reconnect({
242
- lastRetry,
243
- lastEventId,
244
- retryTimes,
245
- error: currentError
246
- });
247
- } catch (e2) {
248
- currentError = e2;
249
- throw e2;
250
- }
251
- })();
252
- if (!reconnected) {
253
- throw exit(currentError);
254
- }
255
- return reconnected;
256
- });
257
- }
258
- }
259
- } finally {
260
- updateEventIteratorStatus(state, "closed");
261
- await current.return?.();
262
- }
263
- }();
264
- registerEventIteratorState(iterator, state);
265
- return iterator;
266
- }
267
171
 
268
- export {
269
- COMMON_ORPC_ERROR_DEFS,
270
- fallbackORPCErrorStatus,
271
- fallbackORPCErrorMessage,
272
- ORPCError,
273
- isDefinedError,
274
- toORPCError,
275
- registerEventIteratorState,
276
- updateEventIteratorStatus,
277
- onEventIteratorStatusChange,
278
- mapEventIterator,
279
- createAutoRetryEventIterator
280
- };
281
- //# sourceMappingURL=chunk-7F3XVLRJ.js.map
172
+ export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, toORPCError as t };
@@ -0,0 +1,30 @@
1
+ type ClientContext = Record<string, any>;
2
+ type ClientOptions<TClientContext extends ClientContext> = {
3
+ signal?: AbortSignal;
4
+ lastEventId?: string | undefined;
5
+ } & (Record<never, never> extends TClientContext ? {
6
+ context?: TClientContext;
7
+ } : {
8
+ context: TClientContext;
9
+ });
10
+ type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options: ClientOptions<TClientContext>];
11
+ type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
12
+ __error?: {
13
+ type: TError;
14
+ };
15
+ };
16
+ interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
17
+ (...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
18
+ }
19
+ type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
20
+ [k: string]: NestedClient<TClientContext>;
21
+ };
22
+ type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
23
+ type ClientOptionsOut<TClientContext extends ClientContext> = ClientOptions<TClientContext> & {
24
+ context: TClientContext;
25
+ };
26
+ interface ClientLink<TClientContext extends ClientContext> {
27
+ call: (path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>) => Promise<unknown>;
28
+ }
29
+
30
+ export type { ClientOptionsOut as C, InferClientContext as I, NestedClient as N, ClientContext as a, ClientLink as b, ClientPromiseResult as c, ClientOptions as d, ClientRest as e, Client as f };
@@ -0,0 +1,30 @@
1
+ type ClientContext = Record<string, any>;
2
+ type ClientOptions<TClientContext extends ClientContext> = {
3
+ signal?: AbortSignal;
4
+ lastEventId?: string | undefined;
5
+ } & (Record<never, never> extends TClientContext ? {
6
+ context?: TClientContext;
7
+ } : {
8
+ context: TClientContext;
9
+ });
10
+ type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options: ClientOptions<TClientContext>];
11
+ type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
12
+ __error?: {
13
+ type: TError;
14
+ };
15
+ };
16
+ interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
17
+ (...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
18
+ }
19
+ type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
20
+ [k: string]: NestedClient<TClientContext>;
21
+ };
22
+ type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
23
+ type ClientOptionsOut<TClientContext extends ClientContext> = ClientOptions<TClientContext> & {
24
+ context: TClientContext;
25
+ };
26
+ interface ClientLink<TClientContext extends ClientContext> {
27
+ call: (path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>) => Promise<unknown>;
28
+ }
29
+
30
+ export type { ClientOptionsOut as C, InferClientContext as I, NestedClient as N, ClientContext as a, ClientLink as b, ClientPromiseResult as c, ClientOptions as d, ClientRest as e, Client as f };
@@ -0,0 +1,12 @@
1
+ class CompositeClientPlugin {
2
+ constructor(plugins = []) {
3
+ this.plugins = plugins;
4
+ }
5
+ init(options) {
6
+ for (const plugin of this.plugins) {
7
+ plugin.init?.(options);
8
+ }
9
+ }
10
+ }
11
+
12
+ export { CompositeClientPlugin as C };
@@ -0,0 +1,45 @@
1
+ import { Interceptor } from '@orpc/shared';
2
+ import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
3
+ import { a as ClientContext, C as ClientOptionsOut, b as ClientLink } from './client.CupM8eRP.mjs';
4
+
5
+ interface StandardLinkCodec<T extends ClientContext> {
6
+ encode(path: readonly string[], input: unknown, options: ClientOptionsOut<any>): Promise<StandardRequest>;
7
+ decode(response: StandardLazyResponse, options: ClientOptionsOut<T>, path: readonly string[], input: unknown): Promise<unknown>;
8
+ }
9
+ interface StandardLinkClient<T extends ClientContext> {
10
+ call(request: StandardRequest, options: ClientOptionsOut<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
11
+ }
12
+
13
+ declare class InvalidEventIteratorRetryResponse extends Error {
14
+ }
15
+ interface StandardLinkOptions<T extends ClientContext> {
16
+ interceptors?: Interceptor<{
17
+ path: readonly string[];
18
+ input: unknown;
19
+ options: ClientOptionsOut<T>;
20
+ }, unknown, unknown>[];
21
+ clientInterceptors?: Interceptor<{
22
+ request: StandardRequest;
23
+ }, StandardLazyResponse, unknown>[];
24
+ plugins?: ClientPlugin<T>[];
25
+ }
26
+ declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
27
+ #private;
28
+ readonly codec: StandardLinkCodec<T>;
29
+ readonly sender: StandardLinkClient<T>;
30
+ private readonly interceptors;
31
+ private readonly clientInterceptors;
32
+ constructor(codec: StandardLinkCodec<T>, sender: StandardLinkClient<T>, options?: StandardLinkOptions<T>);
33
+ call(path: readonly string[], input: unknown, options: ClientOptionsOut<T>): Promise<unknown>;
34
+ }
35
+
36
+ interface ClientPlugin<T extends ClientContext> {
37
+ init?(options: StandardLinkOptions<T>): void;
38
+ }
39
+ declare class CompositeClientPlugin<T extends ClientContext> implements ClientPlugin<T> {
40
+ private readonly plugins;
41
+ constructor(plugins?: ClientPlugin<T>[]);
42
+ init(options: StandardLinkOptions<T>): void;
43
+ }
44
+
45
+ export { type ClientPlugin as C, InvalidEventIteratorRetryResponse as I, type StandardLinkOptions as S, CompositeClientPlugin as a, type StandardLinkClient as b, type StandardLinkCodec as c, StandardLink as d };
@@ -0,0 +1,45 @@
1
+ import { Interceptor } from '@orpc/shared';
2
+ import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
3
+ import { a as ClientContext, C as ClientOptionsOut, b as ClientLink } from './client.CupM8eRP.js';
4
+
5
+ interface StandardLinkCodec<T extends ClientContext> {
6
+ encode(path: readonly string[], input: unknown, options: ClientOptionsOut<any>): Promise<StandardRequest>;
7
+ decode(response: StandardLazyResponse, options: ClientOptionsOut<T>, path: readonly string[], input: unknown): Promise<unknown>;
8
+ }
9
+ interface StandardLinkClient<T extends ClientContext> {
10
+ call(request: StandardRequest, options: ClientOptionsOut<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
11
+ }
12
+
13
+ declare class InvalidEventIteratorRetryResponse extends Error {
14
+ }
15
+ interface StandardLinkOptions<T extends ClientContext> {
16
+ interceptors?: Interceptor<{
17
+ path: readonly string[];
18
+ input: unknown;
19
+ options: ClientOptionsOut<T>;
20
+ }, unknown, unknown>[];
21
+ clientInterceptors?: Interceptor<{
22
+ request: StandardRequest;
23
+ }, StandardLazyResponse, unknown>[];
24
+ plugins?: ClientPlugin<T>[];
25
+ }
26
+ declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
27
+ #private;
28
+ readonly codec: StandardLinkCodec<T>;
29
+ readonly sender: StandardLinkClient<T>;
30
+ private readonly interceptors;
31
+ private readonly clientInterceptors;
32
+ constructor(codec: StandardLinkCodec<T>, sender: StandardLinkClient<T>, options?: StandardLinkOptions<T>);
33
+ call(path: readonly string[], input: unknown, options: ClientOptionsOut<T>): Promise<unknown>;
34
+ }
35
+
36
+ interface ClientPlugin<T extends ClientContext> {
37
+ init?(options: StandardLinkOptions<T>): void;
38
+ }
39
+ declare class CompositeClientPlugin<T extends ClientContext> implements ClientPlugin<T> {
40
+ private readonly plugins;
41
+ constructor(plugins?: ClientPlugin<T>[]);
42
+ init(options: StandardLinkOptions<T>): void;
43
+ }
44
+
45
+ export { type ClientPlugin as C, InvalidEventIteratorRetryResponse as I, type StandardLinkOptions as S, CompositeClientPlugin as a, type StandardLinkClient as b, type StandardLinkCodec as c, StandardLink as d };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/client",
3
3
  "type": "module",
4
- "version": "0.0.0-next.b36125c",
4
+ "version": "0.0.0-next.b3afdeb",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,44 +15,39 @@
15
15
  ],
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/src/index.d.ts",
19
- "import": "./dist/index.js",
20
- "default": "./dist/index.js"
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs",
20
+ "default": "./dist/index.mjs"
21
21
  },
22
- "./openapi": {
23
- "types": "./dist/src/openapi/index.d.ts",
24
- "import": "./dist/openapi.js",
25
- "default": "./dist/openapi.js"
22
+ "./plugins": {
23
+ "types": "./dist/plugins/index.d.mts",
24
+ "import": "./dist/plugins/index.mjs",
25
+ "default": "./dist/plugins/index.mjs"
26
26
  },
27
- "./rpc": {
28
- "types": "./dist/src/rpc/index.d.ts",
29
- "import": "./dist/rpc.js",
30
- "default": "./dist/rpc.js"
27
+ "./standard": {
28
+ "types": "./dist/adapters/standard/index.d.mts",
29
+ "import": "./dist/adapters/standard/index.mjs",
30
+ "default": "./dist/adapters/standard/index.mjs"
31
31
  },
32
32
  "./fetch": {
33
- "types": "./dist/src/adapters/fetch/index.d.ts",
34
- "import": "./dist/fetch.js",
35
- "default": "./dist/fetch.js"
36
- },
37
- "./🔒/*": {
38
- "types": "./dist/src/*.d.ts"
33
+ "types": "./dist/adapters/fetch/index.d.mts",
34
+ "import": "./dist/adapters/fetch/index.mjs",
35
+ "default": "./dist/adapters/fetch/index.mjs"
39
36
  }
40
37
  },
41
38
  "files": [
42
- "!**/*.map",
43
- "!**/*.tsbuildinfo",
44
39
  "dist"
45
40
  ],
46
41
  "dependencies": {
47
- "@orpc/standard-server": "0.0.0-next.b36125c",
48
- "@orpc/shared": "0.0.0-next.b36125c",
49
- "@orpc/standard-server-fetch": "0.0.0-next.b36125c"
42
+ "@orpc/shared": "0.0.0-next.b3afdeb",
43
+ "@orpc/standard-server-fetch": "0.0.0-next.b3afdeb",
44
+ "@orpc/standard-server": "0.0.0-next.b3afdeb"
50
45
  },
51
46
  "devDependencies": {
52
- "zod": "^3.24.1"
47
+ "zod": "^3.24.2"
53
48
  },
54
49
  "scripts": {
55
- "build": "tsup --onSuccess='tsc -b --noCheck'",
50
+ "build": "unbuild",
56
51
  "build:watch": "pnpm run build --watch",
57
52
  "type:check": "tsc -b"
58
53
  }
@@ -1,207 +0,0 @@
1
- import {
2
- ORPCError,
3
- mapEventIterator,
4
- toORPCError
5
- } from "./chunk-7F3XVLRJ.js";
6
-
7
- // src/rpc/json-serializer.ts
8
- import { isObject } from "@orpc/shared";
9
- var RPCJsonSerializer = class {
10
- serialize(data, segments = [], meta = [], maps = [], blobs = []) {
11
- if (data instanceof Blob) {
12
- maps.push(segments);
13
- blobs.push(data);
14
- return [data, meta, maps, blobs];
15
- }
16
- if (typeof data === "bigint") {
17
- meta.push([0, segments]);
18
- return [data.toString(), meta, maps, blobs];
19
- }
20
- if (data instanceof Date) {
21
- meta.push([1, segments]);
22
- if (Number.isNaN(data.getTime())) {
23
- return [null, meta, maps, blobs];
24
- }
25
- return [data.toISOString(), meta, maps, blobs];
26
- }
27
- if (Number.isNaN(data)) {
28
- meta.push([2, segments]);
29
- return [null, meta, maps, blobs];
30
- }
31
- if (data instanceof URL) {
32
- meta.push([4, segments]);
33
- return [data.toString(), meta, maps, blobs];
34
- }
35
- if (data instanceof RegExp) {
36
- meta.push([5, segments]);
37
- return [data.toString(), meta, maps, blobs];
38
- }
39
- if (data instanceof Set) {
40
- const result = this.serialize(Array.from(data), segments, meta, maps, blobs);
41
- meta.push([6, segments]);
42
- return result;
43
- }
44
- if (data instanceof Map) {
45
- const result = this.serialize(Array.from(data.entries()), segments, meta, maps, blobs);
46
- meta.push([7, segments]);
47
- return result;
48
- }
49
- if (Array.isArray(data)) {
50
- const json = data.map((v, i) => {
51
- if (v === void 0) {
52
- meta.push([3, [...segments, i]]);
53
- return v;
54
- }
55
- return this.serialize(v, [...segments, i], meta, maps, blobs)[0];
56
- });
57
- return [json, meta, maps, blobs];
58
- }
59
- if (isObject(data)) {
60
- const json = {};
61
- for (const k in data) {
62
- json[k] = this.serialize(data[k], [...segments, k], meta, maps, blobs)[0];
63
- }
64
- return [json, meta, maps, blobs];
65
- }
66
- return [data, meta, maps, blobs];
67
- }
68
- deserialize(json, meta, maps, getBlob) {
69
- const ref = { data: json };
70
- if (maps && getBlob) {
71
- maps.forEach((segments, i) => {
72
- let currentRef = ref;
73
- let preSegment = "data";
74
- segments.forEach((segment) => {
75
- currentRef = currentRef[preSegment];
76
- preSegment = segment;
77
- });
78
- currentRef[preSegment] = getBlob(i);
79
- });
80
- }
81
- for (const [type, segments] of meta) {
82
- let currentRef = ref;
83
- let preSegment = "data";
84
- segments.forEach((segment) => {
85
- currentRef = currentRef[preSegment];
86
- preSegment = segment;
87
- });
88
- switch (type) {
89
- case 0:
90
- currentRef[preSegment] = BigInt(currentRef[preSegment]);
91
- break;
92
- case 1:
93
- currentRef[preSegment] = new Date(currentRef[preSegment] ?? "Invalid Date");
94
- break;
95
- case 2:
96
- currentRef[preSegment] = Number.NaN;
97
- break;
98
- case 3:
99
- currentRef[preSegment] = void 0;
100
- break;
101
- case 4:
102
- currentRef[preSegment] = new URL(currentRef[preSegment]);
103
- break;
104
- case 5: {
105
- const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
106
- currentRef[preSegment] = new RegExp(pattern, flags);
107
- break;
108
- }
109
- case 6:
110
- currentRef[preSegment] = new Set(currentRef[preSegment]);
111
- break;
112
- case 7:
113
- currentRef[preSegment] = new Map(currentRef[preSegment]);
114
- break;
115
- /* v8 ignore next 3 */
116
- default: {
117
- const _expected = type;
118
- }
119
- }
120
- }
121
- return ref.data;
122
- }
123
- };
124
-
125
- // src/rpc/serializer.ts
126
- import { isAsyncIteratorObject, stringifyJSON } from "@orpc/shared";
127
- import { ErrorEvent } from "@orpc/standard-server";
128
- var RPCSerializer = class {
129
- constructor(jsonSerializer = new RPCJsonSerializer()) {
130
- this.jsonSerializer = jsonSerializer;
131
- }
132
- serialize(data) {
133
- if (isAsyncIteratorObject(data)) {
134
- return mapEventIterator(data, {
135
- value: async (value) => this.#serialize(value, false),
136
- error: async (e) => {
137
- return new ErrorEvent({
138
- data: this.#serialize(toORPCError(e).toJSON(), false),
139
- cause: e
140
- });
141
- }
142
- });
143
- }
144
- return this.#serialize(data, true);
145
- }
146
- #serialize(data, enableFormData) {
147
- if (data === void 0 || data instanceof Blob) {
148
- return data;
149
- }
150
- const [json, meta_, maps, blobs] = this.jsonSerializer.serialize(data);
151
- const meta = meta_.length === 0 ? void 0 : meta_;
152
- if (!enableFormData || blobs.length === 0) {
153
- return {
154
- json,
155
- meta
156
- };
157
- }
158
- const form = new FormData();
159
- form.set("data", stringifyJSON({ json, meta, maps }));
160
- blobs.forEach((blob, i) => {
161
- form.set(i.toString(), blob);
162
- });
163
- return form;
164
- }
165
- deserialize(data) {
166
- if (isAsyncIteratorObject(data)) {
167
- return mapEventIterator(data, {
168
- value: async (value) => this.#deserialize(value),
169
- error: async (e) => {
170
- if (!(e instanceof ErrorEvent)) {
171
- return e;
172
- }
173
- const deserialized = this.#deserialize(e.data);
174
- if (ORPCError.isValidJSON(deserialized)) {
175
- return ORPCError.fromJSON(deserialized, { cause: e });
176
- }
177
- return new ErrorEvent({
178
- data: deserialized,
179
- cause: e
180
- });
181
- }
182
- });
183
- }
184
- return this.#deserialize(data);
185
- }
186
- #deserialize(data) {
187
- if (data === void 0 || data instanceof Blob) {
188
- return data;
189
- }
190
- if (!(data instanceof FormData)) {
191
- return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
192
- }
193
- const serialized = JSON.parse(data.get("data"));
194
- return this.jsonSerializer.deserialize(
195
- serialized.json,
196
- serialized.meta ?? [],
197
- serialized.maps,
198
- (i) => data.get(i.toString())
199
- );
200
- }
201
- };
202
-
203
- export {
204
- RPCJsonSerializer,
205
- RPCSerializer
206
- };
207
- //# sourceMappingURL=chunk-I4MCMTJ2.js.map