@orpc/client 0.0.0-next.c099c92 → 0.0.0-next.c0dd7cd
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 +92 -0
- package/dist/adapters/fetch/index.d.mts +30 -0
- package/dist/adapters/fetch/index.d.ts +30 -0
- package/dist/adapters/fetch/index.mjs +37 -0
- package/dist/adapters/standard/index.d.mts +105 -0
- package/dist/adapters/standard/index.d.ts +105 -0
- package/dist/adapters/standard/index.mjs +5 -0
- package/dist/index.d.mts +150 -0
- package/dist/index.d.ts +150 -0
- package/dist/index.mjs +63 -0
- package/dist/plugins/index.d.mts +62 -0
- package/dist/plugins/index.d.ts +62 -0
- package/dist/plugins/index.mjs +127 -0
- package/dist/shared/client.BacCdg3F.mjs +172 -0
- package/dist/shared/client.Be-O_tdu.mjs +331 -0
- package/dist/shared/client.CupM8eRP.d.mts +30 -0
- package/dist/shared/client.CupM8eRP.d.ts +30 -0
- package/dist/shared/client.CvnV7_uV.mjs +12 -0
- package/dist/shared/client.DrOAzyMB.d.mts +45 -0
- package/dist/shared/client.aGal-uGY.d.ts +45 -0
- package/package.json +22 -18
- package/dist/fetch.js +0 -87
- package/dist/index.js +0 -42
- package/dist/src/adapters/fetch/index.d.ts +0 -3
- package/dist/src/adapters/fetch/orpc-link.d.ts +0 -46
- package/dist/src/adapters/fetch/types.d.ts +0 -4
- package/dist/src/client.d.ts +0 -11
- package/dist/src/dynamic-link.d.ts +0 -13
- package/dist/src/index.d.ts +0 -6
- package/dist/src/types.d.ts +0 -5
package/dist/index.d.ts
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
import { N as NestedClient, b as ClientLink, I as InferClientContext, a as ClientContext, C as ClientOptionsOut, c as ClientPromiseResult } from './shared/client.CupM8eRP.js';
|
2
|
+
export { f as Client, d as ClientOptions, e as ClientRest } from './shared/client.CupM8eRP.js';
|
3
|
+
import { Promisable, MaybeOptionalOptions } from '@orpc/shared';
|
4
|
+
export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
5
|
+
export { ErrorEvent } from '@orpc/standard-server';
|
6
|
+
|
7
|
+
interface createORPCClientOptions {
|
8
|
+
/**
|
9
|
+
* Use as base path for all procedure, useful when you only want to call a subset of the procedure.
|
10
|
+
*/
|
11
|
+
path?: string[];
|
12
|
+
}
|
13
|
+
declare function createORPCClient<T extends NestedClient<any>>(link: ClientLink<InferClientContext<T>>, options?: createORPCClientOptions): T;
|
14
|
+
|
15
|
+
/**
|
16
|
+
* DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
|
17
|
+
* based on the request path, input, and context.
|
18
|
+
*/
|
19
|
+
declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
|
20
|
+
private readonly linkResolver;
|
21
|
+
constructor(linkResolver: (options: ClientOptionsOut<TClientContext>, path: readonly string[], input: unknown) => Promisable<ClientLink<TClientContext>>);
|
22
|
+
call(path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>): Promise<unknown>;
|
23
|
+
}
|
24
|
+
|
25
|
+
declare const COMMON_ORPC_ERROR_DEFS: {
|
26
|
+
readonly BAD_REQUEST: {
|
27
|
+
readonly status: 400;
|
28
|
+
readonly message: "Bad Request";
|
29
|
+
};
|
30
|
+
readonly UNAUTHORIZED: {
|
31
|
+
readonly status: 401;
|
32
|
+
readonly message: "Unauthorized";
|
33
|
+
};
|
34
|
+
readonly FORBIDDEN: {
|
35
|
+
readonly status: 403;
|
36
|
+
readonly message: "Forbidden";
|
37
|
+
};
|
38
|
+
readonly NOT_FOUND: {
|
39
|
+
readonly status: 404;
|
40
|
+
readonly message: "Not Found";
|
41
|
+
};
|
42
|
+
readonly METHOD_NOT_SUPPORTED: {
|
43
|
+
readonly status: 405;
|
44
|
+
readonly message: "Method Not Supported";
|
45
|
+
};
|
46
|
+
readonly NOT_ACCEPTABLE: {
|
47
|
+
readonly status: 406;
|
48
|
+
readonly message: "Not Acceptable";
|
49
|
+
};
|
50
|
+
readonly TIMEOUT: {
|
51
|
+
readonly status: 408;
|
52
|
+
readonly message: "Request Timeout";
|
53
|
+
};
|
54
|
+
readonly CONFLICT: {
|
55
|
+
readonly status: 409;
|
56
|
+
readonly message: "Conflict";
|
57
|
+
};
|
58
|
+
readonly PRECONDITION_FAILED: {
|
59
|
+
readonly status: 412;
|
60
|
+
readonly message: "Precondition Failed";
|
61
|
+
};
|
62
|
+
readonly PAYLOAD_TOO_LARGE: {
|
63
|
+
readonly status: 413;
|
64
|
+
readonly message: "Payload Too Large";
|
65
|
+
};
|
66
|
+
readonly UNSUPPORTED_MEDIA_TYPE: {
|
67
|
+
readonly status: 415;
|
68
|
+
readonly message: "Unsupported Media Type";
|
69
|
+
};
|
70
|
+
readonly UNPROCESSABLE_CONTENT: {
|
71
|
+
readonly status: 422;
|
72
|
+
readonly message: "Unprocessable Content";
|
73
|
+
};
|
74
|
+
readonly TOO_MANY_REQUESTS: {
|
75
|
+
readonly status: 429;
|
76
|
+
readonly message: "Too Many Requests";
|
77
|
+
};
|
78
|
+
readonly CLIENT_CLOSED_REQUEST: {
|
79
|
+
readonly status: 499;
|
80
|
+
readonly message: "Client Closed Request";
|
81
|
+
};
|
82
|
+
readonly INTERNAL_SERVER_ERROR: {
|
83
|
+
readonly status: 500;
|
84
|
+
readonly message: "Internal Server Error";
|
85
|
+
};
|
86
|
+
readonly NOT_IMPLEMENTED: {
|
87
|
+
readonly status: 501;
|
88
|
+
readonly message: "Not Implemented";
|
89
|
+
};
|
90
|
+
readonly BAD_GATEWAY: {
|
91
|
+
readonly status: 502;
|
92
|
+
readonly message: "Bad Gateway";
|
93
|
+
};
|
94
|
+
readonly SERVICE_UNAVAILABLE: {
|
95
|
+
readonly status: 503;
|
96
|
+
readonly message: "Service Unavailable";
|
97
|
+
};
|
98
|
+
readonly GATEWAY_TIMEOUT: {
|
99
|
+
readonly status: 504;
|
100
|
+
readonly message: "Gateway Timeout";
|
101
|
+
};
|
102
|
+
};
|
103
|
+
type CommonORPCErrorCode = keyof typeof COMMON_ORPC_ERROR_DEFS;
|
104
|
+
type ORPCErrorCode = CommonORPCErrorCode | (string & {});
|
105
|
+
declare function fallbackORPCErrorStatus(code: ORPCErrorCode, status: number | undefined): number;
|
106
|
+
declare function fallbackORPCErrorMessage(code: ORPCErrorCode, message: string | undefined): string;
|
107
|
+
type ORPCErrorOptions<TData> = ErrorOptions & {
|
108
|
+
defined?: boolean;
|
109
|
+
status?: number;
|
110
|
+
message?: string;
|
111
|
+
} & (undefined extends TData ? {
|
112
|
+
data?: TData;
|
113
|
+
} : {
|
114
|
+
data: TData;
|
115
|
+
});
|
116
|
+
declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
|
117
|
+
readonly defined: boolean;
|
118
|
+
readonly code: TCode;
|
119
|
+
readonly status: number;
|
120
|
+
readonly data: TData;
|
121
|
+
constructor(code: TCode, ...[options]: MaybeOptionalOptions<ORPCErrorOptions<TData>>);
|
122
|
+
toJSON(): ORPCErrorJSON<TCode, TData>;
|
123
|
+
static fromJSON<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
|
124
|
+
static isValidJSON(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
|
125
|
+
}
|
126
|
+
type ORPCErrorJSON<TCode extends string, TData> = Pick<ORPCError<TCode, TData>, 'defined' | 'code' | 'status' | 'message' | 'data'>;
|
127
|
+
declare function isDefinedError<T>(error: T): error is Extract<T, ORPCError<any, any>>;
|
128
|
+
declare function toORPCError(error: unknown): ORPCError<any, any>;
|
129
|
+
|
130
|
+
declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
|
131
|
+
value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
|
132
|
+
error: (error: unknown) => Promise<unknown>;
|
133
|
+
}): AsyncGenerator<TMap, TMap, TNext>;
|
134
|
+
|
135
|
+
type SafeResult<TOutput, TError extends Error> = [error: null, data: TOutput, isDefined: false] & {
|
136
|
+
error: null;
|
137
|
+
data: TOutput;
|
138
|
+
isDefined: false;
|
139
|
+
} | [error: Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false] & {
|
140
|
+
error: Exclude<TError, ORPCError<any, any>>;
|
141
|
+
data: undefined;
|
142
|
+
isDefined: false;
|
143
|
+
} | [error: Extract<TError, ORPCError<any, any>>, data: undefined, isDefined: true] & {
|
144
|
+
error: Extract<TError, ORPCError<any, any>>;
|
145
|
+
data: undefined;
|
146
|
+
isDefined: true;
|
147
|
+
};
|
148
|
+
declare function safe<TOutput, TError extends Error>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
|
149
|
+
|
150
|
+
export { COMMON_ORPC_ERROR_DEFS, ClientContext, ClientLink, ClientOptionsOut, ClientPromiseResult, type CommonORPCErrorCode, DynamicLink, InferClientContext, NestedClient, ORPCError, type ORPCErrorCode, type ORPCErrorJSON, type ORPCErrorOptions, type SafeResult, createORPCClient, type createORPCClientOptions, fallbackORPCErrorMessage, fallbackORPCErrorStatus, isDefinedError, mapEventIterator, safe, toORPCError };
|
package/dist/index.mjs
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
import { i as isDefinedError } from './shared/client.BacCdg3F.mjs';
|
2
|
+
export { C as COMMON_ORPC_ERROR_DEFS, O as ORPCError, a as fallbackORPCErrorMessage, f as fallbackORPCErrorStatus, m as mapEventIterator, t as toORPCError } from './shared/client.BacCdg3F.mjs';
|
3
|
+
export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
4
|
+
export { ErrorEvent } from '@orpc/standard-server';
|
5
|
+
|
6
|
+
function createORPCClient(link, options) {
|
7
|
+
const path = options?.path ?? [];
|
8
|
+
const procedureClient = async (...[input, options2]) => {
|
9
|
+
const optionsOut = {
|
10
|
+
...options2,
|
11
|
+
context: options2?.context ?? {}
|
12
|
+
// options.context can be undefined when all field is optional
|
13
|
+
};
|
14
|
+
return await link.call(path, input, optionsOut);
|
15
|
+
};
|
16
|
+
const recursive = new Proxy(procedureClient, {
|
17
|
+
get(target, key) {
|
18
|
+
if (typeof key !== "string") {
|
19
|
+
return Reflect.get(target, key);
|
20
|
+
}
|
21
|
+
return createORPCClient(link, {
|
22
|
+
...options,
|
23
|
+
path: [...path, key]
|
24
|
+
});
|
25
|
+
}
|
26
|
+
});
|
27
|
+
return recursive;
|
28
|
+
}
|
29
|
+
|
30
|
+
class DynamicLink {
|
31
|
+
constructor(linkResolver) {
|
32
|
+
this.linkResolver = linkResolver;
|
33
|
+
}
|
34
|
+
async call(path, input, options) {
|
35
|
+
const resolvedLink = await this.linkResolver(options, path, input);
|
36
|
+
const output = await resolvedLink.call(path, input, options);
|
37
|
+
return output;
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
async function safe(promise) {
|
42
|
+
try {
|
43
|
+
const output = await promise;
|
44
|
+
return Object.assign(
|
45
|
+
[null, output, false],
|
46
|
+
{ error: null, data: output, isDefined: false }
|
47
|
+
);
|
48
|
+
} catch (e) {
|
49
|
+
const error = e;
|
50
|
+
if (isDefinedError(error)) {
|
51
|
+
return Object.assign(
|
52
|
+
[error, void 0, true],
|
53
|
+
{ error, data: void 0, isDefined: true }
|
54
|
+
);
|
55
|
+
}
|
56
|
+
return Object.assign(
|
57
|
+
[error, void 0, false],
|
58
|
+
{ error, data: void 0, isDefined: false }
|
59
|
+
);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
export { DynamicLink, createORPCClient, isDefinedError, safe };
|
@@ -0,0 +1,62 @@
|
|
1
|
+
import { C as ClientPlugin, S as StandardLinkOptions } from '../shared/client.DrOAzyMB.mjs';
|
2
|
+
export { a as CompositeClientPlugin } from '../shared/client.DrOAzyMB.mjs';
|
3
|
+
import { Value } from '@orpc/shared';
|
4
|
+
import { C as ClientOptionsOut, a as ClientContext } from '../shared/client.CupM8eRP.mjs';
|
5
|
+
import '@orpc/standard-server';
|
6
|
+
|
7
|
+
interface ClientRetryPluginAttemptOptions {
|
8
|
+
eventIteratorLastRetry: number | undefined;
|
9
|
+
eventIteratorLastEventId: string | undefined;
|
10
|
+
attemptIndex: number;
|
11
|
+
error: unknown;
|
12
|
+
}
|
13
|
+
interface ClientRetryPluginContext {
|
14
|
+
/**
|
15
|
+
* Maximum retry attempts before throwing
|
16
|
+
* Use `Number.POSITIVE_INFINITY` for infinite retries (e.g., when handling Server-Sent Events).
|
17
|
+
*
|
18
|
+
* @default 0
|
19
|
+
*/
|
20
|
+
retry?: number;
|
21
|
+
/**
|
22
|
+
* Delay (in ms) before retrying.
|
23
|
+
*
|
24
|
+
* @default (o) => o.eventIteratorLastRetry ?? 2000
|
25
|
+
*/
|
26
|
+
retryDelay?: Value<number, [
|
27
|
+
attemptOptions: ClientRetryPluginAttemptOptions,
|
28
|
+
clientOptions: ClientOptionsOut<ClientRetryPluginContext>,
|
29
|
+
path: readonly string[],
|
30
|
+
input: unknown
|
31
|
+
]>;
|
32
|
+
/**
|
33
|
+
* Determine should retry or not.
|
34
|
+
*
|
35
|
+
* @default true
|
36
|
+
*/
|
37
|
+
shouldRetry?: Value<boolean, [
|
38
|
+
attemptOptions: ClientRetryPluginAttemptOptions,
|
39
|
+
clientOptions: ClientOptionsOut<ClientRetryPluginContext>,
|
40
|
+
path: readonly string[],
|
41
|
+
input: unknown
|
42
|
+
]>;
|
43
|
+
/**
|
44
|
+
* The hook called when retrying, and return the unsubscribe function.
|
45
|
+
*/
|
46
|
+
onRetry?: (options: ClientRetryPluginAttemptOptions, clientOptions: ClientOptionsOut<ClientRetryPluginContext>, path: readonly string[], input: unknown) => void | (() => void);
|
47
|
+
}
|
48
|
+
declare class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error {
|
49
|
+
}
|
50
|
+
interface ClientRetryPluginOptions {
|
51
|
+
default?: ClientRetryPluginContext;
|
52
|
+
}
|
53
|
+
declare class ClientRetryPlugin<T extends ClientContext & ClientRetryPluginContext> implements ClientPlugin<T> {
|
54
|
+
private readonly defaultRetry;
|
55
|
+
private readonly defaultRetryDelay;
|
56
|
+
private readonly defaultShouldRetry;
|
57
|
+
private readonly defaultOnRetry;
|
58
|
+
constructor(options?: ClientRetryPluginOptions);
|
59
|
+
init(options: StandardLinkOptions<T>): void;
|
60
|
+
}
|
61
|
+
|
62
|
+
export { ClientPlugin, ClientRetryPlugin, type ClientRetryPluginAttemptOptions, type ClientRetryPluginContext, ClientRetryPluginInvalidEventIteratorRetryResponse, type ClientRetryPluginOptions };
|
@@ -0,0 +1,62 @@
|
|
1
|
+
import { C as ClientPlugin, S as StandardLinkOptions } from '../shared/client.aGal-uGY.js';
|
2
|
+
export { a as CompositeClientPlugin } from '../shared/client.aGal-uGY.js';
|
3
|
+
import { Value } from '@orpc/shared';
|
4
|
+
import { C as ClientOptionsOut, a as ClientContext } from '../shared/client.CupM8eRP.js';
|
5
|
+
import '@orpc/standard-server';
|
6
|
+
|
7
|
+
interface ClientRetryPluginAttemptOptions {
|
8
|
+
eventIteratorLastRetry: number | undefined;
|
9
|
+
eventIteratorLastEventId: string | undefined;
|
10
|
+
attemptIndex: number;
|
11
|
+
error: unknown;
|
12
|
+
}
|
13
|
+
interface ClientRetryPluginContext {
|
14
|
+
/**
|
15
|
+
* Maximum retry attempts before throwing
|
16
|
+
* Use `Number.POSITIVE_INFINITY` for infinite retries (e.g., when handling Server-Sent Events).
|
17
|
+
*
|
18
|
+
* @default 0
|
19
|
+
*/
|
20
|
+
retry?: number;
|
21
|
+
/**
|
22
|
+
* Delay (in ms) before retrying.
|
23
|
+
*
|
24
|
+
* @default (o) => o.eventIteratorLastRetry ?? 2000
|
25
|
+
*/
|
26
|
+
retryDelay?: Value<number, [
|
27
|
+
attemptOptions: ClientRetryPluginAttemptOptions,
|
28
|
+
clientOptions: ClientOptionsOut<ClientRetryPluginContext>,
|
29
|
+
path: readonly string[],
|
30
|
+
input: unknown
|
31
|
+
]>;
|
32
|
+
/**
|
33
|
+
* Determine should retry or not.
|
34
|
+
*
|
35
|
+
* @default true
|
36
|
+
*/
|
37
|
+
shouldRetry?: Value<boolean, [
|
38
|
+
attemptOptions: ClientRetryPluginAttemptOptions,
|
39
|
+
clientOptions: ClientOptionsOut<ClientRetryPluginContext>,
|
40
|
+
path: readonly string[],
|
41
|
+
input: unknown
|
42
|
+
]>;
|
43
|
+
/**
|
44
|
+
* The hook called when retrying, and return the unsubscribe function.
|
45
|
+
*/
|
46
|
+
onRetry?: (options: ClientRetryPluginAttemptOptions, clientOptions: ClientOptionsOut<ClientRetryPluginContext>, path: readonly string[], input: unknown) => void | (() => void);
|
47
|
+
}
|
48
|
+
declare class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error {
|
49
|
+
}
|
50
|
+
interface ClientRetryPluginOptions {
|
51
|
+
default?: ClientRetryPluginContext;
|
52
|
+
}
|
53
|
+
declare class ClientRetryPlugin<T extends ClientContext & ClientRetryPluginContext> implements ClientPlugin<T> {
|
54
|
+
private readonly defaultRetry;
|
55
|
+
private readonly defaultRetryDelay;
|
56
|
+
private readonly defaultShouldRetry;
|
57
|
+
private readonly defaultOnRetry;
|
58
|
+
constructor(options?: ClientRetryPluginOptions);
|
59
|
+
init(options: StandardLinkOptions<T>): void;
|
60
|
+
}
|
61
|
+
|
62
|
+
export { ClientPlugin, ClientRetryPlugin, type ClientRetryPluginAttemptOptions, type ClientRetryPluginContext, ClientRetryPluginInvalidEventIteratorRetryResponse, type ClientRetryPluginOptions };
|
@@ -0,0 +1,127 @@
|
|
1
|
+
export { C as CompositeClientPlugin } from '../shared/client.CvnV7_uV.mjs';
|
2
|
+
import { isAsyncIteratorObject, value } from '@orpc/shared';
|
3
|
+
import { getEventMeta } from '@orpc/standard-server';
|
4
|
+
|
5
|
+
class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error {
|
6
|
+
}
|
7
|
+
class ClientRetryPlugin {
|
8
|
+
defaultRetry;
|
9
|
+
defaultRetryDelay;
|
10
|
+
defaultShouldRetry;
|
11
|
+
defaultOnRetry;
|
12
|
+
constructor(options = {}) {
|
13
|
+
this.defaultRetry = options.default?.retry ?? 0;
|
14
|
+
this.defaultRetryDelay = options.default?.retryDelay ?? ((o) => o.eventIteratorLastRetry ?? 2e3);
|
15
|
+
this.defaultShouldRetry = options.default?.shouldRetry ?? true;
|
16
|
+
this.defaultOnRetry = options.default?.onRetry;
|
17
|
+
}
|
18
|
+
init(options) {
|
19
|
+
options.interceptors ??= [];
|
20
|
+
options.interceptors.push(async (interceptorOptions) => {
|
21
|
+
const maxAttempts = interceptorOptions.options.context.retry ?? this.defaultRetry;
|
22
|
+
const retryDelay = interceptorOptions.options.context.retryDelay ?? this.defaultRetryDelay;
|
23
|
+
const shouldRetry = interceptorOptions.options.context.shouldRetry ?? this.defaultShouldRetry;
|
24
|
+
const onRetry = interceptorOptions.options.context.onRetry ?? this.defaultOnRetry;
|
25
|
+
if (maxAttempts <= 0) {
|
26
|
+
return interceptorOptions.next();
|
27
|
+
}
|
28
|
+
let eventIteratorLastEventId = interceptorOptions.options.lastEventId;
|
29
|
+
let eventIteratorLastRetry;
|
30
|
+
let unsubscribe;
|
31
|
+
let attemptIndex = 0;
|
32
|
+
const next = async (initial) => {
|
33
|
+
let current = initial;
|
34
|
+
while (true) {
|
35
|
+
if (current) {
|
36
|
+
if (attemptIndex >= maxAttempts) {
|
37
|
+
throw current.error;
|
38
|
+
}
|
39
|
+
const attemptOptions = {
|
40
|
+
attemptIndex,
|
41
|
+
error: current.error,
|
42
|
+
eventIteratorLastEventId,
|
43
|
+
eventIteratorLastRetry
|
44
|
+
};
|
45
|
+
const shouldRetryBool = await value(
|
46
|
+
shouldRetry,
|
47
|
+
attemptOptions,
|
48
|
+
interceptorOptions.options,
|
49
|
+
interceptorOptions.path,
|
50
|
+
interceptorOptions.input
|
51
|
+
);
|
52
|
+
if (!shouldRetryBool) {
|
53
|
+
throw current.error;
|
54
|
+
}
|
55
|
+
unsubscribe = onRetry?.(
|
56
|
+
attemptOptions,
|
57
|
+
interceptorOptions.options,
|
58
|
+
interceptorOptions.path,
|
59
|
+
interceptorOptions.input
|
60
|
+
);
|
61
|
+
const retryDelayMs = await value(
|
62
|
+
retryDelay,
|
63
|
+
attemptOptions,
|
64
|
+
interceptorOptions.options,
|
65
|
+
interceptorOptions.path,
|
66
|
+
interceptorOptions.input
|
67
|
+
);
|
68
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
|
69
|
+
attemptIndex++;
|
70
|
+
}
|
71
|
+
try {
|
72
|
+
const newClientOptions = { ...interceptorOptions.options, lastEventId: eventIteratorLastEventId };
|
73
|
+
const output2 = await interceptorOptions.next({
|
74
|
+
...interceptorOptions,
|
75
|
+
options: newClientOptions
|
76
|
+
});
|
77
|
+
return output2;
|
78
|
+
} catch (error) {
|
79
|
+
if (interceptorOptions.options.signal?.aborted === true) {
|
80
|
+
throw error;
|
81
|
+
}
|
82
|
+
current = { error };
|
83
|
+
} finally {
|
84
|
+
unsubscribe?.();
|
85
|
+
unsubscribe = void 0;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
};
|
89
|
+
const output = await next();
|
90
|
+
if (!isAsyncIteratorObject(output)) {
|
91
|
+
return output;
|
92
|
+
}
|
93
|
+
return async function* () {
|
94
|
+
let current = output;
|
95
|
+
try {
|
96
|
+
while (true) {
|
97
|
+
try {
|
98
|
+
const item = await current.next();
|
99
|
+
const meta = getEventMeta(item.value);
|
100
|
+
eventIteratorLastEventId = meta?.id ?? eventIteratorLastEventId;
|
101
|
+
eventIteratorLastRetry = meta?.retry ?? eventIteratorLastRetry;
|
102
|
+
if (item.done) {
|
103
|
+
return item.value;
|
104
|
+
}
|
105
|
+
yield item.value;
|
106
|
+
} catch (error) {
|
107
|
+
const meta = getEventMeta(error);
|
108
|
+
eventIteratorLastEventId = meta?.id ?? eventIteratorLastEventId;
|
109
|
+
eventIteratorLastRetry = meta?.retry ?? eventIteratorLastRetry;
|
110
|
+
const maybeEventIterator = await next({ error });
|
111
|
+
if (!isAsyncIteratorObject(maybeEventIterator)) {
|
112
|
+
throw new ClientRetryPluginInvalidEventIteratorRetryResponse(
|
113
|
+
"RetryPlugin: Expected an Event Iterator, got a non-Event Iterator"
|
114
|
+
);
|
115
|
+
}
|
116
|
+
current = maybeEventIterator;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
} finally {
|
120
|
+
await current.return?.();
|
121
|
+
}
|
122
|
+
}();
|
123
|
+
});
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
export { ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse };
|
@@ -0,0 +1,172 @@
|
|
1
|
+
import { isObject, isTypescriptObject } from '@orpc/shared';
|
2
|
+
import { getEventMeta, withEventMeta } from '@orpc/standard-server';
|
3
|
+
|
4
|
+
const COMMON_ORPC_ERROR_DEFS = {
|
5
|
+
BAD_REQUEST: {
|
6
|
+
status: 400,
|
7
|
+
message: "Bad Request"
|
8
|
+
},
|
9
|
+
UNAUTHORIZED: {
|
10
|
+
status: 401,
|
11
|
+
message: "Unauthorized"
|
12
|
+
},
|
13
|
+
FORBIDDEN: {
|
14
|
+
status: 403,
|
15
|
+
message: "Forbidden"
|
16
|
+
},
|
17
|
+
NOT_FOUND: {
|
18
|
+
status: 404,
|
19
|
+
message: "Not Found"
|
20
|
+
},
|
21
|
+
METHOD_NOT_SUPPORTED: {
|
22
|
+
status: 405,
|
23
|
+
message: "Method Not Supported"
|
24
|
+
},
|
25
|
+
NOT_ACCEPTABLE: {
|
26
|
+
status: 406,
|
27
|
+
message: "Not Acceptable"
|
28
|
+
},
|
29
|
+
TIMEOUT: {
|
30
|
+
status: 408,
|
31
|
+
message: "Request Timeout"
|
32
|
+
},
|
33
|
+
CONFLICT: {
|
34
|
+
status: 409,
|
35
|
+
message: "Conflict"
|
36
|
+
},
|
37
|
+
PRECONDITION_FAILED: {
|
38
|
+
status: 412,
|
39
|
+
message: "Precondition Failed"
|
40
|
+
},
|
41
|
+
PAYLOAD_TOO_LARGE: {
|
42
|
+
status: 413,
|
43
|
+
message: "Payload Too Large"
|
44
|
+
},
|
45
|
+
UNSUPPORTED_MEDIA_TYPE: {
|
46
|
+
status: 415,
|
47
|
+
message: "Unsupported Media Type"
|
48
|
+
},
|
49
|
+
UNPROCESSABLE_CONTENT: {
|
50
|
+
status: 422,
|
51
|
+
message: "Unprocessable Content"
|
52
|
+
},
|
53
|
+
TOO_MANY_REQUESTS: {
|
54
|
+
status: 429,
|
55
|
+
message: "Too Many Requests"
|
56
|
+
},
|
57
|
+
CLIENT_CLOSED_REQUEST: {
|
58
|
+
status: 499,
|
59
|
+
message: "Client Closed Request"
|
60
|
+
},
|
61
|
+
INTERNAL_SERVER_ERROR: {
|
62
|
+
status: 500,
|
63
|
+
message: "Internal Server Error"
|
64
|
+
},
|
65
|
+
NOT_IMPLEMENTED: {
|
66
|
+
status: 501,
|
67
|
+
message: "Not Implemented"
|
68
|
+
},
|
69
|
+
BAD_GATEWAY: {
|
70
|
+
status: 502,
|
71
|
+
message: "Bad Gateway"
|
72
|
+
},
|
73
|
+
SERVICE_UNAVAILABLE: {
|
74
|
+
status: 503,
|
75
|
+
message: "Service Unavailable"
|
76
|
+
},
|
77
|
+
GATEWAY_TIMEOUT: {
|
78
|
+
status: 504,
|
79
|
+
message: "Gateway Timeout"
|
80
|
+
}
|
81
|
+
};
|
82
|
+
function fallbackORPCErrorStatus(code, status) {
|
83
|
+
return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
|
84
|
+
}
|
85
|
+
function fallbackORPCErrorMessage(code, message) {
|
86
|
+
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
87
|
+
}
|
88
|
+
class ORPCError extends Error {
|
89
|
+
defined;
|
90
|
+
code;
|
91
|
+
status;
|
92
|
+
data;
|
93
|
+
constructor(code, ...[options]) {
|
94
|
+
if (options?.status && (options.status < 400 || options.status >= 600)) {
|
95
|
+
throw new Error("[ORPCError] The error status code must be in the 400-599 range.");
|
96
|
+
}
|
97
|
+
const message = fallbackORPCErrorMessage(code, options?.message);
|
98
|
+
super(message, options);
|
99
|
+
this.code = code;
|
100
|
+
this.status = fallbackORPCErrorStatus(code, options?.status);
|
101
|
+
this.defined = options?.defined ?? false;
|
102
|
+
this.data = options?.data;
|
103
|
+
}
|
104
|
+
toJSON() {
|
105
|
+
return {
|
106
|
+
defined: this.defined,
|
107
|
+
code: this.code,
|
108
|
+
status: this.status,
|
109
|
+
message: this.message,
|
110
|
+
data: this.data
|
111
|
+
};
|
112
|
+
}
|
113
|
+
static fromJSON(json, options) {
|
114
|
+
return new ORPCError(json.code, {
|
115
|
+
...options,
|
116
|
+
...json
|
117
|
+
});
|
118
|
+
}
|
119
|
+
static isValidJSON(json) {
|
120
|
+
if (!isObject(json)) {
|
121
|
+
return false;
|
122
|
+
}
|
123
|
+
const validKeys = ["defined", "code", "status", "message", "data"];
|
124
|
+
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
125
|
+
return false;
|
126
|
+
}
|
127
|
+
return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string";
|
128
|
+
}
|
129
|
+
}
|
130
|
+
function isDefinedError(error) {
|
131
|
+
return error instanceof ORPCError && error.defined;
|
132
|
+
}
|
133
|
+
function toORPCError(error) {
|
134
|
+
return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
|
135
|
+
message: "Internal server error",
|
136
|
+
cause: error
|
137
|
+
});
|
138
|
+
}
|
139
|
+
|
140
|
+
function mapEventIterator(iterator, maps) {
|
141
|
+
return async function* () {
|
142
|
+
try {
|
143
|
+
while (true) {
|
144
|
+
const { done, value } = await iterator.next();
|
145
|
+
let mappedValue = await maps.value(value, done);
|
146
|
+
if (mappedValue !== value) {
|
147
|
+
const meta = getEventMeta(value);
|
148
|
+
if (meta && isTypescriptObject(mappedValue)) {
|
149
|
+
mappedValue = withEventMeta(mappedValue, meta);
|
150
|
+
}
|
151
|
+
}
|
152
|
+
if (done) {
|
153
|
+
return mappedValue;
|
154
|
+
}
|
155
|
+
yield mappedValue;
|
156
|
+
}
|
157
|
+
} catch (error) {
|
158
|
+
let mappedError = await maps.error(error);
|
159
|
+
if (mappedError !== error) {
|
160
|
+
const meta = getEventMeta(error);
|
161
|
+
if (meta && isTypescriptObject(mappedError)) {
|
162
|
+
mappedError = withEventMeta(mappedError, meta);
|
163
|
+
}
|
164
|
+
}
|
165
|
+
throw mappedError;
|
166
|
+
} finally {
|
167
|
+
await iterator.return?.();
|
168
|
+
}
|
169
|
+
}();
|
170
|
+
}
|
171
|
+
|
172
|
+
export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, toORPCError as t };
|