@orpc/client 0.0.0-next.32a6de9 → 0.0.0-next.32bf331
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 +30 -30
- package/dist/adapters/fetch/index.d.mts +31 -11
- package/dist/adapters/fetch/index.d.ts +31 -11
- package/dist/adapters/fetch/index.mjs +27 -13
- package/dist/adapters/message-port/index.d.mts +80 -0
- package/dist/adapters/message-port/index.d.ts +80 -0
- package/dist/adapters/message-port/index.mjs +87 -0
- package/dist/adapters/standard/index.d.mts +6 -5
- package/dist/adapters/standard/index.d.ts +6 -5
- package/dist/adapters/standard/index.mjs +4 -2
- package/dist/adapters/websocket/index.d.mts +29 -0
- package/dist/adapters/websocket/index.d.ts +29 -0
- package/dist/adapters/websocket/index.mjs +47 -0
- package/dist/index.d.mts +98 -23
- package/dist/index.d.ts +98 -23
- package/dist/index.mjs +55 -8
- package/dist/plugins/index.d.mts +151 -14
- package/dist/plugins/index.d.ts +151 -14
- package/dist/plugins/index.mjs +267 -38
- package/dist/shared/{client.C7z5zk4v.d.mts → client.2jUAqzYU.d.ts} +16 -11
- package/dist/shared/client.54Qh2j9q.mjs +171 -0
- package/dist/shared/{client.cs2A0fEV.d.ts → client.B3pNRBih.d.ts} +13 -9
- package/dist/shared/{client.Cev5VIeo.d.mts → client.BFAVy68H.d.mts} +13 -9
- package/dist/shared/client.BLtwTQUg.mjs +40 -0
- package/dist/shared/{client.p3ON_yzu.d.ts → client.CpCa3si8.d.mts} +16 -11
- package/dist/shared/{client.BQuFq0Vi.mjs → client._PIYQrwY.mjs} +98 -32
- package/dist/shared/client.i2uoJbEp.d.mts +83 -0
- package/dist/shared/client.i2uoJbEp.d.ts +83 -0
- package/package.json +18 -8
- package/dist/shared/client.CipPQkhk.d.mts +0 -29
- package/dist/shared/client.CipPQkhk.d.ts +0 -29
- package/dist/shared/client.jKEwIsRd.mjs +0 -175
package/dist/plugins/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { isAsyncIteratorObject, value, splitInHalf, toArray } from '@orpc/shared';
|
|
2
|
-
import { toBatchRequest, parseBatchResponse } from '@orpc/standard-server/batch';
|
|
3
|
-
import { getEventMeta } from '@orpc/standard-server';
|
|
1
|
+
import { isAsyncIteratorObject, defer, value, splitInHalf, toArray, stringifyJSON, overlayProxy, AsyncIteratorClass } from '@orpc/shared';
|
|
2
|
+
import { toBatchRequest, parseBatchResponse, toBatchAbortSignal } from '@orpc/standard-server/batch';
|
|
3
|
+
import { replicateStandardLazyResponse, getEventMeta, flattenHeader } from '@orpc/standard-server';
|
|
4
|
+
import { C as COMMON_ORPC_ERROR_DEFS } from '../shared/client.54Qh2j9q.mjs';
|
|
4
5
|
|
|
5
6
|
class BatchLinkPlugin {
|
|
6
7
|
groups;
|
|
@@ -10,12 +11,15 @@ class BatchLinkPlugin {
|
|
|
10
11
|
batchHeaders;
|
|
11
12
|
mapRequestItem;
|
|
12
13
|
exclude;
|
|
14
|
+
mode;
|
|
13
15
|
pending;
|
|
16
|
+
order = 5e6;
|
|
14
17
|
constructor(options) {
|
|
15
18
|
this.groups = options.groups;
|
|
16
19
|
this.pending = /* @__PURE__ */ new Map();
|
|
17
20
|
this.maxSize = options.maxSize ?? 10;
|
|
18
21
|
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
|
22
|
+
this.mode = options.mode ?? "streaming";
|
|
19
23
|
this.batchUrl = options.url ?? (([options2]) => `${options2.request.url.origin}${options2.request.url.pathname}/__batch__`);
|
|
20
24
|
this.batchHeaders = options.headers ?? (([options2, ...rest]) => {
|
|
21
25
|
const headers = {};
|
|
@@ -61,7 +65,7 @@ class BatchLinkPlugin {
|
|
|
61
65
|
});
|
|
62
66
|
});
|
|
63
67
|
options.clientInterceptors.push((options2) => {
|
|
64
|
-
if (this.exclude(options2) || options2.request.body instanceof Blob || options2.request.body instanceof FormData || isAsyncIteratorObject(options2.request.body)) {
|
|
68
|
+
if (this.exclude(options2) || options2.request.body instanceof Blob || options2.request.body instanceof FormData || isAsyncIteratorObject(options2.request.body) || options2.request.signal?.aborted) {
|
|
65
69
|
return options2.next();
|
|
66
70
|
}
|
|
67
71
|
const group = this.groups.find((group2) => group2.condition(options2));
|
|
@@ -70,7 +74,7 @@ class BatchLinkPlugin {
|
|
|
70
74
|
}
|
|
71
75
|
return new Promise((resolve, reject) => {
|
|
72
76
|
this.#enqueueRequest(group, options2, resolve, reject);
|
|
73
|
-
|
|
77
|
+
defer(() => this.#processPendingBatches());
|
|
74
78
|
});
|
|
75
79
|
});
|
|
76
80
|
}
|
|
@@ -126,16 +130,28 @@ class BatchLinkPlugin {
|
|
|
126
130
|
this.#executeBatch(method, group, second);
|
|
127
131
|
return;
|
|
128
132
|
}
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
133
|
+
const mode = value(this.mode, options);
|
|
134
|
+
try {
|
|
135
|
+
const lazyResponse = await options[0].next({
|
|
136
|
+
request: { ...batchRequest, headers: { ...batchRequest.headers, "x-orpc-batch": mode } },
|
|
137
|
+
signal: batchRequest.signal,
|
|
138
|
+
context: group.context,
|
|
139
|
+
input: group.input,
|
|
140
|
+
path: toArray(group.path)
|
|
141
|
+
});
|
|
142
|
+
const parsed = parseBatchResponse({ ...lazyResponse, body: await lazyResponse.body() });
|
|
143
|
+
for await (const item of parsed) {
|
|
144
|
+
batchItems[item.index]?.[1]({ ...item, body: () => Promise.resolve(item.body) });
|
|
145
|
+
}
|
|
146
|
+
} catch (err) {
|
|
147
|
+
if (batchRequest.signal?.aborted && batchRequest.signal.reason === err) {
|
|
148
|
+
for (const [{ signal }, , reject] of batchItems) {
|
|
149
|
+
if (signal?.aborted) {
|
|
150
|
+
reject(signal.reason);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
throw err;
|
|
139
155
|
}
|
|
140
156
|
throw new Error("Something went wrong make batch response not contains enough responses. This can be a bug please report it.");
|
|
141
157
|
} catch (error) {
|
|
@@ -146,6 +162,101 @@ class BatchLinkPlugin {
|
|
|
146
162
|
}
|
|
147
163
|
}
|
|
148
164
|
|
|
165
|
+
class DedupeRequestsPlugin {
|
|
166
|
+
#groups;
|
|
167
|
+
#filter;
|
|
168
|
+
order = 4e6;
|
|
169
|
+
// make sure execute before batch plugin
|
|
170
|
+
#queue = /* @__PURE__ */ new Map();
|
|
171
|
+
constructor(options) {
|
|
172
|
+
this.#groups = options.groups;
|
|
173
|
+
this.#filter = options.filter ?? (({ request }) => request.method === "GET");
|
|
174
|
+
}
|
|
175
|
+
init(options) {
|
|
176
|
+
options.clientInterceptors ??= [];
|
|
177
|
+
options.clientInterceptors.push((options2) => {
|
|
178
|
+
if (options2.request.body instanceof Blob || options2.request.body instanceof FormData || options2.request.body instanceof URLSearchParams || isAsyncIteratorObject(options2.request.body) || !this.#filter(options2)) {
|
|
179
|
+
return options2.next();
|
|
180
|
+
}
|
|
181
|
+
const group = this.#groups.find((group2) => group2.condition(options2));
|
|
182
|
+
if (!group) {
|
|
183
|
+
return options2.next();
|
|
184
|
+
}
|
|
185
|
+
return new Promise((resolve, reject) => {
|
|
186
|
+
this.#enqueue(group, options2, resolve, reject);
|
|
187
|
+
defer(() => this.#dequeue());
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
#enqueue(group, options, resolve, reject) {
|
|
192
|
+
let queue = this.#queue.get(group);
|
|
193
|
+
if (!queue) {
|
|
194
|
+
this.#queue.set(group, queue = []);
|
|
195
|
+
}
|
|
196
|
+
const matched = queue.find((item) => {
|
|
197
|
+
const requestString1 = stringifyJSON({
|
|
198
|
+
body: item.options.request.body,
|
|
199
|
+
headers: item.options.request.headers,
|
|
200
|
+
method: item.options.request.method,
|
|
201
|
+
url: item.options.request.url
|
|
202
|
+
});
|
|
203
|
+
const requestString2 = stringifyJSON({
|
|
204
|
+
body: options.request.body,
|
|
205
|
+
headers: options.request.headers,
|
|
206
|
+
method: options.request.method,
|
|
207
|
+
url: options.request.url
|
|
208
|
+
});
|
|
209
|
+
return requestString1 === requestString2;
|
|
210
|
+
});
|
|
211
|
+
if (matched) {
|
|
212
|
+
matched.signals.push(options.request.signal);
|
|
213
|
+
matched.resolves.push(resolve);
|
|
214
|
+
matched.rejects.push(reject);
|
|
215
|
+
} else {
|
|
216
|
+
queue.push({
|
|
217
|
+
options,
|
|
218
|
+
signals: [options.request.signal],
|
|
219
|
+
resolves: [resolve],
|
|
220
|
+
rejects: [reject]
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async #dequeue() {
|
|
225
|
+
const promises = [];
|
|
226
|
+
for (const [group, items] of this.#queue) {
|
|
227
|
+
for (const { options, signals, resolves, rejects } of items) {
|
|
228
|
+
promises.push(
|
|
229
|
+
this.#execute(group, options, signals, resolves, rejects)
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
this.#queue.clear();
|
|
234
|
+
await Promise.all(promises);
|
|
235
|
+
}
|
|
236
|
+
async #execute(group, options, signals, resolves, rejects) {
|
|
237
|
+
try {
|
|
238
|
+
const dedupedRequest = {
|
|
239
|
+
...options.request,
|
|
240
|
+
signal: toBatchAbortSignal(signals)
|
|
241
|
+
};
|
|
242
|
+
const response = await options.next({
|
|
243
|
+
...options,
|
|
244
|
+
request: dedupedRequest,
|
|
245
|
+
signal: dedupedRequest.signal,
|
|
246
|
+
context: group.context
|
|
247
|
+
});
|
|
248
|
+
const replicatedResponses = replicateStandardLazyResponse(response, resolves.length);
|
|
249
|
+
for (const resolve of resolves) {
|
|
250
|
+
resolve(replicatedResponses.shift());
|
|
251
|
+
}
|
|
252
|
+
} catch (error) {
|
|
253
|
+
for (const reject of rejects) {
|
|
254
|
+
reject(error);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
149
260
|
class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error {
|
|
150
261
|
}
|
|
151
262
|
class ClientRetryPlugin {
|
|
@@ -153,6 +264,7 @@ class ClientRetryPlugin {
|
|
|
153
264
|
defaultRetryDelay;
|
|
154
265
|
defaultShouldRetry;
|
|
155
266
|
defaultOnRetry;
|
|
267
|
+
order = 18e5;
|
|
156
268
|
constructor(options = {}) {
|
|
157
269
|
this.defaultRetry = options.default?.retry ?? 0;
|
|
158
270
|
this.defaultRetryDelay = options.default?.retryDelay ?? ((o) => o.lastEventRetry ?? 2e3);
|
|
@@ -174,20 +286,20 @@ class ClientRetryPlugin {
|
|
|
174
286
|
}
|
|
175
287
|
let lastEventId = interceptorOptions.lastEventId;
|
|
176
288
|
let lastEventRetry;
|
|
177
|
-
let
|
|
289
|
+
let callback;
|
|
178
290
|
let attemptIndex = 0;
|
|
179
|
-
const next = async (
|
|
180
|
-
let
|
|
291
|
+
const next = async (initialError) => {
|
|
292
|
+
let currentError = initialError;
|
|
181
293
|
while (true) {
|
|
182
294
|
const updatedInterceptorOptions = { ...interceptorOptions, lastEventId };
|
|
183
|
-
if (
|
|
295
|
+
if (currentError) {
|
|
184
296
|
if (attemptIndex >= maxAttempts) {
|
|
185
|
-
throw
|
|
297
|
+
throw currentError.error;
|
|
186
298
|
}
|
|
187
299
|
const attemptOptions = {
|
|
188
300
|
...updatedInterceptorOptions,
|
|
189
301
|
attemptIndex,
|
|
190
|
-
error:
|
|
302
|
+
error: currentError.error,
|
|
191
303
|
lastEventRetry
|
|
192
304
|
};
|
|
193
305
|
const shouldRetryBool = await value(
|
|
@@ -195,23 +307,24 @@ class ClientRetryPlugin {
|
|
|
195
307
|
attemptOptions
|
|
196
308
|
);
|
|
197
309
|
if (!shouldRetryBool) {
|
|
198
|
-
throw
|
|
310
|
+
throw currentError.error;
|
|
199
311
|
}
|
|
200
|
-
|
|
312
|
+
callback = onRetry?.(attemptOptions);
|
|
201
313
|
const retryDelayMs = await value(retryDelay, attemptOptions);
|
|
202
314
|
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
|
|
203
315
|
attemptIndex++;
|
|
204
316
|
}
|
|
205
317
|
try {
|
|
318
|
+
currentError = void 0;
|
|
206
319
|
return await interceptorOptions.next(updatedInterceptorOptions);
|
|
207
320
|
} catch (error) {
|
|
208
|
-
|
|
321
|
+
currentError = { error };
|
|
322
|
+
if (updatedInterceptorOptions.signal?.aborted) {
|
|
209
323
|
throw error;
|
|
210
324
|
}
|
|
211
|
-
current = { error };
|
|
212
325
|
} finally {
|
|
213
|
-
|
|
214
|
-
|
|
326
|
+
callback?.(!currentError);
|
|
327
|
+
callback = void 0;
|
|
215
328
|
}
|
|
216
329
|
}
|
|
217
330
|
};
|
|
@@ -219,19 +332,17 @@ class ClientRetryPlugin {
|
|
|
219
332
|
if (!isAsyncIteratorObject(output)) {
|
|
220
333
|
return output;
|
|
221
334
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
335
|
+
let current = output;
|
|
336
|
+
let isIteratorAborted = false;
|
|
337
|
+
return overlayProxy(() => current, new AsyncIteratorClass(
|
|
338
|
+
async () => {
|
|
225
339
|
while (true) {
|
|
226
340
|
try {
|
|
227
341
|
const item = await current.next();
|
|
228
342
|
const meta = getEventMeta(item.value);
|
|
229
343
|
lastEventId = meta?.id ?? lastEventId;
|
|
230
344
|
lastEventRetry = meta?.retry ?? lastEventRetry;
|
|
231
|
-
|
|
232
|
-
return item.value;
|
|
233
|
-
}
|
|
234
|
-
yield item.value;
|
|
345
|
+
return item;
|
|
235
346
|
} catch (error) {
|
|
236
347
|
const meta = getEventMeta(error);
|
|
237
348
|
lastEventId = meta?.id ?? lastEventId;
|
|
@@ -243,14 +354,132 @@ class ClientRetryPlugin {
|
|
|
243
354
|
);
|
|
244
355
|
}
|
|
245
356
|
current = maybeEventIterator;
|
|
357
|
+
if (isIteratorAborted) {
|
|
358
|
+
await current.return?.();
|
|
359
|
+
throw error;
|
|
360
|
+
}
|
|
246
361
|
}
|
|
247
362
|
}
|
|
248
|
-
}
|
|
249
|
-
|
|
363
|
+
},
|
|
364
|
+
async (reason) => {
|
|
365
|
+
isIteratorAborted = true;
|
|
366
|
+
if (reason !== "next") {
|
|
367
|
+
await current.return?.();
|
|
368
|
+
}
|
|
250
369
|
}
|
|
251
|
-
|
|
370
|
+
));
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
class RetryAfterPlugin {
|
|
376
|
+
condition;
|
|
377
|
+
maxAttempts;
|
|
378
|
+
timeout;
|
|
379
|
+
order = 19e5;
|
|
380
|
+
constructor(options = {}) {
|
|
381
|
+
this.condition = options.condition ?? ((response) => response.status === COMMON_ORPC_ERROR_DEFS.TOO_MANY_REQUESTS.status || response.status === COMMON_ORPC_ERROR_DEFS.SERVICE_UNAVAILABLE.status);
|
|
382
|
+
this.maxAttempts = options.maxAttempts ?? 3;
|
|
383
|
+
this.timeout = options.timeout ?? 5 * 60 * 1e3;
|
|
384
|
+
}
|
|
385
|
+
init(options) {
|
|
386
|
+
options.clientInterceptors ??= [];
|
|
387
|
+
options.clientInterceptors.push(async (interceptorOptions) => {
|
|
388
|
+
const startTime = Date.now();
|
|
389
|
+
let attemptCount = 0;
|
|
390
|
+
while (true) {
|
|
391
|
+
attemptCount++;
|
|
392
|
+
const response = await interceptorOptions.next();
|
|
393
|
+
if (!value(this.condition, response, interceptorOptions)) {
|
|
394
|
+
return response;
|
|
395
|
+
}
|
|
396
|
+
const retryAfterHeader = flattenHeader(response.headers["retry-after"]);
|
|
397
|
+
const retryAfterMs = this.parseRetryAfterHeader(retryAfterHeader);
|
|
398
|
+
if (retryAfterMs === void 0) {
|
|
399
|
+
return response;
|
|
400
|
+
}
|
|
401
|
+
if (attemptCount >= value(this.maxAttempts, response, interceptorOptions)) {
|
|
402
|
+
return response;
|
|
403
|
+
}
|
|
404
|
+
const timeoutMs = value(this.timeout, response, interceptorOptions);
|
|
405
|
+
const elapsedTime = Date.now() - startTime;
|
|
406
|
+
if (elapsedTime + retryAfterMs > timeoutMs) {
|
|
407
|
+
return response;
|
|
408
|
+
}
|
|
409
|
+
await this.delayExecution(retryAfterMs, interceptorOptions.signal);
|
|
410
|
+
if (interceptorOptions.signal?.aborted) {
|
|
411
|
+
return response;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
parseRetryAfterHeader(value2) {
|
|
417
|
+
value2 = value2?.trim();
|
|
418
|
+
if (!value2) {
|
|
419
|
+
return void 0;
|
|
420
|
+
}
|
|
421
|
+
const seconds = Number(value2);
|
|
422
|
+
if (Number.isFinite(seconds)) {
|
|
423
|
+
return Math.max(0, seconds * 1e3);
|
|
424
|
+
}
|
|
425
|
+
const retryDate = Date.parse(value2);
|
|
426
|
+
if (!Number.isNaN(retryDate)) {
|
|
427
|
+
return Math.max(0, retryDate - Date.now());
|
|
428
|
+
}
|
|
429
|
+
return void 0;
|
|
430
|
+
}
|
|
431
|
+
delayExecution(ms, signal) {
|
|
432
|
+
return new Promise((resolve) => {
|
|
433
|
+
if (signal?.aborted) {
|
|
434
|
+
resolve();
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
let timeout;
|
|
438
|
+
const onAbort = () => {
|
|
439
|
+
clearTimeout(timeout);
|
|
440
|
+
timeout = void 0;
|
|
441
|
+
resolve();
|
|
442
|
+
};
|
|
443
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
444
|
+
timeout = setTimeout(() => {
|
|
445
|
+
signal?.removeEventListener("abort", onAbort);
|
|
446
|
+
resolve();
|
|
447
|
+
}, ms);
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
class SimpleCsrfProtectionLinkPlugin {
|
|
453
|
+
headerName;
|
|
454
|
+
headerValue;
|
|
455
|
+
exclude;
|
|
456
|
+
constructor(options = {}) {
|
|
457
|
+
this.headerName = options.headerName ?? "x-csrf-token";
|
|
458
|
+
this.headerValue = options.headerValue ?? "orpc";
|
|
459
|
+
this.exclude = options.exclude ?? false;
|
|
460
|
+
}
|
|
461
|
+
order = 8e6;
|
|
462
|
+
init(options) {
|
|
463
|
+
options.clientInterceptors ??= [];
|
|
464
|
+
options.clientInterceptors.push(async (options2) => {
|
|
465
|
+
const excluded = await value(this.exclude, options2);
|
|
466
|
+
if (excluded) {
|
|
467
|
+
return options2.next();
|
|
468
|
+
}
|
|
469
|
+
const headerName = await value(this.headerName, options2);
|
|
470
|
+
const headerValue = await value(this.headerValue, options2);
|
|
471
|
+
return options2.next({
|
|
472
|
+
...options2,
|
|
473
|
+
request: {
|
|
474
|
+
...options2.request,
|
|
475
|
+
headers: {
|
|
476
|
+
...options2.request.headers,
|
|
477
|
+
[headerName]: headerValue
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
});
|
|
252
481
|
});
|
|
253
482
|
}
|
|
254
483
|
}
|
|
255
484
|
|
|
256
|
-
export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse };
|
|
485
|
+
export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, RetryAfterPlugin, SimpleCsrfProtectionLinkPlugin };
|
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
import { Interceptor
|
|
1
|
+
import { Interceptor } from '@orpc/shared';
|
|
2
2
|
import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
3
|
-
import {
|
|
3
|
+
import { b as ClientContext, c as ClientOptions, C as ClientLink } from './client.i2uoJbEp.js';
|
|
4
|
+
|
|
5
|
+
interface StandardLinkPlugin<T extends ClientContext> {
|
|
6
|
+
order?: number;
|
|
7
|
+
init?(options: StandardLinkOptions<T>): void;
|
|
8
|
+
}
|
|
9
|
+
declare class CompositeStandardLinkPlugin<T extends ClientContext, TPlugin extends StandardLinkPlugin<T>> implements StandardLinkPlugin<T> {
|
|
10
|
+
protected readonly plugins: TPlugin[];
|
|
11
|
+
constructor(plugins?: readonly TPlugin[]);
|
|
12
|
+
init(options: StandardLinkOptions<T>): void;
|
|
13
|
+
}
|
|
4
14
|
|
|
5
15
|
interface StandardLinkCodec<T extends ClientContext> {
|
|
6
16
|
encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
|
|
@@ -10,11 +20,6 @@ interface StandardLinkClient<T extends ClientContext> {
|
|
|
10
20
|
call(request: StandardRequest, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
|
|
11
21
|
}
|
|
12
22
|
|
|
13
|
-
declare class InvalidEventIteratorRetryResponse extends Error {
|
|
14
|
-
}
|
|
15
|
-
interface StandardLinkPlugin<T extends ClientContext> {
|
|
16
|
-
init?(options: StandardLinkOptions<T>): void;
|
|
17
|
-
}
|
|
18
23
|
interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
19
24
|
path: readonly string[];
|
|
20
25
|
input: unknown;
|
|
@@ -23,12 +28,11 @@ interface StandardLinkClientInterceptorOptions<T extends ClientContext> extends
|
|
|
23
28
|
request: StandardRequest;
|
|
24
29
|
}
|
|
25
30
|
interface StandardLinkOptions<T extends ClientContext> {
|
|
26
|
-
interceptors?: Interceptor<StandardLinkInterceptorOptions<T>, unknown
|
|
27
|
-
clientInterceptors?: Interceptor<StandardLinkClientInterceptorOptions<T>, StandardLazyResponse
|
|
31
|
+
interceptors?: Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>[];
|
|
32
|
+
clientInterceptors?: Interceptor<StandardLinkClientInterceptorOptions<T>, Promise<StandardLazyResponse>>[];
|
|
28
33
|
plugins?: StandardLinkPlugin<T>[];
|
|
29
34
|
}
|
|
30
35
|
declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
31
|
-
#private;
|
|
32
36
|
readonly codec: StandardLinkCodec<T>;
|
|
33
37
|
readonly sender: StandardLinkClient<T>;
|
|
34
38
|
private readonly interceptors;
|
|
@@ -37,4 +41,5 @@ declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
|
37
41
|
call(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
|
|
38
42
|
}
|
|
39
43
|
|
|
40
|
-
export {
|
|
44
|
+
export { CompositeStandardLinkPlugin as C, StandardLink as d };
|
|
45
|
+
export type { StandardLinkClientInterceptorOptions as S, StandardLinkPlugin as a, StandardLinkOptions as b, StandardLinkInterceptorOptions as c, StandardLinkCodec as e, StandardLinkClient as f };
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { resolveMaybeOptionalOptions, getConstructor, isObject } from '@orpc/shared';
|
|
2
|
+
|
|
3
|
+
const ORPC_CLIENT_PACKAGE_NAME = "@orpc/client";
|
|
4
|
+
const ORPC_CLIENT_PACKAGE_VERSION = "0.0.0-next.32bf331";
|
|
5
|
+
|
|
6
|
+
const COMMON_ORPC_ERROR_DEFS = {
|
|
7
|
+
BAD_REQUEST: {
|
|
8
|
+
status: 400,
|
|
9
|
+
message: "Bad Request"
|
|
10
|
+
},
|
|
11
|
+
UNAUTHORIZED: {
|
|
12
|
+
status: 401,
|
|
13
|
+
message: "Unauthorized"
|
|
14
|
+
},
|
|
15
|
+
FORBIDDEN: {
|
|
16
|
+
status: 403,
|
|
17
|
+
message: "Forbidden"
|
|
18
|
+
},
|
|
19
|
+
NOT_FOUND: {
|
|
20
|
+
status: 404,
|
|
21
|
+
message: "Not Found"
|
|
22
|
+
},
|
|
23
|
+
METHOD_NOT_SUPPORTED: {
|
|
24
|
+
status: 405,
|
|
25
|
+
message: "Method Not Supported"
|
|
26
|
+
},
|
|
27
|
+
NOT_ACCEPTABLE: {
|
|
28
|
+
status: 406,
|
|
29
|
+
message: "Not Acceptable"
|
|
30
|
+
},
|
|
31
|
+
TIMEOUT: {
|
|
32
|
+
status: 408,
|
|
33
|
+
message: "Request Timeout"
|
|
34
|
+
},
|
|
35
|
+
CONFLICT: {
|
|
36
|
+
status: 409,
|
|
37
|
+
message: "Conflict"
|
|
38
|
+
},
|
|
39
|
+
PRECONDITION_FAILED: {
|
|
40
|
+
status: 412,
|
|
41
|
+
message: "Precondition Failed"
|
|
42
|
+
},
|
|
43
|
+
PAYLOAD_TOO_LARGE: {
|
|
44
|
+
status: 413,
|
|
45
|
+
message: "Payload Too Large"
|
|
46
|
+
},
|
|
47
|
+
UNSUPPORTED_MEDIA_TYPE: {
|
|
48
|
+
status: 415,
|
|
49
|
+
message: "Unsupported Media Type"
|
|
50
|
+
},
|
|
51
|
+
UNPROCESSABLE_CONTENT: {
|
|
52
|
+
status: 422,
|
|
53
|
+
message: "Unprocessable Content"
|
|
54
|
+
},
|
|
55
|
+
TOO_MANY_REQUESTS: {
|
|
56
|
+
status: 429,
|
|
57
|
+
message: "Too Many Requests"
|
|
58
|
+
},
|
|
59
|
+
CLIENT_CLOSED_REQUEST: {
|
|
60
|
+
status: 499,
|
|
61
|
+
message: "Client Closed Request"
|
|
62
|
+
},
|
|
63
|
+
INTERNAL_SERVER_ERROR: {
|
|
64
|
+
status: 500,
|
|
65
|
+
message: "Internal Server Error"
|
|
66
|
+
},
|
|
67
|
+
NOT_IMPLEMENTED: {
|
|
68
|
+
status: 501,
|
|
69
|
+
message: "Not Implemented"
|
|
70
|
+
},
|
|
71
|
+
BAD_GATEWAY: {
|
|
72
|
+
status: 502,
|
|
73
|
+
message: "Bad Gateway"
|
|
74
|
+
},
|
|
75
|
+
SERVICE_UNAVAILABLE: {
|
|
76
|
+
status: 503,
|
|
77
|
+
message: "Service Unavailable"
|
|
78
|
+
},
|
|
79
|
+
GATEWAY_TIMEOUT: {
|
|
80
|
+
status: 504,
|
|
81
|
+
message: "Gateway Timeout"
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
function fallbackORPCErrorStatus(code, status) {
|
|
85
|
+
return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
|
|
86
|
+
}
|
|
87
|
+
function fallbackORPCErrorMessage(code, message) {
|
|
88
|
+
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
|
89
|
+
}
|
|
90
|
+
const GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL = Symbol.for(`__${ORPC_CLIENT_PACKAGE_NAME}@${ORPC_CLIENT_PACKAGE_VERSION}/error/ORPC_ERROR_CONSTRUCTORS__`);
|
|
91
|
+
void (globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL] ??= /* @__PURE__ */ new WeakSet());
|
|
92
|
+
const globalORPCErrorConstructors = globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL];
|
|
93
|
+
class ORPCError extends Error {
|
|
94
|
+
defined;
|
|
95
|
+
code;
|
|
96
|
+
status;
|
|
97
|
+
data;
|
|
98
|
+
constructor(code, ...rest) {
|
|
99
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
100
|
+
if (options.status !== void 0 && !isORPCErrorStatus(options.status)) {
|
|
101
|
+
throw new Error("[ORPCError] Invalid error status code.");
|
|
102
|
+
}
|
|
103
|
+
const message = fallbackORPCErrorMessage(code, options.message);
|
|
104
|
+
super(message, options);
|
|
105
|
+
this.code = code;
|
|
106
|
+
this.status = fallbackORPCErrorStatus(code, options.status);
|
|
107
|
+
this.defined = options.defined ?? false;
|
|
108
|
+
this.data = options.data;
|
|
109
|
+
}
|
|
110
|
+
toJSON() {
|
|
111
|
+
return {
|
|
112
|
+
defined: this.defined,
|
|
113
|
+
code: this.code,
|
|
114
|
+
status: this.status,
|
|
115
|
+
message: this.message,
|
|
116
|
+
data: this.data
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Workaround for Next.js where different contexts use separate
|
|
121
|
+
* dependency graphs, causing multiple ORPCError constructors existing and breaking
|
|
122
|
+
* `instanceof` checks across contexts.
|
|
123
|
+
*
|
|
124
|
+
* This is particularly problematic with "Optimized SSR", where orpc-client
|
|
125
|
+
* executes in one context but is invoked from another. When an error is thrown
|
|
126
|
+
* in the execution context, `instanceof ORPCError` checks fail in the
|
|
127
|
+
* invocation context due to separate class constructors.
|
|
128
|
+
*
|
|
129
|
+
* @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
|
|
130
|
+
*/
|
|
131
|
+
static [Symbol.hasInstance](instance) {
|
|
132
|
+
if (globalORPCErrorConstructors.has(this)) {
|
|
133
|
+
const constructor = getConstructor(instance);
|
|
134
|
+
if (constructor && globalORPCErrorConstructors.has(constructor)) {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return super[Symbol.hasInstance](instance);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
globalORPCErrorConstructors.add(ORPCError);
|
|
142
|
+
function isDefinedError(error) {
|
|
143
|
+
return error instanceof ORPCError && error.defined;
|
|
144
|
+
}
|
|
145
|
+
function toORPCError(error) {
|
|
146
|
+
return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
|
|
147
|
+
message: "Internal server error",
|
|
148
|
+
cause: error
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
function isORPCErrorStatus(status) {
|
|
152
|
+
return status < 200 || status >= 400;
|
|
153
|
+
}
|
|
154
|
+
function isORPCErrorJson(json) {
|
|
155
|
+
if (!isObject(json)) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
const validKeys = ["defined", "code", "status", "message", "data"];
|
|
159
|
+
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && isORPCErrorStatus(json.status) && "message" in json && typeof json.message === "string";
|
|
163
|
+
}
|
|
164
|
+
function createORPCErrorFromJson(json, options = {}) {
|
|
165
|
+
return new ORPCError(json.code, {
|
|
166
|
+
...options,
|
|
167
|
+
...json
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export { COMMON_ORPC_ERROR_DEFS as C, ORPC_CLIENT_PACKAGE_NAME as O, ORPC_CLIENT_PACKAGE_VERSION as a, fallbackORPCErrorMessage as b, ORPCError as c, isORPCErrorStatus as d, isORPCErrorJson as e, fallbackORPCErrorStatus as f, createORPCErrorFromJson as g, isDefinedError as i, toORPCError as t };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Segment, Value } from '@orpc/shared';
|
|
1
|
+
import { b as ClientContext, c as ClientOptions, f as HTTPMethod } from './client.i2uoJbEp.js';
|
|
2
|
+
import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.2jUAqzYU.js';
|
|
3
|
+
import { Segment, Value, Promisable } from '@orpc/shared';
|
|
4
4
|
import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
5
5
|
|
|
6
6
|
declare const STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES: {
|
|
@@ -44,30 +44,30 @@ interface StandardRPCLinkCodecOptions<T extends ClientContext> {
|
|
|
44
44
|
/**
|
|
45
45
|
* Base url for all requests.
|
|
46
46
|
*/
|
|
47
|
-
url: Value<string | URL
|
|
47
|
+
url: Value<Promisable<string | URL>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
48
48
|
/**
|
|
49
49
|
* The maximum length of the URL.
|
|
50
50
|
*
|
|
51
51
|
* @default 2083
|
|
52
52
|
*/
|
|
53
|
-
maxUrlLength?: Value<number
|
|
53
|
+
maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
54
54
|
/**
|
|
55
55
|
* The method used to make the request.
|
|
56
56
|
*
|
|
57
57
|
* @default 'POST'
|
|
58
58
|
*/
|
|
59
|
-
method?: Value<HTTPMethod, [
|
|
59
|
+
method?: Value<Promisable<Exclude<HTTPMethod, 'HEAD'>>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
60
60
|
/**
|
|
61
61
|
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
62
62
|
* GET is not allowed, it's very dangerous.
|
|
63
63
|
*
|
|
64
64
|
* @default 'POST'
|
|
65
65
|
*/
|
|
66
|
-
fallbackMethod?: Exclude<HTTPMethod, 'GET'>;
|
|
66
|
+
fallbackMethod?: Exclude<HTTPMethod, 'HEAD' | 'GET'>;
|
|
67
67
|
/**
|
|
68
68
|
* Inject headers to the request.
|
|
69
69
|
*/
|
|
70
|
-
headers?: Value<StandardHeaders
|
|
70
|
+
headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
71
71
|
}
|
|
72
72
|
declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
73
73
|
private readonly serializer;
|
|
@@ -83,5 +83,9 @@ declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardL
|
|
|
83
83
|
|
|
84
84
|
interface StandardRPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardRPCLinkCodecOptions<T>, StandardRPCJsonSerializerOptions {
|
|
85
85
|
}
|
|
86
|
+
declare class StandardRPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
87
|
+
constructor(linkClient: StandardLinkClient<T>, options: StandardRPCLinkOptions<T>);
|
|
88
|
+
}
|
|
86
89
|
|
|
87
|
-
export { STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as S,
|
|
90
|
+
export { STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as S, StandardRPCJsonSerializer as e, StandardRPCLink as g, StandardRPCLinkCodec as i, StandardRPCSerializer as j };
|
|
91
|
+
export type { StandardRPCJsonSerializedMetaItem as a, StandardRPCJsonSerialized as b, StandardRPCCustomJsonSerializer as c, StandardRPCJsonSerializerOptions as d, StandardRPCLinkOptions as f, StandardRPCLinkCodecOptions as h };
|