@orpc/client 2.0.0-beta.47 → 2.0.0-beta.49
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 +6 -7
- package/dist/adapters/fetch/index.d.ts +2 -48
- package/dist/adapters/fetch/index.js +2 -0
- package/dist/adapters/fetch/rpc-link.d.ts +14 -0
- package/dist/adapters/fetch/rpc-link.js +14 -0
- package/dist/adapters/fetch/transport.d.ts +34 -0
- package/dist/adapters/fetch/transport.js +65 -0
- package/dist/adapters/message-port/index.d.ts +3 -80
- package/dist/adapters/message-port/index.js +3 -0
- package/dist/adapters/message-port/message-port.d.ts +29 -0
- package/dist/adapters/message-port/message-port.js +56 -0
- package/dist/adapters/message-port/rpc-link.d.ts +15 -0
- package/dist/adapters/message-port/rpc-link.js +15 -0
- package/dist/adapters/message-port/transport.d.ts +38 -0
- package/dist/adapters/message-port/transport.js +52 -0
- package/dist/adapters/standard/codec.d.ts +14 -0
- package/dist/adapters/standard/codec.js +1 -0
- package/dist/adapters/standard/index.d.ts +5 -60
- package/dist/adapters/standard/index.js +4 -0
- package/dist/adapters/standard/link.d.ts +40 -0
- package/dist/adapters/standard/link.js +85 -0
- package/dist/adapters/standard/plugin.d.ts +32 -0
- package/dist/adapters/standard/plugin.js +16 -0
- package/dist/adapters/standard/{index.d.mts → rpc-link-codec.d.ts} +17 -14
- package/dist/adapters/standard/rpc-link-codec.js +104 -0
- package/dist/adapters/standard/transport.d.ts +14 -0
- package/dist/adapters/standard/transport.js +1 -0
- package/dist/adapters/websocket/index.d.ts +2 -136
- package/dist/adapters/websocket/index.js +2 -0
- package/dist/adapters/websocket/rpc-link.d.ts +14 -0
- package/dist/adapters/websocket/rpc-link.js +14 -0
- package/dist/adapters/websocket/{index.d.mts → transport.d.ts} +32 -53
- package/dist/adapters/websocket/transport.js +133 -0
- package/dist/async-iterator-object.d.ts +2 -0
- package/dist/async-iterator-object.js +29 -0
- package/dist/client-safe.d.ts +18 -0
- package/dist/client-safe.js +36 -0
- package/dist/client.d.ts +40 -0
- package/dist/client.js +39 -0
- package/dist/consts.d.ts +10 -0
- package/dist/consts.js +43 -0
- package/dist/dynamic-link.d.ts +13 -0
- package/dist/dynamic-link.js +17 -0
- package/dist/error-utils.d.ts +21 -0
- package/dist/error-utils.js +58 -0
- package/dist/error.d.ts +132 -0
- package/dist/error.js +178 -0
- package/dist/index.d.ts +38 -129
- package/dist/index.js +40 -0
- package/dist/plugins/batch.d.ts +185 -0
- package/dist/plugins/batch.js +401 -0
- package/dist/plugins/cookie-jar.d.ts +82 -0
- package/dist/plugins/cookie-jar.js +268 -0
- package/dist/plugins/dedupe.d.ts +64 -0
- package/dist/plugins/dedupe.js +226 -0
- package/dist/plugins/index.d.ts +20 -412
- package/dist/plugins/index.js +23 -0
- package/dist/plugins/request-compression.d.ts +44 -0
- package/dist/plugins/request-compression.js +178 -0
- package/dist/plugins/response-compression.d.ts +28 -0
- package/dist/plugins/response-compression.js +91 -0
- package/dist/plugins/retry-after.d.ts +50 -0
- package/dist/plugins/retry-after.js +88 -0
- package/dist/plugins/retry.d.ts +84 -0
- package/dist/plugins/retry.js +140 -0
- package/dist/plugins/timeout.d.ts +25 -0
- package/dist/plugins/timeout.js +37 -0
- package/dist/{shared/client.DqIWeNhQ.d.mts → rpc-json-serializer.d.ts} +9 -41
- package/dist/rpc-json-serializer.js +392 -0
- package/dist/rpc-serializer.d.ts +32 -0
- package/dist/rpc-serializer.js +130 -0
- package/dist/types.d.ts +97 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +47 -0
- package/dist/utils.js +51 -0
- package/package.json +43 -45
- package/dist/adapters/fetch/index.d.mts +0 -48
- package/dist/adapters/fetch/index.mjs +0 -77
- package/dist/adapters/message-port/index.d.mts +0 -80
- package/dist/adapters/message-port/index.mjs +0 -101
- package/dist/adapters/standard/index.mjs +0 -6
- package/dist/adapters/websocket/index.mjs +0 -131
- package/dist/index.d.mts +0 -144
- package/dist/index.mjs +0 -147
- package/dist/plugins/index.d.mts +0 -420
- package/dist/plugins/index.mjs +0 -971
- package/dist/shared/client.BoHJeq15.d.mts +0 -233
- package/dist/shared/client.BoHJeq15.d.ts +0 -233
- package/dist/shared/client.CKQeJJ2U.mjs +0 -172
- package/dist/shared/client.DqIWeNhQ.d.ts +0 -131
- package/dist/shared/client.JBJwBhR8.d.ts +0 -90
- package/dist/shared/client.LF04eOvL.mjs +0 -115
- package/dist/shared/client._wG5INmC.mjs +0 -505
- package/dist/shared/client.euYjjcFz.d.mts +0 -90
|
@@ -1,505 +0,0 @@
|
|
|
1
|
-
import { isPlainObject, wrapAsyncIterator, isTypescriptObject, NullProtoObj, parseBigIntString, DEFAULT_MAX_BIGINT_DIGITS, copyOnWrite, isAsyncIteratorObject, stringifyJSON } from '@orpc/shared';
|
|
2
|
-
import { getEventMeta, withEventMeta, ErrorEvent } from '@standard-server/core';
|
|
3
|
-
import { O as ORPCError } from './client.LF04eOvL.mjs';
|
|
4
|
-
|
|
5
|
-
function isDefinedError(error) {
|
|
6
|
-
return error instanceof ORPCError && error.defined;
|
|
7
|
-
}
|
|
8
|
-
function toORPCError(error) {
|
|
9
|
-
return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", { cause: error });
|
|
10
|
-
}
|
|
11
|
-
function isORPCErrorJson(json) {
|
|
12
|
-
if (!isPlainObject(json)) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
const validKeys = ["defined", "code", "message", "data"];
|
|
16
|
-
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "message" in json && typeof json.message === "string";
|
|
20
|
-
}
|
|
21
|
-
function createORPCErrorFromJson(json, options = {}) {
|
|
22
|
-
const error = new ORPCError(json.code, {
|
|
23
|
-
...json,
|
|
24
|
-
...options
|
|
25
|
-
});
|
|
26
|
-
error.defined = json.defined;
|
|
27
|
-
return error;
|
|
28
|
-
}
|
|
29
|
-
function cloneORPCError(error) {
|
|
30
|
-
const cloned = new ORPCError(error.code, {
|
|
31
|
-
message: error.message,
|
|
32
|
-
data: error.data,
|
|
33
|
-
cause: error.cause
|
|
34
|
-
});
|
|
35
|
-
Object.setPrototypeOf(cloned, Object.getPrototypeOf(error));
|
|
36
|
-
Object.defineProperties(cloned, Object.getOwnPropertyDescriptors(error));
|
|
37
|
-
cloned.stack = error.stack;
|
|
38
|
-
return cloned;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function wrapAsyncIteratorPreservingEventMeta(iterator, { mapResult, mapError, ...rest }) {
|
|
42
|
-
return wrapAsyncIterator(iterator, {
|
|
43
|
-
...rest,
|
|
44
|
-
mapResult: mapResult && (async (result) => {
|
|
45
|
-
const mapped = await mapResult(result);
|
|
46
|
-
if (mapped.value !== result.value) {
|
|
47
|
-
const meta = getEventMeta(result.value);
|
|
48
|
-
if (meta && isTypescriptObject(mapped.value)) {
|
|
49
|
-
return { done: mapped.done, value: withEventMeta(mapped.value, meta) };
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return mapped;
|
|
53
|
-
}),
|
|
54
|
-
mapError: mapError && (async (error) => {
|
|
55
|
-
const mapped = await mapError(error);
|
|
56
|
-
if (mapped !== error) {
|
|
57
|
-
const meta = getEventMeta(error);
|
|
58
|
-
if (meta && isTypescriptObject(mapped)) {
|
|
59
|
-
return withEventMeta(mapped, meta);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return mapped;
|
|
63
|
-
})
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function invalidSerializedData(detail) {
|
|
68
|
-
return new TypeError(`Invalid RPC serialized data: ${detail}`);
|
|
69
|
-
}
|
|
70
|
-
function assertSerializedType(ok, type, expected) {
|
|
71
|
-
if (!ok) {
|
|
72
|
-
throw invalidSerializedData(`type "${type}" expects ${expected}.`);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
function isWalkable(value) {
|
|
76
|
-
return Array.isArray(value) || isPlainObject(value);
|
|
77
|
-
}
|
|
78
|
-
function walkInto(parent, key, original, restored) {
|
|
79
|
-
const value = parent[key];
|
|
80
|
-
if (!isWalkable(value) || restored?.has(value)) {
|
|
81
|
-
throw invalidSerializedData(`segment "${key}" cannot be walked into.`);
|
|
82
|
-
}
|
|
83
|
-
return value === original ? copyOnWrite(parent, key, original) : value;
|
|
84
|
-
}
|
|
85
|
-
const DEFAULT_RPC_JSON_SERIALIZER_HANDLERS = {
|
|
86
|
-
undefined: {
|
|
87
|
-
condition(data) {
|
|
88
|
-
return data === void 0;
|
|
89
|
-
},
|
|
90
|
-
serialize() {
|
|
91
|
-
return null;
|
|
92
|
-
},
|
|
93
|
-
deserialize(serialized) {
|
|
94
|
-
assertSerializedType(serialized === null, "undefined", "null");
|
|
95
|
-
return void 0;
|
|
96
|
-
},
|
|
97
|
-
isTerminal: true
|
|
98
|
-
},
|
|
99
|
-
bigint: {
|
|
100
|
-
condition(data) {
|
|
101
|
-
return typeof data === "bigint";
|
|
102
|
-
},
|
|
103
|
-
serialize(data) {
|
|
104
|
-
return data.toString();
|
|
105
|
-
},
|
|
106
|
-
deserialize(serialized) {
|
|
107
|
-
assertSerializedType(typeof serialized === "string", "bigint", "a string");
|
|
108
|
-
const value = parseBigIntString(serialized, DEFAULT_MAX_BIGINT_DIGITS);
|
|
109
|
-
if (value === void 0) {
|
|
110
|
-
throw invalidSerializedData(`type "bigint" expects a decimal integer string of at most ${DEFAULT_MAX_BIGINT_DIGITS} digits.`);
|
|
111
|
-
}
|
|
112
|
-
return value;
|
|
113
|
-
},
|
|
114
|
-
isTerminal: true
|
|
115
|
-
},
|
|
116
|
-
date: {
|
|
117
|
-
condition(data) {
|
|
118
|
-
return data instanceof Date;
|
|
119
|
-
},
|
|
120
|
-
serialize(data) {
|
|
121
|
-
if (Number.isNaN(data.getTime())) {
|
|
122
|
-
return null;
|
|
123
|
-
}
|
|
124
|
-
return data.toISOString();
|
|
125
|
-
},
|
|
126
|
-
deserialize(serialized) {
|
|
127
|
-
assertSerializedType(typeof serialized === "string" || serialized === null, "date", "a string or null");
|
|
128
|
-
return new Date(serialized ?? Number.NaN);
|
|
129
|
-
},
|
|
130
|
-
isTerminal: true
|
|
131
|
-
},
|
|
132
|
-
nan: {
|
|
133
|
-
condition(data) {
|
|
134
|
-
return typeof data === "number" && Number.isNaN(data);
|
|
135
|
-
},
|
|
136
|
-
serialize() {
|
|
137
|
-
return null;
|
|
138
|
-
},
|
|
139
|
-
deserialize(serialized) {
|
|
140
|
-
assertSerializedType(serialized === null, "nan", "null");
|
|
141
|
-
return Number.NaN;
|
|
142
|
-
},
|
|
143
|
-
isTerminal: true
|
|
144
|
-
},
|
|
145
|
-
infinity: {
|
|
146
|
-
condition(data) {
|
|
147
|
-
return data === Number.POSITIVE_INFINITY || data === Number.NEGATIVE_INFINITY;
|
|
148
|
-
},
|
|
149
|
-
serialize(data) {
|
|
150
|
-
return data > 0 ? "Infinity" : "-Infinity";
|
|
151
|
-
},
|
|
152
|
-
deserialize(serialized) {
|
|
153
|
-
assertSerializedType(serialized === "Infinity" || serialized === "-Infinity", "infinity", '"Infinity" or "-Infinity"');
|
|
154
|
-
return serialized === "Infinity" ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
|
|
155
|
-
},
|
|
156
|
-
isTerminal: true
|
|
157
|
-
},
|
|
158
|
-
url: {
|
|
159
|
-
condition(data) {
|
|
160
|
-
return data instanceof URL;
|
|
161
|
-
},
|
|
162
|
-
serialize(data) {
|
|
163
|
-
return data.toString();
|
|
164
|
-
},
|
|
165
|
-
deserialize(serialized) {
|
|
166
|
-
assertSerializedType(typeof serialized === "string", "url", "a string");
|
|
167
|
-
return new URL(serialized);
|
|
168
|
-
},
|
|
169
|
-
isTerminal: true
|
|
170
|
-
},
|
|
171
|
-
set: {
|
|
172
|
-
condition(data) {
|
|
173
|
-
return data instanceof Set;
|
|
174
|
-
},
|
|
175
|
-
serialize(data) {
|
|
176
|
-
return Array.from(data);
|
|
177
|
-
},
|
|
178
|
-
deserialize(serialized) {
|
|
179
|
-
assertSerializedType(Array.isArray(serialized), "set", "an array");
|
|
180
|
-
return new Set(serialized);
|
|
181
|
-
}
|
|
182
|
-
},
|
|
183
|
-
map: {
|
|
184
|
-
condition(data) {
|
|
185
|
-
return data instanceof Map;
|
|
186
|
-
},
|
|
187
|
-
serialize(data) {
|
|
188
|
-
return Array.from(data.entries());
|
|
189
|
-
},
|
|
190
|
-
deserialize(serialized) {
|
|
191
|
-
assertSerializedType(Array.isArray(serialized), "map", "an array");
|
|
192
|
-
return new Map(serialized);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
class RPCJsonSerializer {
|
|
197
|
-
handlers;
|
|
198
|
-
inlineBuiltInHandlers;
|
|
199
|
-
handlerEntries;
|
|
200
|
-
omitUndefinedProperties;
|
|
201
|
-
constructor(options = {}) {
|
|
202
|
-
this.omitUndefinedProperties = options.omitUndefinedProperties !== false;
|
|
203
|
-
this.handlers = Object.assign(new NullProtoObj(), DEFAULT_RPC_JSON_SERIALIZER_HANDLERS);
|
|
204
|
-
const customHandlers = options.handlers;
|
|
205
|
-
if (customHandlers === void 0) {
|
|
206
|
-
this.inlineBuiltInHandlers = true;
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
let inlineBuiltInHandlers = true;
|
|
210
|
-
let handlerEntries = [];
|
|
211
|
-
for (const key of Object.keys(customHandlers)) {
|
|
212
|
-
const handler = customHandlers[key];
|
|
213
|
-
this.handlers[key] = handler;
|
|
214
|
-
if (inlineBuiltInHandlers && key in DEFAULT_RPC_JSON_SERIALIZER_HANDLERS) {
|
|
215
|
-
inlineBuiltInHandlers = false;
|
|
216
|
-
}
|
|
217
|
-
if (inlineBuiltInHandlers && handler !== void 0) {
|
|
218
|
-
handlerEntries.push([key, handler]);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
if (!inlineBuiltInHandlers) {
|
|
222
|
-
handlerEntries = [];
|
|
223
|
-
for (const key of Object.keys(this.handlers)) {
|
|
224
|
-
const handler = this.handlers[key];
|
|
225
|
-
if (handler !== void 0) {
|
|
226
|
-
handlerEntries.push([key, handler]);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
this.inlineBuiltInHandlers = inlineBuiltInHandlers;
|
|
231
|
-
this.handlerEntries = handlerEntries;
|
|
232
|
-
}
|
|
233
|
-
serializeJson(data) {
|
|
234
|
-
let meta = [];
|
|
235
|
-
const maps = [];
|
|
236
|
-
const blobs = [];
|
|
237
|
-
const json = this.serializeValue(data, [], meta, maps, blobs);
|
|
238
|
-
meta = meta.length === 0 ? void 0 : meta;
|
|
239
|
-
if (maps.length === 0) {
|
|
240
|
-
return { json, meta };
|
|
241
|
-
}
|
|
242
|
-
return { json, meta, maps, blobs };
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* `segments` is a shared mutable stack (push/pop while walking),
|
|
246
|
-
* so it must be copied before being stored in `meta` or `maps`.
|
|
247
|
-
*/
|
|
248
|
-
serializeValue(data, segments, meta, maps, blobs) {
|
|
249
|
-
if (this.inlineBuiltInHandlers) {
|
|
250
|
-
switch (typeof data) {
|
|
251
|
-
case "string":
|
|
252
|
-
case "boolean":
|
|
253
|
-
return data;
|
|
254
|
-
case "number":
|
|
255
|
-
if (Number.isFinite(data)) {
|
|
256
|
-
return data;
|
|
257
|
-
}
|
|
258
|
-
if (Number.isNaN(data)) {
|
|
259
|
-
meta.push(["nan", ...segments]);
|
|
260
|
-
return null;
|
|
261
|
-
}
|
|
262
|
-
meta.push(["infinity", ...segments]);
|
|
263
|
-
return data > 0 ? "Infinity" : "-Infinity";
|
|
264
|
-
case "undefined":
|
|
265
|
-
meta.push(["undefined", ...segments]);
|
|
266
|
-
return null;
|
|
267
|
-
case "bigint":
|
|
268
|
-
meta.push(["bigint", ...segments]);
|
|
269
|
-
return data.toString();
|
|
270
|
-
case "object": {
|
|
271
|
-
if (data === null) {
|
|
272
|
-
return data;
|
|
273
|
-
}
|
|
274
|
-
if (data instanceof Date) {
|
|
275
|
-
meta.push(["date", ...segments]);
|
|
276
|
-
return Number.isNaN(data.getTime()) ? null : data.toISOString();
|
|
277
|
-
}
|
|
278
|
-
if (data instanceof URL) {
|
|
279
|
-
meta.push(["url", ...segments]);
|
|
280
|
-
return data.toString();
|
|
281
|
-
}
|
|
282
|
-
if (data instanceof Set) {
|
|
283
|
-
const result = this.serializeValue(Array.from(data), segments, meta, maps, blobs);
|
|
284
|
-
meta.push(["set", ...segments]);
|
|
285
|
-
return result;
|
|
286
|
-
}
|
|
287
|
-
if (data instanceof Map) {
|
|
288
|
-
const result = this.serializeValue(Array.from(data.entries()), segments, meta, maps, blobs);
|
|
289
|
-
meta.push(["map", ...segments]);
|
|
290
|
-
return result;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
const handlerEntries = this.handlerEntries;
|
|
296
|
-
if (handlerEntries) {
|
|
297
|
-
for (let i = 0; i < handlerEntries.length; i++) {
|
|
298
|
-
const entry = handlerEntries[i];
|
|
299
|
-
const handler = entry[1];
|
|
300
|
-
if (handler.condition(data)) {
|
|
301
|
-
const serialized = handler.serialize(data);
|
|
302
|
-
if (handler.isTerminal) {
|
|
303
|
-
meta.push([entry[0], ...segments]);
|
|
304
|
-
if (serialized instanceof Blob) {
|
|
305
|
-
maps.push(segments.slice());
|
|
306
|
-
blobs.push(serialized);
|
|
307
|
-
}
|
|
308
|
-
return serialized;
|
|
309
|
-
}
|
|
310
|
-
const result = this.serializeValue(serialized, segments, meta, maps, blobs);
|
|
311
|
-
meta.push([entry[0], ...segments]);
|
|
312
|
-
return result;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
if (data instanceof Blob) {
|
|
317
|
-
maps.push(segments.slice());
|
|
318
|
-
blobs.push(data);
|
|
319
|
-
return data;
|
|
320
|
-
}
|
|
321
|
-
if (Array.isArray(data)) {
|
|
322
|
-
const json = [];
|
|
323
|
-
for (let i = 0; i < data.length; i++) {
|
|
324
|
-
segments.push(i);
|
|
325
|
-
json.push(this.serializeValue(data[i], segments, meta, maps, blobs));
|
|
326
|
-
segments.pop();
|
|
327
|
-
}
|
|
328
|
-
return json;
|
|
329
|
-
}
|
|
330
|
-
if (isPlainObject(data)) {
|
|
331
|
-
const json = new NullProtoObj();
|
|
332
|
-
for (const k of Object.keys(data)) {
|
|
333
|
-
const v = data[k];
|
|
334
|
-
if (k === "toJSON" && typeof v === "function") {
|
|
335
|
-
continue;
|
|
336
|
-
}
|
|
337
|
-
if (v === void 0 && this.omitUndefinedProperties) {
|
|
338
|
-
continue;
|
|
339
|
-
}
|
|
340
|
-
segments.push(k);
|
|
341
|
-
json[k] = this.serializeValue(v, segments, meta, maps, blobs);
|
|
342
|
-
segments.pop();
|
|
343
|
-
}
|
|
344
|
-
return json;
|
|
345
|
-
}
|
|
346
|
-
return data;
|
|
347
|
-
}
|
|
348
|
-
deserializeJson(serialized) {
|
|
349
|
-
const ref = { json: serialized.json };
|
|
350
|
-
if (serialized.blobs?.length) {
|
|
351
|
-
for (let i = 0; i < serialized.maps.length; i++) {
|
|
352
|
-
const blob = serialized.blobs[i];
|
|
353
|
-
if (!(blob instanceof Blob)) {
|
|
354
|
-
throw invalidSerializedData(`blob ${i} is not a Blob.`);
|
|
355
|
-
}
|
|
356
|
-
const segments = serialized.maps[i];
|
|
357
|
-
let original = serialized;
|
|
358
|
-
let currentRef = ref;
|
|
359
|
-
let preSegment = "json";
|
|
360
|
-
for (let j = 0; j < segments.length; j++) {
|
|
361
|
-
original = original[preSegment];
|
|
362
|
-
currentRef = walkInto(currentRef, preSegment, original);
|
|
363
|
-
preSegment = segments[j];
|
|
364
|
-
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
365
|
-
throw invalidSerializedData(`segment "${preSegment}" does not exist.`);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
currentRef[preSegment] = blob;
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
const meta = serialized.meta;
|
|
372
|
-
if (meta) {
|
|
373
|
-
let restored;
|
|
374
|
-
const chain = meta.length === 1 ? void 0 : [];
|
|
375
|
-
let chainDepth = 0;
|
|
376
|
-
for (const item of meta) {
|
|
377
|
-
const type = item[0];
|
|
378
|
-
const handler = this.handlers[type];
|
|
379
|
-
if (handler === void 0) {
|
|
380
|
-
throw invalidSerializedData(`type "${type}" is not supported.`);
|
|
381
|
-
}
|
|
382
|
-
const depth = item.length - 1;
|
|
383
|
-
const shared = Math.min(depth, chainDepth);
|
|
384
|
-
let d = 0;
|
|
385
|
-
let original = serialized;
|
|
386
|
-
let currentRef = ref;
|
|
387
|
-
let preSegment = "json";
|
|
388
|
-
if (chain !== void 0 && shared > 0) {
|
|
389
|
-
d = 1;
|
|
390
|
-
while (d < shared && chain[3 * d] === item[d]) {
|
|
391
|
-
d++;
|
|
392
|
-
}
|
|
393
|
-
currentRef = chain[3 * d - 2];
|
|
394
|
-
original = chain[3 * d - 1];
|
|
395
|
-
preSegment = item[d];
|
|
396
|
-
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
397
|
-
throw invalidSerializedData(`segment "${preSegment}" does not exist.`);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
while (d < depth) {
|
|
401
|
-
original = original[preSegment];
|
|
402
|
-
currentRef = walkInto(currentRef, preSegment, original, restored);
|
|
403
|
-
if (chain !== void 0) {
|
|
404
|
-
chain[3 * d] = preSegment;
|
|
405
|
-
chain[3 * d + 1] = currentRef;
|
|
406
|
-
chain[3 * d + 2] = original;
|
|
407
|
-
}
|
|
408
|
-
preSegment = item[++d];
|
|
409
|
-
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
410
|
-
throw invalidSerializedData(`segment "${preSegment}" does not exist.`);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
chainDepth = depth;
|
|
414
|
-
const value = handler.deserialize(currentRef[preSegment]);
|
|
415
|
-
currentRef[preSegment] = value;
|
|
416
|
-
if (isWalkable(value)) {
|
|
417
|
-
(restored ??= /* @__PURE__ */ new Set()).add(value);
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
return ref.json;
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
class RPCSerializer extends RPCJsonSerializer {
|
|
426
|
-
defaultSerializeOptions;
|
|
427
|
-
constructor(options = {}) {
|
|
428
|
-
super(options);
|
|
429
|
-
this.defaultSerializeOptions = options.serialize;
|
|
430
|
-
}
|
|
431
|
-
serialize(data, options = {}) {
|
|
432
|
-
if (data === void 0 || data instanceof ReadableStream || data instanceof Blob) {
|
|
433
|
-
return data;
|
|
434
|
-
}
|
|
435
|
-
if (isAsyncIteratorObject(data)) {
|
|
436
|
-
return wrapAsyncIteratorPreservingEventMeta(data, {
|
|
437
|
-
mapResult: (result) => {
|
|
438
|
-
if (result.value === void 0) {
|
|
439
|
-
return result;
|
|
440
|
-
}
|
|
441
|
-
return { done: result.done, value: this.serializePayload(result.value, false) };
|
|
442
|
-
},
|
|
443
|
-
mapError: (e) => new ErrorEvent(
|
|
444
|
-
this.serializePayload(toORPCError(e).toJSON(), false),
|
|
445
|
-
{ cause: e }
|
|
446
|
-
)
|
|
447
|
-
});
|
|
448
|
-
}
|
|
449
|
-
const useFormDataForBlobs = options.useFormDataForBlobFields ?? this.defaultSerializeOptions?.useFormDataForBlobFields ?? true;
|
|
450
|
-
return this.serializePayload(data, useFormDataForBlobs);
|
|
451
|
-
}
|
|
452
|
-
serializePayload(data, useFormDataForBlobs) {
|
|
453
|
-
const { json, meta, maps, blobs } = this.serializeJson(data);
|
|
454
|
-
if (!useFormDataForBlobs || !blobs?.length) {
|
|
455
|
-
return { json, meta };
|
|
456
|
-
}
|
|
457
|
-
const form = new FormData();
|
|
458
|
-
form.set("data", stringifyJSON({ json, meta, maps }));
|
|
459
|
-
blobs.forEach((blob, i) => {
|
|
460
|
-
form.set(i.toString(), blob);
|
|
461
|
-
});
|
|
462
|
-
return form;
|
|
463
|
-
}
|
|
464
|
-
deserialize(data) {
|
|
465
|
-
if (data === void 0 || data instanceof ReadableStream || data instanceof Blob) {
|
|
466
|
-
return data;
|
|
467
|
-
}
|
|
468
|
-
if (isAsyncIteratorObject(data)) {
|
|
469
|
-
return wrapAsyncIteratorPreservingEventMeta(data, {
|
|
470
|
-
mapResult: (result) => {
|
|
471
|
-
if (result.value === void 0) {
|
|
472
|
-
return result;
|
|
473
|
-
}
|
|
474
|
-
return { done: result.done, value: this.deserializePayload(result.value) };
|
|
475
|
-
},
|
|
476
|
-
mapError: (e) => {
|
|
477
|
-
if (!(e instanceof ErrorEvent)) {
|
|
478
|
-
return e;
|
|
479
|
-
}
|
|
480
|
-
const deserialized = this.deserializePayload(e.data);
|
|
481
|
-
if (isORPCErrorJson(deserialized)) {
|
|
482
|
-
return createORPCErrorFromJson(deserialized, { cause: e });
|
|
483
|
-
}
|
|
484
|
-
return new ErrorEvent(deserialized, { cause: e });
|
|
485
|
-
}
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
return this.deserializePayload(data);
|
|
489
|
-
}
|
|
490
|
-
deserializePayload(data) {
|
|
491
|
-
if (!(data instanceof FormData)) {
|
|
492
|
-
return this.deserializeJson(data);
|
|
493
|
-
}
|
|
494
|
-
const serialized = JSON.parse(data.get("data"));
|
|
495
|
-
const blobs = [];
|
|
496
|
-
for (const [key, value] of data) {
|
|
497
|
-
if (value instanceof Blob) {
|
|
498
|
-
blobs[Number(key)] = value;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
return this.deserializeJson({ ...serialized, blobs });
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
export { RPCSerializer as R, isDefinedError as a, RPCJsonSerializer as b, createORPCErrorFromJson as c, cloneORPCError as d, isORPCErrorJson as i, toORPCError as t, wrapAsyncIteratorPreservingEventMeta as w };
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { Promisable, OrderablePlugin, Interceptor } from '@orpc/shared';
|
|
2
|
-
import { StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
3
|
-
import { C as ClientContext, a as ClientOptions, A as AnyORPCError, b as ClientLink } from './client.BoHJeq15.mjs';
|
|
4
|
-
|
|
5
|
-
type StandardLinkCodecDecodedResponse = {
|
|
6
|
-
kind: 'output';
|
|
7
|
-
output: unknown;
|
|
8
|
-
} | {
|
|
9
|
-
kind: 'error';
|
|
10
|
-
error: AnyORPCError;
|
|
11
|
-
};
|
|
12
|
-
interface StandardLinkCodec<T extends ClientContext> {
|
|
13
|
-
encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promisable<StandardRequest>;
|
|
14
|
-
decodeResponse(response: StandardLazyResponse, path: string[], options: ClientOptions<T>): Promisable<StandardLinkCodecDecodedResponse>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface StandardLinkPlugin<T extends ClientContext> extends OrderablePlugin {
|
|
18
|
-
/**
|
|
19
|
-
* Initializes the plugin and returns new link options.
|
|
20
|
-
* Called once per plugin instance during composition.
|
|
21
|
-
*
|
|
22
|
-
* This method allows plugins to wrap, extend, or transform link options
|
|
23
|
-
* such as interceptors, or configuration.
|
|
24
|
-
*
|
|
25
|
-
* @param options - The current link options from previous plugins or base configuration
|
|
26
|
-
* @returns Transformed link options with plugin's modifications applied
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```ts
|
|
30
|
-
* init(options) {
|
|
31
|
-
* return {
|
|
32
|
-
* ...options,
|
|
33
|
-
* interceptors: [...(options.interceptors || []), myInterceptor]
|
|
34
|
-
* }
|
|
35
|
-
* }
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
init?(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Handles the transport layer for sending requests and receiving responses.
|
|
43
|
-
*
|
|
44
|
-
* Implementations are responsible for the actual network communication,
|
|
45
|
-
* such as HTTP fetch, WebSocket, or other transport mechanisms.
|
|
46
|
-
*/
|
|
47
|
-
interface StandardLinkTransport<T extends ClientContext> {
|
|
48
|
-
/**
|
|
49
|
-
* @throws Transport-level errors (network failures, timeouts, etc.)
|
|
50
|
-
*/
|
|
51
|
-
send(request: StandardRequest, path: string[], options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
55
|
-
path: string[];
|
|
56
|
-
input: unknown;
|
|
57
|
-
}
|
|
58
|
-
type StandardLinkInterceptor<T extends ClientContext> = Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>;
|
|
59
|
-
interface StandardLinkTransportInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
60
|
-
path: string[];
|
|
61
|
-
request: StandardRequest;
|
|
62
|
-
}
|
|
63
|
-
type StandardLinkTransportInterceptor<T extends ClientContext> = Interceptor<StandardLinkTransportInterceptorOptions<T>, Promise<StandardLazyResponse>>;
|
|
64
|
-
interface StandardLinkOptions<T extends ClientContext> {
|
|
65
|
-
/**
|
|
66
|
-
* Interceptors that execute around the entire call, including transport and codec.
|
|
67
|
-
* Useful for error handling, logging, metrics, ...
|
|
68
|
-
*/
|
|
69
|
-
interceptors?: StandardLinkInterceptor<T>[] | undefined;
|
|
70
|
-
/**
|
|
71
|
-
* Interceptors that execute around the transport layer, after encoding and before decoding.
|
|
72
|
-
* Useful for modifying the request or response, adding transport-level logging, ...
|
|
73
|
-
*/
|
|
74
|
-
transportInterceptors?: StandardLinkTransportInterceptor<T>[] | undefined;
|
|
75
|
-
plugins?: StandardLinkPlugin<T>[] | undefined;
|
|
76
|
-
}
|
|
77
|
-
declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
78
|
-
private readonly codec;
|
|
79
|
-
private readonly transport;
|
|
80
|
-
private readonly interceptors;
|
|
81
|
-
private readonly transportInterceptors;
|
|
82
|
-
constructor(codec: StandardLinkCodec<T>, transport: StandardLinkTransport<T>, options?: StandardLinkOptions<T>);
|
|
83
|
-
/**
|
|
84
|
-
* @throws ORPCError, MalformedResponseError, transport-level errors (network failures, timeouts, etc.)
|
|
85
|
-
*/
|
|
86
|
-
call(path: string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export { StandardLink as a };
|
|
90
|
-
export type { StandardLinkTransport as S, StandardLinkOptions as b, StandardLinkPlugin as c, StandardLinkTransportInterceptorOptions as d, StandardLinkInterceptorOptions as e, StandardLinkCodec as f, StandardLinkCodecDecodedResponse as g, StandardLinkInterceptor as h, StandardLinkTransportInterceptor as i };
|