@orpc/client 0.0.0-next.f9e0a4c → 0.0.0-next.f9e5dec
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 +5 -1
- package/dist/adapters/fetch/index.d.mts +23 -10
- package/dist/adapters/fetch/index.d.ts +23 -10
- package/dist/adapters/fetch/index.mjs +25 -8
- package/dist/adapters/message-port/index.d.mts +28 -7
- package/dist/adapters/message-port/index.d.ts +28 -7
- package/dist/adapters/message-port/index.mjs +26 -10
- 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 +3 -3
- package/dist/adapters/websocket/index.d.ts +3 -3
- package/dist/adapters/websocket/index.mjs +5 -3
- package/dist/index.d.mts +51 -6
- package/dist/index.d.ts +51 -6
- package/dist/index.mjs +36 -6
- package/dist/plugins/index.d.mts +52 -5
- package/dist/plugins/index.d.ts +52 -5
- package/dist/plugins/index.mjs +99 -14
- package/dist/shared/client.BH1AYT_p.d.mts +83 -0
- package/dist/shared/client.BH1AYT_p.d.ts +83 -0
- package/dist/shared/client.BLtwTQUg.mjs +40 -0
- package/dist/shared/{client.C176log5.d.ts → client.BxV-mzeR.d.ts} +3 -3
- package/dist/shared/{client.DKmRtVO2.mjs → client.CNUUHXJ_.mjs} +14 -6
- package/dist/shared/{client.Bwgm6dgk.d.mts → client.CPgZaUox.d.mts} +1 -1
- package/dist/shared/{client.txdq_i5V.mjs → client.CyPIKa9q.mjs} +30 -39
- package/dist/shared/{client.Ycwr4Tuo.d.mts → client.D8lMmWVC.d.mts} +3 -3
- package/dist/shared/{client.BG98rYdO.d.ts → client.De8SW4Kw.d.ts} +1 -1
- package/package.json +6 -6
- package/dist/shared/client.BOYsZIRq.d.mts +0 -29
- package/dist/shared/client.BOYsZIRq.d.ts +0 -29
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
3
|
-
import {
|
|
4
|
-
export {
|
|
1
|
+
import { preventNativeAwait, isTypescriptObject } from '@orpc/shared';
|
|
2
|
+
export { AsyncIteratorClass, EventPublisher, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onError, onFinish, onStart, onSuccess, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
|
|
3
|
+
import { i as isDefinedError } from './shared/client.CyPIKa9q.mjs';
|
|
4
|
+
export { C as COMMON_ORPC_ERROR_DEFS, c as ORPCError, O as ORPC_CLIENT_PACKAGE_NAME, a as ORPC_CLIENT_PACKAGE_VERSION, g as createORPCErrorFromJson, b as fallbackORPCErrorMessage, f as fallbackORPCErrorStatus, e as isORPCErrorJson, d as isORPCErrorStatus, t as toORPCError } from './shared/client.CyPIKa9q.mjs';
|
|
5
|
+
export { m as mapEventIterator } from './shared/client.BLtwTQUg.mjs';
|
|
5
6
|
export { ErrorEvent } from '@orpc/standard-server';
|
|
6
7
|
|
|
7
8
|
async function safe(promise) {
|
|
@@ -32,6 +33,35 @@ function resolveFriendlyClientOptions(options) {
|
|
|
32
33
|
// Context only optional if all fields are optional
|
|
33
34
|
};
|
|
34
35
|
}
|
|
36
|
+
function consumeEventIterator(iterator, options) {
|
|
37
|
+
void (async () => {
|
|
38
|
+
let onFinishState;
|
|
39
|
+
try {
|
|
40
|
+
const resolvedIterator = await iterator;
|
|
41
|
+
while (true) {
|
|
42
|
+
const { done, value } = await resolvedIterator.next();
|
|
43
|
+
if (done) {
|
|
44
|
+
const realValue = value;
|
|
45
|
+
onFinishState = [null, realValue, true];
|
|
46
|
+
options.onSuccess?.(realValue);
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
options.onEvent(value);
|
|
50
|
+
}
|
|
51
|
+
} catch (error) {
|
|
52
|
+
onFinishState = [error, void 0, false];
|
|
53
|
+
if (!options.onError && !options.onFinish) {
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
options.onError?.(error);
|
|
57
|
+
} finally {
|
|
58
|
+
options.onFinish?.(onFinishState);
|
|
59
|
+
}
|
|
60
|
+
})();
|
|
61
|
+
return async () => {
|
|
62
|
+
await (await iterator)?.return?.();
|
|
63
|
+
};
|
|
64
|
+
}
|
|
35
65
|
|
|
36
66
|
function createORPCClient(link, options = {}) {
|
|
37
67
|
const path = options.path ?? [];
|
|
@@ -49,7 +79,7 @@ function createORPCClient(link, options = {}) {
|
|
|
49
79
|
});
|
|
50
80
|
}
|
|
51
81
|
});
|
|
52
|
-
return recursive;
|
|
82
|
+
return preventNativeAwait(recursive);
|
|
53
83
|
}
|
|
54
84
|
|
|
55
85
|
function createSafeClient(client) {
|
|
@@ -79,4 +109,4 @@ class DynamicLink {
|
|
|
79
109
|
}
|
|
80
110
|
}
|
|
81
111
|
|
|
82
|
-
export { DynamicLink, createORPCClient, createSafeClient, isDefinedError, resolveFriendlyClientOptions, safe };
|
|
112
|
+
export { DynamicLink, consumeEventIterator, createORPCClient, createSafeClient, isDefinedError, resolveFriendlyClientOptions, safe };
|
package/dist/plugins/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Value, Promisable } from '@orpc/shared';
|
|
2
|
-
import { StandardHeaders, StandardRequest } from '@orpc/standard-server';
|
|
2
|
+
import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
3
3
|
import { BatchResponseMode } from '@orpc/standard-server/batch';
|
|
4
|
-
import { S as StandardLinkClientInterceptorOptions, a as StandardLinkPlugin, b as StandardLinkOptions, c as StandardLinkInterceptorOptions } from '../shared/client.
|
|
5
|
-
import { b as ClientContext } from '../shared/client.
|
|
4
|
+
import { S as StandardLinkClientInterceptorOptions, a as StandardLinkPlugin, b as StandardLinkOptions, c as StandardLinkInterceptorOptions } from '../shared/client.CPgZaUox.mjs';
|
|
5
|
+
import { b as ClientContext } from '../shared/client.BH1AYT_p.mjs';
|
|
6
6
|
|
|
7
7
|
interface BatchLinkPluginGroup<T extends ClientContext> {
|
|
8
8
|
condition(options: StandardLinkClientInterceptorOptions<T>): boolean;
|
|
@@ -157,10 +157,57 @@ declare class ClientRetryPlugin<T extends ClientRetryPluginContext> implements S
|
|
|
157
157
|
private readonly defaultRetryDelay;
|
|
158
158
|
private readonly defaultShouldRetry;
|
|
159
159
|
private readonly defaultOnRetry;
|
|
160
|
+
order: number;
|
|
160
161
|
constructor(options?: ClientRetryPluginOptions);
|
|
161
162
|
init(options: StandardLinkOptions<T>): void;
|
|
162
163
|
}
|
|
163
164
|
|
|
165
|
+
interface RetryAfterPluginOptions<T extends ClientContext> {
|
|
166
|
+
/**
|
|
167
|
+
* Override condition to determine whether to retry or not.
|
|
168
|
+
*
|
|
169
|
+
* @default ((response) => response.status === 429 || response.status === 503)
|
|
170
|
+
*/
|
|
171
|
+
condition?: Value<boolean, [
|
|
172
|
+
response: StandardLazyResponse,
|
|
173
|
+
options: StandardLinkClientInterceptorOptions<T>
|
|
174
|
+
]>;
|
|
175
|
+
/**
|
|
176
|
+
* Maximum attempts before giving up retries.
|
|
177
|
+
*
|
|
178
|
+
* @default 3
|
|
179
|
+
*/
|
|
180
|
+
maxAttempts?: Value<number, [
|
|
181
|
+
response: StandardLazyResponse,
|
|
182
|
+
options: StandardLinkClientInterceptorOptions<T>
|
|
183
|
+
]>;
|
|
184
|
+
/**
|
|
185
|
+
* Maximum timeout in milliseconds to wait before giving up retries.
|
|
186
|
+
*
|
|
187
|
+
* @default 5 * 60 * 1000 (5 minutes)
|
|
188
|
+
*/
|
|
189
|
+
timeout?: Value<number, [
|
|
190
|
+
response: StandardLazyResponse,
|
|
191
|
+
options: StandardLinkClientInterceptorOptions<T>
|
|
192
|
+
]>;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* The Retry After Plugin automatically retries requests based on server `Retry-After` headers.
|
|
196
|
+
* This is particularly useful for handling rate limiting and temporary server unavailability.
|
|
197
|
+
*
|
|
198
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/retry-after Retry After Plugin Docs}
|
|
199
|
+
*/
|
|
200
|
+
declare class RetryAfterPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
201
|
+
private readonly condition;
|
|
202
|
+
private readonly maxAttempts;
|
|
203
|
+
private readonly timeout;
|
|
204
|
+
order: number;
|
|
205
|
+
constructor(options?: RetryAfterPluginOptions<T>);
|
|
206
|
+
init(options: StandardLinkOptions<T>): void;
|
|
207
|
+
private parseRetryAfterHeader;
|
|
208
|
+
private delayExecution;
|
|
209
|
+
}
|
|
210
|
+
|
|
164
211
|
interface SimpleCsrfProtectionLinkPluginOptions<T extends ClientContext> {
|
|
165
212
|
/**
|
|
166
213
|
* The name of the header to check.
|
|
@@ -198,5 +245,5 @@ declare class SimpleCsrfProtectionLinkPlugin<T extends ClientContext> implements
|
|
|
198
245
|
init(options: StandardLinkOptions<T>): void;
|
|
199
246
|
}
|
|
200
247
|
|
|
201
|
-
export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, SimpleCsrfProtectionLinkPlugin };
|
|
202
|
-
export type { BatchLinkPluginGroup, BatchLinkPluginOptions, ClientRetryPluginAttemptOptions, ClientRetryPluginContext, ClientRetryPluginOptions, DedupeRequestsPluginGroup, DedupeRequestsPluginOptions, SimpleCsrfProtectionLinkPluginOptions };
|
|
248
|
+
export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, RetryAfterPlugin, SimpleCsrfProtectionLinkPlugin };
|
|
249
|
+
export type { BatchLinkPluginGroup, BatchLinkPluginOptions, ClientRetryPluginAttemptOptions, ClientRetryPluginContext, ClientRetryPluginOptions, DedupeRequestsPluginGroup, DedupeRequestsPluginOptions, RetryAfterPluginOptions, SimpleCsrfProtectionLinkPluginOptions };
|
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Value, Promisable } from '@orpc/shared';
|
|
2
|
-
import { StandardHeaders, StandardRequest } from '@orpc/standard-server';
|
|
2
|
+
import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
3
3
|
import { BatchResponseMode } from '@orpc/standard-server/batch';
|
|
4
|
-
import { S as StandardLinkClientInterceptorOptions, a as StandardLinkPlugin, b as StandardLinkOptions, c as StandardLinkInterceptorOptions } from '../shared/client.
|
|
5
|
-
import { b as ClientContext } from '../shared/client.
|
|
4
|
+
import { S as StandardLinkClientInterceptorOptions, a as StandardLinkPlugin, b as StandardLinkOptions, c as StandardLinkInterceptorOptions } from '../shared/client.De8SW4Kw.js';
|
|
5
|
+
import { b as ClientContext } from '../shared/client.BH1AYT_p.js';
|
|
6
6
|
|
|
7
7
|
interface BatchLinkPluginGroup<T extends ClientContext> {
|
|
8
8
|
condition(options: StandardLinkClientInterceptorOptions<T>): boolean;
|
|
@@ -157,10 +157,57 @@ declare class ClientRetryPlugin<T extends ClientRetryPluginContext> implements S
|
|
|
157
157
|
private readonly defaultRetryDelay;
|
|
158
158
|
private readonly defaultShouldRetry;
|
|
159
159
|
private readonly defaultOnRetry;
|
|
160
|
+
order: number;
|
|
160
161
|
constructor(options?: ClientRetryPluginOptions);
|
|
161
162
|
init(options: StandardLinkOptions<T>): void;
|
|
162
163
|
}
|
|
163
164
|
|
|
165
|
+
interface RetryAfterPluginOptions<T extends ClientContext> {
|
|
166
|
+
/**
|
|
167
|
+
* Override condition to determine whether to retry or not.
|
|
168
|
+
*
|
|
169
|
+
* @default ((response) => response.status === 429 || response.status === 503)
|
|
170
|
+
*/
|
|
171
|
+
condition?: Value<boolean, [
|
|
172
|
+
response: StandardLazyResponse,
|
|
173
|
+
options: StandardLinkClientInterceptorOptions<T>
|
|
174
|
+
]>;
|
|
175
|
+
/**
|
|
176
|
+
* Maximum attempts before giving up retries.
|
|
177
|
+
*
|
|
178
|
+
* @default 3
|
|
179
|
+
*/
|
|
180
|
+
maxAttempts?: Value<number, [
|
|
181
|
+
response: StandardLazyResponse,
|
|
182
|
+
options: StandardLinkClientInterceptorOptions<T>
|
|
183
|
+
]>;
|
|
184
|
+
/**
|
|
185
|
+
* Maximum timeout in milliseconds to wait before giving up retries.
|
|
186
|
+
*
|
|
187
|
+
* @default 5 * 60 * 1000 (5 minutes)
|
|
188
|
+
*/
|
|
189
|
+
timeout?: Value<number, [
|
|
190
|
+
response: StandardLazyResponse,
|
|
191
|
+
options: StandardLinkClientInterceptorOptions<T>
|
|
192
|
+
]>;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* The Retry After Plugin automatically retries requests based on server `Retry-After` headers.
|
|
196
|
+
* This is particularly useful for handling rate limiting and temporary server unavailability.
|
|
197
|
+
*
|
|
198
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/retry-after Retry After Plugin Docs}
|
|
199
|
+
*/
|
|
200
|
+
declare class RetryAfterPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
201
|
+
private readonly condition;
|
|
202
|
+
private readonly maxAttempts;
|
|
203
|
+
private readonly timeout;
|
|
204
|
+
order: number;
|
|
205
|
+
constructor(options?: RetryAfterPluginOptions<T>);
|
|
206
|
+
init(options: StandardLinkOptions<T>): void;
|
|
207
|
+
private parseRetryAfterHeader;
|
|
208
|
+
private delayExecution;
|
|
209
|
+
}
|
|
210
|
+
|
|
164
211
|
interface SimpleCsrfProtectionLinkPluginOptions<T extends ClientContext> {
|
|
165
212
|
/**
|
|
166
213
|
* The name of the header to check.
|
|
@@ -198,5 +245,5 @@ declare class SimpleCsrfProtectionLinkPlugin<T extends ClientContext> implements
|
|
|
198
245
|
init(options: StandardLinkOptions<T>): void;
|
|
199
246
|
}
|
|
200
247
|
|
|
201
|
-
export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, SimpleCsrfProtectionLinkPlugin };
|
|
202
|
-
export type { BatchLinkPluginGroup, BatchLinkPluginOptions, ClientRetryPluginAttemptOptions, ClientRetryPluginContext, ClientRetryPluginOptions, DedupeRequestsPluginGroup, DedupeRequestsPluginOptions, SimpleCsrfProtectionLinkPluginOptions };
|
|
248
|
+
export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, RetryAfterPlugin, SimpleCsrfProtectionLinkPlugin };
|
|
249
|
+
export type { BatchLinkPluginGroup, BatchLinkPluginOptions, ClientRetryPluginAttemptOptions, ClientRetryPluginContext, ClientRetryPluginOptions, DedupeRequestsPluginGroup, DedupeRequestsPluginOptions, RetryAfterPluginOptions, SimpleCsrfProtectionLinkPluginOptions };
|
package/dist/plugins/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { isAsyncIteratorObject, defer, value, splitInHalf, toArray, stringifyJSON } from '@orpc/shared';
|
|
1
|
+
import { isAsyncIteratorObject, defer, value, splitInHalf, toArray, stringifyJSON, overlayProxy, AsyncIteratorClass } from '@orpc/shared';
|
|
2
2
|
import { toBatchRequest, parseBatchResponse, toBatchAbortSignal } from '@orpc/standard-server/batch';
|
|
3
|
-
import { replicateStandardLazyResponse, getEventMeta } from '@orpc/standard-server';
|
|
3
|
+
import { replicateStandardLazyResponse, getEventMeta, flattenHeader } from '@orpc/standard-server';
|
|
4
|
+
import { C as COMMON_ORPC_ERROR_DEFS } from '../shared/client.CyPIKa9q.mjs';
|
|
4
5
|
|
|
5
6
|
class BatchLinkPlugin {
|
|
6
7
|
groups;
|
|
@@ -263,6 +264,7 @@ class ClientRetryPlugin {
|
|
|
263
264
|
defaultRetryDelay;
|
|
264
265
|
defaultShouldRetry;
|
|
265
266
|
defaultOnRetry;
|
|
267
|
+
order = 18e5;
|
|
266
268
|
constructor(options = {}) {
|
|
267
269
|
this.defaultRetry = options.default?.retry ?? 0;
|
|
268
270
|
this.defaultRetryDelay = options.default?.retryDelay ?? ((o) => o.lastEventRetry ?? 2e3);
|
|
@@ -317,7 +319,7 @@ class ClientRetryPlugin {
|
|
|
317
319
|
return await interceptorOptions.next(updatedInterceptorOptions);
|
|
318
320
|
} catch (error) {
|
|
319
321
|
currentError = { error };
|
|
320
|
-
if (updatedInterceptorOptions.signal?.aborted
|
|
322
|
+
if (updatedInterceptorOptions.signal?.aborted) {
|
|
321
323
|
throw error;
|
|
322
324
|
}
|
|
323
325
|
} finally {
|
|
@@ -330,19 +332,17 @@ class ClientRetryPlugin {
|
|
|
330
332
|
if (!isAsyncIteratorObject(output)) {
|
|
331
333
|
return output;
|
|
332
334
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
335
|
+
let current = output;
|
|
336
|
+
let isIteratorAborted = false;
|
|
337
|
+
return overlayProxy(() => current, new AsyncIteratorClass(
|
|
338
|
+
async () => {
|
|
336
339
|
while (true) {
|
|
337
340
|
try {
|
|
338
341
|
const item = await current.next();
|
|
339
342
|
const meta = getEventMeta(item.value);
|
|
340
343
|
lastEventId = meta?.id ?? lastEventId;
|
|
341
344
|
lastEventRetry = meta?.retry ?? lastEventRetry;
|
|
342
|
-
|
|
343
|
-
return item.value;
|
|
344
|
-
}
|
|
345
|
-
yield item.value;
|
|
345
|
+
return item;
|
|
346
346
|
} catch (error) {
|
|
347
347
|
const meta = getEventMeta(error);
|
|
348
348
|
lastEventId = meta?.id ?? lastEventId;
|
|
@@ -354,12 +354,97 @@ class ClientRetryPlugin {
|
|
|
354
354
|
);
|
|
355
355
|
}
|
|
356
356
|
current = maybeEventIterator;
|
|
357
|
+
if (isIteratorAborted) {
|
|
358
|
+
await current.return?.();
|
|
359
|
+
throw error;
|
|
360
|
+
}
|
|
357
361
|
}
|
|
358
362
|
}
|
|
359
|
-
}
|
|
360
|
-
|
|
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;
|
|
361
400
|
}
|
|
362
|
-
|
|
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);
|
|
363
448
|
});
|
|
364
449
|
}
|
|
365
450
|
}
|
|
@@ -397,4 +482,4 @@ class SimpleCsrfProtectionLinkPlugin {
|
|
|
397
482
|
}
|
|
398
483
|
}
|
|
399
484
|
|
|
400
|
-
export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, SimpleCsrfProtectionLinkPlugin };
|
|
485
|
+
export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, RetryAfterPlugin, SimpleCsrfProtectionLinkPlugin };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { PromiseWithError } from '@orpc/shared';
|
|
2
|
+
|
|
3
|
+
type HTTPPath = `/${string}`;
|
|
4
|
+
type HTTPMethod = 'HEAD' | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
5
|
+
type ClientContext = Record<PropertyKey, any>;
|
|
6
|
+
interface ClientOptions<T extends ClientContext> {
|
|
7
|
+
signal?: AbortSignal;
|
|
8
|
+
lastEventId?: string | undefined;
|
|
9
|
+
context: T;
|
|
10
|
+
}
|
|
11
|
+
type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
|
12
|
+
context?: T;
|
|
13
|
+
} : {
|
|
14
|
+
context: T;
|
|
15
|
+
});
|
|
16
|
+
type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options: FriendlyClientOptions<TClientContext>];
|
|
17
|
+
type ClientPromiseResult<TOutput, TError> = PromiseWithError<TOutput, TError>;
|
|
18
|
+
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
19
|
+
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
|
20
|
+
}
|
|
21
|
+
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
22
|
+
[k: string]: NestedClient<TClientContext>;
|
|
23
|
+
};
|
|
24
|
+
type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
|
|
25
|
+
interface ClientLink<TClientContext extends ClientContext> {
|
|
26
|
+
call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Recursively infers the **input types** from a client.
|
|
30
|
+
*
|
|
31
|
+
* Produces a nested map where each endpoint's input type is preserved.
|
|
32
|
+
*/
|
|
33
|
+
type InferClientInputs<T extends NestedClient<any>> = T extends Client<any, infer U, any, any> ? U : {
|
|
34
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientInputs<T[K]> : never;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Recursively infers the **body input types** from a client.
|
|
38
|
+
*
|
|
39
|
+
* If an endpoint's input includes `{ body: ... }`, only the `body` portion is extracted.
|
|
40
|
+
* Produces a nested map of body input types.
|
|
41
|
+
*/
|
|
42
|
+
type InferClientBodyInputs<T extends NestedClient<any>> = T extends Client<any, infer U, any, any> ? U extends {
|
|
43
|
+
body: infer UBody;
|
|
44
|
+
} ? UBody : U : {
|
|
45
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientBodyInputs<T[K]> : never;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Recursively infers the **output types** from a client.
|
|
49
|
+
*
|
|
50
|
+
* Produces a nested map where each endpoint's output type is preserved.
|
|
51
|
+
*/
|
|
52
|
+
type InferClientOutputs<T extends NestedClient<any>> = T extends Client<any, any, infer U, any> ? U : {
|
|
53
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientOutputs<T[K]> : never;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Recursively infers the **body output types** from a client.
|
|
57
|
+
*
|
|
58
|
+
* If an endpoint's output includes `{ body: ... }`, only the `body` portion is extracted.
|
|
59
|
+
* Produces a nested map of body output types.
|
|
60
|
+
*/
|
|
61
|
+
type InferClientBodyOutputs<T extends NestedClient<any>> = T extends Client<any, any, infer U, any> ? U extends {
|
|
62
|
+
body: infer UBody;
|
|
63
|
+
} ? UBody : U : {
|
|
64
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientBodyOutputs<T[K]> : never;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Recursively infers the **error types** from a client when you use [type-safe errors](https://orpc.unnoq.com/docs/error-handling#type‐safe-error-handling).
|
|
68
|
+
*
|
|
69
|
+
* Produces a nested map where each endpoint's error type is preserved.
|
|
70
|
+
*/
|
|
71
|
+
type InferClientErrors<T extends NestedClient<any>> = T extends Client<any, any, any, infer U> ? U : {
|
|
72
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientErrors<T[K]> : never;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Recursively infers a **union of all error types** from a client when you use [type-safe errors](https://orpc.unnoq.com/docs/error-handling#type‐safe-error-handling).
|
|
76
|
+
*
|
|
77
|
+
* Useful when you want to handle all possible errors from any endpoint at once.
|
|
78
|
+
*/
|
|
79
|
+
type InferClientErrorUnion<T extends NestedClient<any>> = T extends Client<any, any, any, infer U> ? U : {
|
|
80
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientErrorUnion<T[K]> : never;
|
|
81
|
+
}[keyof T];
|
|
82
|
+
|
|
83
|
+
export type { ClientLink as C, FriendlyClientOptions as F, HTTPPath as H, InferClientContext as I, NestedClient as N, ClientPromiseResult as a, ClientContext as b, ClientOptions as c, Client as d, ClientRest as e, HTTPMethod as f, InferClientInputs as g, InferClientBodyInputs as h, InferClientOutputs as i, InferClientBodyOutputs as j, InferClientErrors as k, InferClientErrorUnion as l };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { PromiseWithError } from '@orpc/shared';
|
|
2
|
+
|
|
3
|
+
type HTTPPath = `/${string}`;
|
|
4
|
+
type HTTPMethod = 'HEAD' | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
5
|
+
type ClientContext = Record<PropertyKey, any>;
|
|
6
|
+
interface ClientOptions<T extends ClientContext> {
|
|
7
|
+
signal?: AbortSignal;
|
|
8
|
+
lastEventId?: string | undefined;
|
|
9
|
+
context: T;
|
|
10
|
+
}
|
|
11
|
+
type FriendlyClientOptions<T extends ClientContext> = Omit<ClientOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
|
12
|
+
context?: T;
|
|
13
|
+
} : {
|
|
14
|
+
context: T;
|
|
15
|
+
});
|
|
16
|
+
type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options?: FriendlyClientOptions<TClientContext>] : [input: TInput, options: FriendlyClientOptions<TClientContext>];
|
|
17
|
+
type ClientPromiseResult<TOutput, TError> = PromiseWithError<TOutput, TError>;
|
|
18
|
+
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
19
|
+
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
|
20
|
+
}
|
|
21
|
+
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
|
22
|
+
[k: string]: NestedClient<TClientContext>;
|
|
23
|
+
};
|
|
24
|
+
type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
|
|
25
|
+
interface ClientLink<TClientContext extends ClientContext> {
|
|
26
|
+
call: (path: readonly string[], input: unknown, options: ClientOptions<TClientContext>) => Promise<unknown>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Recursively infers the **input types** from a client.
|
|
30
|
+
*
|
|
31
|
+
* Produces a nested map where each endpoint's input type is preserved.
|
|
32
|
+
*/
|
|
33
|
+
type InferClientInputs<T extends NestedClient<any>> = T extends Client<any, infer U, any, any> ? U : {
|
|
34
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientInputs<T[K]> : never;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Recursively infers the **body input types** from a client.
|
|
38
|
+
*
|
|
39
|
+
* If an endpoint's input includes `{ body: ... }`, only the `body` portion is extracted.
|
|
40
|
+
* Produces a nested map of body input types.
|
|
41
|
+
*/
|
|
42
|
+
type InferClientBodyInputs<T extends NestedClient<any>> = T extends Client<any, infer U, any, any> ? U extends {
|
|
43
|
+
body: infer UBody;
|
|
44
|
+
} ? UBody : U : {
|
|
45
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientBodyInputs<T[K]> : never;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Recursively infers the **output types** from a client.
|
|
49
|
+
*
|
|
50
|
+
* Produces a nested map where each endpoint's output type is preserved.
|
|
51
|
+
*/
|
|
52
|
+
type InferClientOutputs<T extends NestedClient<any>> = T extends Client<any, any, infer U, any> ? U : {
|
|
53
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientOutputs<T[K]> : never;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Recursively infers the **body output types** from a client.
|
|
57
|
+
*
|
|
58
|
+
* If an endpoint's output includes `{ body: ... }`, only the `body` portion is extracted.
|
|
59
|
+
* Produces a nested map of body output types.
|
|
60
|
+
*/
|
|
61
|
+
type InferClientBodyOutputs<T extends NestedClient<any>> = T extends Client<any, any, infer U, any> ? U extends {
|
|
62
|
+
body: infer UBody;
|
|
63
|
+
} ? UBody : U : {
|
|
64
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientBodyOutputs<T[K]> : never;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Recursively infers the **error types** from a client when you use [type-safe errors](https://orpc.unnoq.com/docs/error-handling#type‐safe-error-handling).
|
|
68
|
+
*
|
|
69
|
+
* Produces a nested map where each endpoint's error type is preserved.
|
|
70
|
+
*/
|
|
71
|
+
type InferClientErrors<T extends NestedClient<any>> = T extends Client<any, any, any, infer U> ? U : {
|
|
72
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientErrors<T[K]> : never;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Recursively infers a **union of all error types** from a client when you use [type-safe errors](https://orpc.unnoq.com/docs/error-handling#type‐safe-error-handling).
|
|
76
|
+
*
|
|
77
|
+
* Useful when you want to handle all possible errors from any endpoint at once.
|
|
78
|
+
*/
|
|
79
|
+
type InferClientErrorUnion<T extends NestedClient<any>> = T extends Client<any, any, any, infer U> ? U : {
|
|
80
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? InferClientErrorUnion<T[K]> : never;
|
|
81
|
+
}[keyof T];
|
|
82
|
+
|
|
83
|
+
export type { ClientLink as C, FriendlyClientOptions as F, HTTPPath as H, InferClientContext as I, NestedClient as N, ClientPromiseResult as a, ClientContext as b, ClientOptions as c, Client as d, ClientRest as e, HTTPMethod as f, InferClientInputs as g, InferClientBodyInputs as h, InferClientOutputs as i, InferClientBodyOutputs as j, InferClientErrors as k, InferClientErrorUnion as l };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { AsyncIteratorClass, isTypescriptObject } from '@orpc/shared';
|
|
2
|
+
import { getEventMeta, withEventMeta } from '@orpc/standard-server';
|
|
3
|
+
|
|
4
|
+
function mapEventIterator(iterator, maps) {
|
|
5
|
+
const mapError = async (error) => {
|
|
6
|
+
let mappedError = await maps.error(error);
|
|
7
|
+
if (mappedError !== error) {
|
|
8
|
+
const meta = getEventMeta(error);
|
|
9
|
+
if (meta && isTypescriptObject(mappedError)) {
|
|
10
|
+
mappedError = withEventMeta(mappedError, meta);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return mappedError;
|
|
14
|
+
};
|
|
15
|
+
return new AsyncIteratorClass(async () => {
|
|
16
|
+
const { done, value } = await (async () => {
|
|
17
|
+
try {
|
|
18
|
+
return await iterator.next();
|
|
19
|
+
} catch (error) {
|
|
20
|
+
throw await mapError(error);
|
|
21
|
+
}
|
|
22
|
+
})();
|
|
23
|
+
let mappedValue = await maps.value(value, done);
|
|
24
|
+
if (mappedValue !== value) {
|
|
25
|
+
const meta = getEventMeta(value);
|
|
26
|
+
if (meta && isTypescriptObject(mappedValue)) {
|
|
27
|
+
mappedValue = withEventMeta(mappedValue, meta);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return { done, value: mappedValue };
|
|
31
|
+
}, async () => {
|
|
32
|
+
try {
|
|
33
|
+
await iterator.return?.();
|
|
34
|
+
} catch (error) {
|
|
35
|
+
throw await mapError(error);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { mapEventIterator as m };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { b as ClientContext, c as ClientOptions, f as HTTPMethod } from './client.
|
|
2
|
-
import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.
|
|
1
|
+
import { b as ClientContext, c as ClientOptions, f as HTTPMethod } from './client.BH1AYT_p.js';
|
|
2
|
+
import { e as StandardLinkCodec, b as StandardLinkOptions, d as StandardLink, f as StandardLinkClient } from './client.De8SW4Kw.js';
|
|
3
3
|
import { Segment, Value, Promisable } from '@orpc/shared';
|
|
4
4
|
import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
5
5
|
|
|
@@ -67,7 +67,7 @@ interface StandardRPCLinkCodecOptions<T extends ClientContext> {
|
|
|
67
67
|
/**
|
|
68
68
|
* Inject headers to the request.
|
|
69
69
|
*/
|
|
70
|
-
headers?: Value<Promisable<StandardHeaders>, [options: ClientOptions<T>, path: readonly string[], input: unknown]>;
|
|
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;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { toArray, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, intercept, getGlobalOtelConfig, isObject, value, stringifyJSON } from '@orpc/shared';
|
|
2
2
|
import { mergeStandardHeaders, ErrorEvent } from '@orpc/standard-server';
|
|
3
|
-
import { C as COMMON_ORPC_ERROR_DEFS,
|
|
3
|
+
import { C as COMMON_ORPC_ERROR_DEFS, d as isORPCErrorStatus, e as isORPCErrorJson, g as createORPCErrorFromJson, c as ORPCError, t as toORPCError } from './client.CyPIKa9q.mjs';
|
|
4
|
+
import { toStandardHeaders as toStandardHeaders$1 } from '@orpc/standard-server-fetch';
|
|
5
|
+
import { m as mapEventIterator } from './client.BLtwTQUg.mjs';
|
|
4
6
|
|
|
5
7
|
class CompositeStandardLinkPlugin {
|
|
6
8
|
plugins;
|
|
@@ -224,6 +226,12 @@ class StandardRPCJsonSerializer {
|
|
|
224
226
|
function toHttpPath(path) {
|
|
225
227
|
return `/${path.map(encodeURIComponent).join("/")}`;
|
|
226
228
|
}
|
|
229
|
+
function toStandardHeaders(headers) {
|
|
230
|
+
if (typeof headers.forEach === "function") {
|
|
231
|
+
return toStandardHeaders$1(headers);
|
|
232
|
+
}
|
|
233
|
+
return headers;
|
|
234
|
+
}
|
|
227
235
|
function getMalformedResponseErrorCode(status) {
|
|
228
236
|
return Object.entries(COMMON_ORPC_ERROR_DEFS).find(([, def]) => def.status === status)?.[0] ?? "MALFORMED_ORPC_ERROR_RESPONSE";
|
|
229
237
|
}
|
|
@@ -243,14 +251,14 @@ class StandardRPCLinkCodec {
|
|
|
243
251
|
expectedMethod;
|
|
244
252
|
headers;
|
|
245
253
|
async encode(path, input, options) {
|
|
254
|
+
let headers = toStandardHeaders(await value(this.headers, options, path, input));
|
|
255
|
+
if (options.lastEventId !== void 0) {
|
|
256
|
+
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
257
|
+
}
|
|
246
258
|
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
|
247
|
-
let headers = await value(this.headers, options, path, input);
|
|
248
259
|
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
249
260
|
const url = new URL(baseUrl);
|
|
250
261
|
url.pathname = `${url.pathname.replace(/\/$/, "")}${toHttpPath(path)}`;
|
|
251
|
-
if (options.lastEventId !== void 0) {
|
|
252
|
-
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
253
|
-
}
|
|
254
262
|
const serialized = this.serializer.serialize(input);
|
|
255
263
|
if (expectedMethod === "GET" && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
|
|
256
264
|
const maxUrlLength = await value(this.maxUrlLength, options, path, input);
|
|
@@ -387,4 +395,4 @@ class StandardRPCLink extends StandardLink {
|
|
|
387
395
|
}
|
|
388
396
|
}
|
|
389
397
|
|
|
390
|
-
export { CompositeStandardLinkPlugin as C, StandardLink as S, STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as a, StandardRPCJsonSerializer as b, StandardRPCLink as c, StandardRPCLinkCodec as d, StandardRPCSerializer as e, getMalformedResponseErrorCode as g, toHttpPath as t };
|
|
398
|
+
export { CompositeStandardLinkPlugin as C, StandardLink as S, STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as a, StandardRPCJsonSerializer as b, StandardRPCLink as c, StandardRPCLinkCodec as d, StandardRPCSerializer as e, toStandardHeaders as f, getMalformedResponseErrorCode as g, toHttpPath as t };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Interceptor } from '@orpc/shared';
|
|
2
2
|
import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
3
|
-
import { b as ClientContext, c as ClientOptions, C as ClientLink } from './client.
|
|
3
|
+
import { b as ClientContext, c as ClientOptions, C as ClientLink } from './client.BH1AYT_p.mjs';
|
|
4
4
|
|
|
5
5
|
interface StandardLinkPlugin<T extends ClientContext> {
|
|
6
6
|
order?: number;
|