@orpc/client 0.0.0-next.783ae4e → 0.0.0-next.7844b89
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 +25 -25
- 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 +7 -5
- package/dist/adapters/standard/index.d.ts +7 -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 +105 -27
- package/dist/index.d.ts +105 -27
- package/dist/index.mjs +61 -14
- package/dist/plugins/index.d.mts +209 -21
- package/dist/plugins/index.d.ts +209 -21
- package/dist/plugins/index.mjs +412 -53
- package/dist/shared/{client.D9lmRwGB.d.mts → client.2jUAqzYU.d.ts} +20 -14
- package/dist/shared/{client.CZFIVTQj.d.ts → client.B3pNRBih.d.ts} +13 -25
- package/dist/shared/{client.BC0T26HA.d.mts → client.BFAVy68H.d.mts} +13 -25
- package/dist/shared/client.BLtwTQUg.mjs +40 -0
- package/dist/shared/{client.BaocqKnn.d.ts → client.CpCa3si8.d.mts} +20 -14
- package/dist/shared/client.DUAcviHe.mjs +171 -0
- package/dist/shared/{client.D77bw0hs.mjs → client.DX26RjQn.mjs} +95 -30
- package/dist/shared/client.i2uoJbEp.d.mts +83 -0
- package/dist/shared/client.i2uoJbEp.d.ts +83 -0
- package/package.json +17 -6
- package/dist/shared/client.87WXDX8t.d.mts +0 -32
- package/dist/shared/client.87WXDX8t.d.ts +0 -32
- package/dist/shared/client.jKEwIsRd.mjs +0 -175
package/dist/plugins/index.mjs
CHANGED
|
@@ -1,5 +1,261 @@
|
|
|
1
|
-
import { isAsyncIteratorObject, value } from '@orpc/shared';
|
|
2
|
-
import {
|
|
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.DUAcviHe.mjs';
|
|
5
|
+
|
|
6
|
+
class BatchLinkPlugin {
|
|
7
|
+
groups;
|
|
8
|
+
maxSize;
|
|
9
|
+
batchUrl;
|
|
10
|
+
maxUrlLength;
|
|
11
|
+
batchHeaders;
|
|
12
|
+
mapRequestItem;
|
|
13
|
+
exclude;
|
|
14
|
+
mode;
|
|
15
|
+
pending;
|
|
16
|
+
order = 5e6;
|
|
17
|
+
constructor(options) {
|
|
18
|
+
this.groups = options.groups;
|
|
19
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
20
|
+
this.maxSize = options.maxSize ?? 10;
|
|
21
|
+
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
|
22
|
+
this.mode = options.mode ?? "streaming";
|
|
23
|
+
this.batchUrl = options.url ?? (([options2]) => `${options2.request.url.origin}${options2.request.url.pathname}/__batch__`);
|
|
24
|
+
this.batchHeaders = options.headers ?? (([options2, ...rest]) => {
|
|
25
|
+
const headers = {};
|
|
26
|
+
for (const [key, value2] of Object.entries(options2.request.headers)) {
|
|
27
|
+
if (rest.every((item) => item.request.headers[key] === value2)) {
|
|
28
|
+
headers[key] = value2;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return headers;
|
|
32
|
+
});
|
|
33
|
+
this.mapRequestItem = options.mapRequestItem ?? (({ request, batchHeaders }) => {
|
|
34
|
+
const headers = {};
|
|
35
|
+
for (const [key, value2] of Object.entries(request.headers)) {
|
|
36
|
+
if (batchHeaders[key] !== value2) {
|
|
37
|
+
headers[key] = value2;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
method: request.method,
|
|
42
|
+
url: request.url,
|
|
43
|
+
headers,
|
|
44
|
+
body: request.body,
|
|
45
|
+
signal: request.signal
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
this.exclude = options.exclude ?? (() => false);
|
|
49
|
+
}
|
|
50
|
+
init(options) {
|
|
51
|
+
options.clientInterceptors ??= [];
|
|
52
|
+
options.clientInterceptors.push((options2) => {
|
|
53
|
+
if (options2.request.headers["x-orpc-batch"] !== "1") {
|
|
54
|
+
return options2.next();
|
|
55
|
+
}
|
|
56
|
+
return options2.next({
|
|
57
|
+
...options2,
|
|
58
|
+
request: {
|
|
59
|
+
...options2.request,
|
|
60
|
+
headers: {
|
|
61
|
+
...options2.request.headers,
|
|
62
|
+
"x-orpc-batch": void 0
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
options.clientInterceptors.push((options2) => {
|
|
68
|
+
if (this.exclude(options2) || options2.request.body instanceof Blob || options2.request.body instanceof FormData || isAsyncIteratorObject(options2.request.body) || options2.request.signal?.aborted) {
|
|
69
|
+
return options2.next();
|
|
70
|
+
}
|
|
71
|
+
const group = this.groups.find((group2) => group2.condition(options2));
|
|
72
|
+
if (!group) {
|
|
73
|
+
return options2.next();
|
|
74
|
+
}
|
|
75
|
+
return new Promise((resolve, reject) => {
|
|
76
|
+
this.#enqueueRequest(group, options2, resolve, reject);
|
|
77
|
+
defer(() => this.#processPendingBatches());
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
#enqueueRequest(group, options, resolve, reject) {
|
|
82
|
+
const items = this.pending.get(group);
|
|
83
|
+
if (items) {
|
|
84
|
+
items.push([options, resolve, reject]);
|
|
85
|
+
} else {
|
|
86
|
+
this.pending.set(group, [[options, resolve, reject]]);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async #processPendingBatches() {
|
|
90
|
+
const pending = this.pending;
|
|
91
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
92
|
+
for (const [group, items] of pending) {
|
|
93
|
+
const getItems = items.filter(([options]) => options.request.method === "GET");
|
|
94
|
+
const restItems = items.filter(([options]) => options.request.method !== "GET");
|
|
95
|
+
this.#executeBatch("GET", group, getItems);
|
|
96
|
+
this.#executeBatch("POST", group, restItems);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async #executeBatch(method, group, groupItems) {
|
|
100
|
+
if (!groupItems.length) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const batchItems = groupItems;
|
|
104
|
+
if (batchItems.length === 1) {
|
|
105
|
+
batchItems[0][0].next().then(batchItems[0][1]).catch(batchItems[0][2]);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
const options = batchItems.map(([options2]) => options2);
|
|
110
|
+
const maxSize = await value(this.maxSize, options);
|
|
111
|
+
if (batchItems.length > maxSize) {
|
|
112
|
+
const [first, second] = splitInHalf(batchItems);
|
|
113
|
+
this.#executeBatch(method, group, first);
|
|
114
|
+
this.#executeBatch(method, group, second);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const batchUrl = new URL(await value(this.batchUrl, options));
|
|
118
|
+
const batchHeaders = await value(this.batchHeaders, options);
|
|
119
|
+
const mappedItems = batchItems.map(([options2]) => this.mapRequestItem({ ...options2, batchUrl, batchHeaders }));
|
|
120
|
+
const batchRequest = toBatchRequest({
|
|
121
|
+
method,
|
|
122
|
+
url: batchUrl,
|
|
123
|
+
headers: batchHeaders,
|
|
124
|
+
requests: mappedItems
|
|
125
|
+
});
|
|
126
|
+
const maxUrlLength = await value(this.maxUrlLength, options);
|
|
127
|
+
if (batchRequest.url.toString().length > maxUrlLength) {
|
|
128
|
+
const [first, second] = splitInHalf(batchItems);
|
|
129
|
+
this.#executeBatch(method, group, first);
|
|
130
|
+
this.#executeBatch(method, group, second);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
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;
|
|
155
|
+
}
|
|
156
|
+
throw new Error("Something went wrong make batch response not contains enough responses. This can be a bug please report it.");
|
|
157
|
+
} catch (error) {
|
|
158
|
+
for (const [, , reject] of batchItems) {
|
|
159
|
+
reject(error);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
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
|
+
}
|
|
3
259
|
|
|
4
260
|
class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error {
|
|
5
261
|
}
|
|
@@ -8,6 +264,7 @@ class ClientRetryPlugin {
|
|
|
8
264
|
defaultRetryDelay;
|
|
9
265
|
defaultShouldRetry;
|
|
10
266
|
defaultOnRetry;
|
|
267
|
+
order = 18e5;
|
|
11
268
|
constructor(options = {}) {
|
|
12
269
|
this.defaultRetry = options.default?.retry ?? 0;
|
|
13
270
|
this.defaultRetryDelay = options.default?.retryDelay ?? ((o) => o.lastEventRetry ?? 2e3);
|
|
@@ -17,71 +274,57 @@ class ClientRetryPlugin {
|
|
|
17
274
|
init(options) {
|
|
18
275
|
options.interceptors ??= [];
|
|
19
276
|
options.interceptors.push(async (interceptorOptions) => {
|
|
20
|
-
const maxAttempts =
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
277
|
+
const maxAttempts = await value(
|
|
278
|
+
interceptorOptions.context.retry ?? this.defaultRetry,
|
|
279
|
+
interceptorOptions
|
|
280
|
+
);
|
|
281
|
+
const retryDelay = interceptorOptions.context.retryDelay ?? this.defaultRetryDelay;
|
|
282
|
+
const shouldRetry = interceptorOptions.context.shouldRetry ?? this.defaultShouldRetry;
|
|
283
|
+
const onRetry = interceptorOptions.context.onRetry ?? this.defaultOnRetry;
|
|
24
284
|
if (maxAttempts <= 0) {
|
|
25
285
|
return interceptorOptions.next();
|
|
26
286
|
}
|
|
27
|
-
let lastEventId = interceptorOptions.
|
|
287
|
+
let lastEventId = interceptorOptions.lastEventId;
|
|
28
288
|
let lastEventRetry;
|
|
29
|
-
let
|
|
289
|
+
let callback;
|
|
30
290
|
let attemptIndex = 0;
|
|
31
|
-
const next = async (
|
|
32
|
-
let
|
|
291
|
+
const next = async (initialError) => {
|
|
292
|
+
let currentError = initialError;
|
|
33
293
|
while (true) {
|
|
34
|
-
const
|
|
35
|
-
if (
|
|
294
|
+
const updatedInterceptorOptions = { ...interceptorOptions, lastEventId };
|
|
295
|
+
if (currentError) {
|
|
36
296
|
if (attemptIndex >= maxAttempts) {
|
|
37
|
-
throw
|
|
297
|
+
throw currentError.error;
|
|
38
298
|
}
|
|
39
299
|
const attemptOptions = {
|
|
300
|
+
...updatedInterceptorOptions,
|
|
40
301
|
attemptIndex,
|
|
41
|
-
error:
|
|
42
|
-
lastEventId,
|
|
302
|
+
error: currentError.error,
|
|
43
303
|
lastEventRetry
|
|
44
304
|
};
|
|
45
305
|
const shouldRetryBool = await value(
|
|
46
306
|
shouldRetry,
|
|
47
|
-
attemptOptions
|
|
48
|
-
newClientOptions,
|
|
49
|
-
interceptorOptions.path,
|
|
50
|
-
interceptorOptions.input
|
|
307
|
+
attemptOptions
|
|
51
308
|
);
|
|
52
309
|
if (!shouldRetryBool) {
|
|
53
|
-
throw
|
|
310
|
+
throw currentError.error;
|
|
54
311
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
newClientOptions,
|
|
58
|
-
interceptorOptions.path,
|
|
59
|
-
interceptorOptions.input
|
|
60
|
-
);
|
|
61
|
-
const retryDelayMs = await value(
|
|
62
|
-
retryDelay,
|
|
63
|
-
attemptOptions,
|
|
64
|
-
newClientOptions,
|
|
65
|
-
interceptorOptions.path,
|
|
66
|
-
interceptorOptions.input
|
|
67
|
-
);
|
|
312
|
+
callback = onRetry?.(attemptOptions);
|
|
313
|
+
const retryDelayMs = await value(retryDelay, attemptOptions);
|
|
68
314
|
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
|
|
69
315
|
attemptIndex++;
|
|
70
316
|
}
|
|
71
317
|
try {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
options: newClientOptions
|
|
75
|
-
});
|
|
76
|
-
return output2;
|
|
318
|
+
currentError = void 0;
|
|
319
|
+
return await interceptorOptions.next(updatedInterceptorOptions);
|
|
77
320
|
} catch (error) {
|
|
78
|
-
|
|
321
|
+
currentError = { error };
|
|
322
|
+
if (updatedInterceptorOptions.signal?.aborted) {
|
|
79
323
|
throw error;
|
|
80
324
|
}
|
|
81
|
-
current = { error };
|
|
82
325
|
} finally {
|
|
83
|
-
|
|
84
|
-
|
|
326
|
+
callback?.(!currentError);
|
|
327
|
+
callback = void 0;
|
|
85
328
|
}
|
|
86
329
|
}
|
|
87
330
|
};
|
|
@@ -89,19 +332,17 @@ class ClientRetryPlugin {
|
|
|
89
332
|
if (!isAsyncIteratorObject(output)) {
|
|
90
333
|
return output;
|
|
91
334
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
335
|
+
let current = output;
|
|
336
|
+
let isIteratorAborted = false;
|
|
337
|
+
return overlayProxy(() => current, new AsyncIteratorClass(
|
|
338
|
+
async () => {
|
|
95
339
|
while (true) {
|
|
96
340
|
try {
|
|
97
341
|
const item = await current.next();
|
|
98
342
|
const meta = getEventMeta(item.value);
|
|
99
343
|
lastEventId = meta?.id ?? lastEventId;
|
|
100
344
|
lastEventRetry = meta?.retry ?? lastEventRetry;
|
|
101
|
-
|
|
102
|
-
return item.value;
|
|
103
|
-
}
|
|
104
|
-
yield item.value;
|
|
345
|
+
return item;
|
|
105
346
|
} catch (error) {
|
|
106
347
|
const meta = getEventMeta(error);
|
|
107
348
|
lastEventId = meta?.id ?? lastEventId;
|
|
@@ -113,14 +354,132 @@ class ClientRetryPlugin {
|
|
|
113
354
|
);
|
|
114
355
|
}
|
|
115
356
|
current = maybeEventIterator;
|
|
357
|
+
if (isIteratorAborted) {
|
|
358
|
+
await current.return?.();
|
|
359
|
+
throw error;
|
|
360
|
+
}
|
|
116
361
|
}
|
|
117
362
|
}
|
|
118
|
-
}
|
|
119
|
-
|
|
363
|
+
},
|
|
364
|
+
async (reason) => {
|
|
365
|
+
isIteratorAborted = true;
|
|
366
|
+
if (reason !== "next") {
|
|
367
|
+
await current.return?.();
|
|
368
|
+
}
|
|
369
|
+
}
|
|
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
|
+
}
|
|
120
479
|
}
|
|
121
|
-
}
|
|
480
|
+
});
|
|
122
481
|
});
|
|
123
482
|
}
|
|
124
483
|
}
|
|
125
484
|
|
|
126
|
-
export { ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse };
|
|
485
|
+
export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, RetryAfterPlugin, SimpleCsrfProtectionLinkPlugin };
|
|
@@ -1,6 +1,16 @@
|
|
|
1
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,24 +20,19 @@ 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
|
-
|
|
23
|
+
interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
24
|
+
path: readonly string[];
|
|
25
|
+
input: unknown;
|
|
14
26
|
}
|
|
15
|
-
interface
|
|
16
|
-
|
|
27
|
+
interface StandardLinkClientInterceptorOptions<T extends ClientContext> extends StandardLinkInterceptorOptions<T> {
|
|
28
|
+
request: StandardRequest;
|
|
17
29
|
}
|
|
18
30
|
interface StandardLinkOptions<T extends ClientContext> {
|
|
19
|
-
interceptors?: Interceptor<
|
|
20
|
-
|
|
21
|
-
input: unknown;
|
|
22
|
-
options: ClientOptions<T>;
|
|
23
|
-
}, unknown, unknown>[];
|
|
24
|
-
clientInterceptors?: Interceptor<{
|
|
25
|
-
request: StandardRequest;
|
|
26
|
-
}, StandardLazyResponse, unknown>[];
|
|
31
|
+
interceptors?: Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>[];
|
|
32
|
+
clientInterceptors?: Interceptor<StandardLinkClientInterceptorOptions<T>, Promise<StandardLazyResponse>>[];
|
|
27
33
|
plugins?: StandardLinkPlugin<T>[];
|
|
28
34
|
}
|
|
29
35
|
declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
30
|
-
#private;
|
|
31
36
|
readonly codec: StandardLinkCodec<T>;
|
|
32
37
|
readonly sender: StandardLinkClient<T>;
|
|
33
38
|
private readonly interceptors;
|
|
@@ -36,4 +41,5 @@ declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
|
36
41
|
call(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
|
|
37
42
|
}
|
|
38
43
|
|
|
39
|
-
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 };
|
|
@@ -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,46 +44,30 @@ interface StandardRPCLinkCodecOptions<T extends ClientContext> {
|
|
|
44
44
|
/**
|
|
45
45
|
* Base url for all requests.
|
|
46
46
|
*/
|
|
47
|
-
url: Value<string | URL,
|
|
48
|
-
options: ClientOptions<T>,
|
|
49
|
-
path: readonly string[],
|
|
50
|
-
input: unknown
|
|
51
|
-
]>;
|
|
47
|
+
url: Value<Promisable<string | URL>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
52
48
|
/**
|
|
53
49
|
* The maximum length of the URL.
|
|
54
50
|
*
|
|
55
51
|
* @default 2083
|
|
56
52
|
*/
|
|
57
|
-
maxUrlLength?: Value<number,
|
|
58
|
-
options: ClientOptions<T>,
|
|
59
|
-
path: readonly string[],
|
|
60
|
-
input: unknown
|
|
61
|
-
]>;
|
|
53
|
+
maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
62
54
|
/**
|
|
63
55
|
* The method used to make the request.
|
|
64
56
|
*
|
|
65
57
|
* @default 'POST'
|
|
66
58
|
*/
|
|
67
|
-
method?: Value<HTTPMethod, [
|
|
68
|
-
options: ClientOptions<T>,
|
|
69
|
-
path: readonly string[],
|
|
70
|
-
input: unknown
|
|
71
|
-
]>;
|
|
59
|
+
method?: Value<Promisable<Exclude<HTTPMethod, 'HEAD'>>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
72
60
|
/**
|
|
73
61
|
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
74
62
|
* GET is not allowed, it's very dangerous.
|
|
75
63
|
*
|
|
76
64
|
* @default 'POST'
|
|
77
65
|
*/
|
|
78
|
-
fallbackMethod?: Exclude<HTTPMethod, 'GET'>;
|
|
66
|
+
fallbackMethod?: Exclude<HTTPMethod, 'HEAD' | 'GET'>;
|
|
79
67
|
/**
|
|
80
68
|
* Inject headers to the request.
|
|
81
69
|
*/
|
|
82
|
-
headers?: Value<StandardHeaders,
|
|
83
|
-
options: ClientOptions<T>,
|
|
84
|
-
path: readonly string[],
|
|
85
|
-
input: unknown
|
|
86
|
-
]>;
|
|
70
|
+
headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
87
71
|
}
|
|
88
72
|
declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
89
73
|
private readonly serializer;
|
|
@@ -99,5 +83,9 @@ declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardL
|
|
|
99
83
|
|
|
100
84
|
interface StandardRPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardRPCLinkCodecOptions<T>, StandardRPCJsonSerializerOptions {
|
|
101
85
|
}
|
|
86
|
+
declare class StandardRPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
87
|
+
constructor(linkClient: StandardLinkClient<T>, options: StandardRPCLinkOptions<T>);
|
|
88
|
+
}
|
|
102
89
|
|
|
103
|
-
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 };
|