@orpc/client 2.0.0-beta.46 → 2.0.0-beta.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/dist/adapters/fetch/index.d.mts +2 -48
- package/dist/adapters/fetch/index.mjs +2 -77
- package/dist/adapters/fetch/rpc-link.d.mts +13 -0
- package/dist/adapters/fetch/rpc-link.mjs +10 -0
- package/dist/adapters/fetch/transport.d.mts +31 -0
- package/dist/adapters/fetch/transport.mjs +65 -0
- package/dist/adapters/message-port/index.d.mts +3 -80
- package/dist/adapters/message-port/index.mjs +3 -101
- package/dist/adapters/message-port/message-port.d.mts +29 -0
- package/dist/adapters/message-port/message-port.mjs +43 -0
- package/dist/adapters/message-port/rpc-link.d.mts +14 -0
- package/dist/adapters/message-port/rpc-link.mjs +10 -0
- package/dist/adapters/message-port/transport.d.mts +35 -0
- package/dist/adapters/message-port/transport.mjs +47 -0
- package/dist/adapters/standard/codec.d.mts +14 -0
- package/dist/adapters/standard/codec.mjs +1 -0
- package/dist/adapters/standard/index.d.mts +5 -60
- package/dist/adapters/standard/index.mjs +4 -6
- package/dist/adapters/standard/link.d.mts +40 -0
- package/dist/adapters/standard/link.mjs +79 -0
- package/dist/adapters/standard/plugin.d.mts +32 -0
- package/dist/adapters/standard/plugin.mjs +16 -0
- package/dist/adapters/standard/rpc-link-codec.d.mts +55 -0
- package/dist/adapters/standard/rpc-link-codec.mjs +104 -0
- package/dist/adapters/standard/transport.d.mts +14 -0
- package/dist/adapters/standard/transport.mjs +1 -0
- package/dist/adapters/websocket/index.d.mts +2 -136
- package/dist/adapters/websocket/index.mjs +2 -131
- package/dist/adapters/websocket/rpc-link.d.mts +13 -0
- package/dist/adapters/websocket/rpc-link.mjs +10 -0
- package/dist/adapters/websocket/transport.d.mts +119 -0
- package/dist/adapters/websocket/transport.mjs +123 -0
- package/dist/async-iterator-object.d.mts +7 -0
- package/dist/async-iterator-object.mjs +30 -0
- package/dist/client-safe.d.mts +16 -0
- package/dist/client-safe.mjs +23 -0
- package/dist/client.d.mts +51 -0
- package/dist/client.mjs +32 -0
- package/dist/consts.d.mts +10 -0
- package/dist/consts.mjs +10 -0
- package/dist/dynamic-link.d.mts +13 -0
- package/dist/dynamic-link.mjs +12 -0
- package/dist/error-utils.d.mts +24 -0
- package/dist/error-utils.mjs +44 -0
- package/dist/error.d.mts +138 -0
- package/dist/error.mjs +105 -0
- package/dist/index.d.mts +42 -137
- package/dist/index.mjs +18 -142
- package/dist/plugins/batch.d.mts +150 -0
- package/dist/plugins/batch.mjs +339 -0
- package/dist/plugins/cookie-jar.d.mts +82 -0
- package/dist/plugins/cookie-jar.mjs +229 -0
- package/dist/plugins/dedupe.d.mts +58 -0
- package/dist/plugins/dedupe.mjs +199 -0
- package/dist/plugins/index.d.mts +28 -420
- package/dist/plugins/index.mjs +17 -971
- package/dist/plugins/request-compression.d.mts +44 -0
- package/dist/plugins/request-compression.mjs +150 -0
- package/dist/plugins/response-compression.d.mts +28 -0
- package/dist/plugins/response-compression.mjs +78 -0
- package/dist/plugins/retry-after.d.mts +41 -0
- package/dist/plugins/retry-after.mjs +71 -0
- package/dist/plugins/retry.d.mts +82 -0
- package/dist/plugins/retry.mjs +127 -0
- package/dist/plugins/timeout.d.mts +25 -0
- package/dist/plugins/timeout.mjs +36 -0
- package/dist/rpc-json-serializer.d.mts +99 -0
- package/dist/rpc-json-serializer.mjs +365 -0
- package/dist/rpc-serializer.d.mts +32 -0
- package/dist/rpc-serializer.mjs +100 -0
- package/dist/types.d.mts +93 -0
- package/dist/types.mjs +1 -0
- package/dist/utils.d.mts +46 -0
- package/dist/utils.mjs +53 -0
- package/package.json +5 -5
- package/dist/adapters/fetch/index.d.ts +0 -48
- package/dist/adapters/message-port/index.d.ts +0 -80
- package/dist/adapters/standard/index.d.ts +0 -60
- package/dist/adapters/websocket/index.d.ts +0 -136
- package/dist/index.d.ts +0 -144
- package/dist/plugins/index.d.ts +0 -420
- package/dist/shared/client.BoHJeq15.d.mts +0 -233
- package/dist/shared/client.BoHJeq15.d.ts +0 -233
- package/dist/shared/client.CKQeJJ2U.mjs +0 -172
- package/dist/shared/client.DqIWeNhQ.d.mts +0 -131
- package/dist/shared/client.DqIWeNhQ.d.ts +0 -131
- package/dist/shared/client.JBJwBhR8.d.ts +0 -90
- package/dist/shared/client.LF04eOvL.mjs +0 -115
- package/dist/shared/client._wG5INmC.mjs +0 -505
- package/dist/shared/client.euYjjcFz.d.mts +0 -90
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isPlainObject } from "@orpc/shared";
|
|
2
|
+
import { ORPCError } from "./error.mjs";
|
|
3
|
+
|
|
4
|
+
export function isDefinedError(error) {
|
|
5
|
+
return error instanceof ORPCError && error.defined;
|
|
6
|
+
}
|
|
7
|
+
export function toORPCError(error) {
|
|
8
|
+
return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", { cause: error });
|
|
9
|
+
}
|
|
10
|
+
export function isORPCErrorJson(json) {
|
|
11
|
+
if (!isPlainObject(json)) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
const validKeys = [
|
|
15
|
+
"defined",
|
|
16
|
+
"code",
|
|
17
|
+
"message",
|
|
18
|
+
"data"
|
|
19
|
+
];
|
|
20
|
+
if (Object.keys(json).some((k) => !validKeys.includes(k))) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "message" in json && typeof json.message === "string";
|
|
24
|
+
}
|
|
25
|
+
export function createORPCErrorFromJson(json, options = {}) {
|
|
26
|
+
const error = new ORPCError(json.code, {
|
|
27
|
+
...json,
|
|
28
|
+
...options
|
|
29
|
+
});
|
|
30
|
+
error.defined = json.defined;
|
|
31
|
+
return error;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function cloneORPCError(error) {
|
|
35
|
+
const cloned = new ORPCError(error.code, {
|
|
36
|
+
message: error.message,
|
|
37
|
+
data: error.data,
|
|
38
|
+
cause: error.cause
|
|
39
|
+
});
|
|
40
|
+
Object.setPrototypeOf(cloned, Object.getPrototypeOf(error));
|
|
41
|
+
Object.defineProperties(cloned, Object.getOwnPropertyDescriptors(error));
|
|
42
|
+
cloned.stack = error.stack;
|
|
43
|
+
return cloned;
|
|
44
|
+
}
|
package/dist/error.d.mts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { MaybeOptionalOptions, Registry } from "@orpc/shared";
|
|
2
|
+
import type { StandardResponse } from "@standard-server/core";
|
|
3
|
+
/**
|
|
4
|
+
* Default mapping between common oRPC error codes and HTTP status codes.
|
|
5
|
+
* Handlers use it to determine response status codes; spread it to build a custom `errorStatusMap`.
|
|
6
|
+
*
|
|
7
|
+
* @see {@link https://orpc.dev/docs/rpc/handler#custom-error-response | RPC Handler - Custom Error Response}
|
|
8
|
+
* @see {@link https://orpc.dev/docs/openapi/handler#custom-error-response | OpenAPI Handler - Custom Error Response}
|
|
9
|
+
*/
|
|
10
|
+
export declare const COMMON_ERROR_STATUS_MAP: {
|
|
11
|
+
BAD_REQUEST: number;
|
|
12
|
+
UNAUTHORIZED: number;
|
|
13
|
+
PAYMENT_REQUIRED: number;
|
|
14
|
+
FORBIDDEN: number;
|
|
15
|
+
NOT_FOUND: number;
|
|
16
|
+
METHOD_NOT_SUPPORTED: number;
|
|
17
|
+
NOT_ACCEPTABLE: number;
|
|
18
|
+
TIMEOUT: number;
|
|
19
|
+
CONFLICT: number;
|
|
20
|
+
GONE: number;
|
|
21
|
+
LENGTH_REQUIRED: number;
|
|
22
|
+
PRECONDITION_FAILED: number;
|
|
23
|
+
PAYLOAD_TOO_LARGE: number;
|
|
24
|
+
UNSUPPORTED_MEDIA_TYPE: number;
|
|
25
|
+
UNPROCESSABLE_CONTENT: number;
|
|
26
|
+
PRECONDITION_REQUIRED: number;
|
|
27
|
+
TOO_MANY_REQUESTS: number;
|
|
28
|
+
CLIENT_CLOSED_REQUEST: number;
|
|
29
|
+
INTERNAL_SERVER_ERROR: number;
|
|
30
|
+
NOT_IMPLEMENTED: number;
|
|
31
|
+
BAD_GATEWAY: number;
|
|
32
|
+
SERVICE_UNAVAILABLE: number;
|
|
33
|
+
GATEWAY_TIMEOUT: number;
|
|
34
|
+
};
|
|
35
|
+
export type ORPCErrorCode = Registry extends {
|
|
36
|
+
ORPCErrorCode: infer T extends string;
|
|
37
|
+
} ? T : keyof typeof COMMON_ERROR_STATUS_MAP | string & {};
|
|
38
|
+
export type ORPCErrorOptions<TData> = ErrorOptions & {
|
|
39
|
+
message?: string | undefined;
|
|
40
|
+
} & (undefined extends TData ? {
|
|
41
|
+
data?: TData;
|
|
42
|
+
} : {
|
|
43
|
+
data: TData;
|
|
44
|
+
});
|
|
45
|
+
/**
|
|
46
|
+
* Typed error carrying a `code`, a `message`, and optional `data`.
|
|
47
|
+
* Throw it from handlers or middleware to produce typed error responses on the client.
|
|
48
|
+
*
|
|
49
|
+
* @see {@link https://orpc.dev/docs/error-handling#orpcerror-class | Error Handling - ORPCError Class}
|
|
50
|
+
*/
|
|
51
|
+
export declare class ORPCError<
|
|
52
|
+
TCode extends ORPCErrorCode,
|
|
53
|
+
TData
|
|
54
|
+
> extends Error {
|
|
55
|
+
/**
|
|
56
|
+
* @remarks
|
|
57
|
+
* **Note**: The `__branch` property is used for type branding, helping TypeScript distinguish
|
|
58
|
+
* an `ORPCError` instance from plain objects with a similar structure.
|
|
59
|
+
*/
|
|
60
|
+
override readonly name: "ORPCError" & {
|
|
61
|
+
__branch: "ORPCError";
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Indicates whether the error matches a definition in the procedure's `.errors` map,
|
|
65
|
+
* which makes its type inferable on the client.
|
|
66
|
+
*/
|
|
67
|
+
readonly defined: boolean;
|
|
68
|
+
code: TCode;
|
|
69
|
+
data: TData;
|
|
70
|
+
constructor(code: TCode, ...rest: MaybeOptionalOptions<ORPCErrorOptions<TData>>);
|
|
71
|
+
toJSON(): ORPCErrorJSON<TCode, TData>;
|
|
72
|
+
/**
|
|
73
|
+
* Workaround for Next.js where different contexts use separate
|
|
74
|
+
* dependency graphs, causing multiple ORPCError constructors existing and breaking
|
|
75
|
+
* `instanceof` checks across contexts.
|
|
76
|
+
*
|
|
77
|
+
* This is particularly problematic with "Optimized SSR", where orpc-client
|
|
78
|
+
* executes in one context but is invoked from another. When an error is thrown
|
|
79
|
+
* in the execution context, `instanceof ORPCError` checks fail in the
|
|
80
|
+
* invocation context due to separate class constructors.
|
|
81
|
+
*
|
|
82
|
+
* @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
|
|
83
|
+
*/
|
|
84
|
+
static override [Symbol.hasInstance](instance: unknown): boolean;
|
|
85
|
+
}
|
|
86
|
+
export interface ORPCErrorJSON<
|
|
87
|
+
TCode extends string,
|
|
88
|
+
TData
|
|
89
|
+
> extends Pick<ORPCError<TCode, TData>, "code" | "message" | "data"> {
|
|
90
|
+
/**
|
|
91
|
+
* remove readonly
|
|
92
|
+
*/
|
|
93
|
+
defined: boolean;
|
|
94
|
+
}
|
|
95
|
+
export type AnyORPCError = ORPCError<any, any>;
|
|
96
|
+
export type AnyORPCErrorJSON = ORPCErrorJSON<any, any>;
|
|
97
|
+
export interface ResponseErrorOptions extends ErrorOptions {
|
|
98
|
+
/**
|
|
99
|
+
* @default inferred from the response status
|
|
100
|
+
*/
|
|
101
|
+
message?: string | undefined;
|
|
102
|
+
response: StandardResponse;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Error carrying the resolved response a link received. Links set it as the `cause`
|
|
106
|
+
* of an `ORPCError` decoded from an error response, so details that are not part
|
|
107
|
+
* of the error itself, such as its status or headers, stay available.
|
|
108
|
+
*
|
|
109
|
+
* @see {@link https://orpc.dev/docs/rpc/link#error-responses | RPC Link - Error Responses}
|
|
110
|
+
* @see {@link https://orpc.dev/docs/openapi/link#error-responses | OpenAPI Link - Error Responses}
|
|
111
|
+
*/
|
|
112
|
+
export declare class ResponseError extends Error {
|
|
113
|
+
override readonly name: string;
|
|
114
|
+
response: StandardResponse;
|
|
115
|
+
constructor(options: ResponseErrorOptions);
|
|
116
|
+
}
|
|
117
|
+
export interface MalformedResponseErrorOptions extends ResponseErrorOptions {
|
|
118
|
+
/**
|
|
119
|
+
* @default inferred from the response body or status
|
|
120
|
+
*/
|
|
121
|
+
message?: string | undefined;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Error thrown by links when a response does not follow the expected oRPC format,
|
|
125
|
+
* for example when a proxy or gateway answers instead of your handler. Extends
|
|
126
|
+
* `ResponseError`, so it carries the resolved response. Unless overridden via
|
|
127
|
+
* `options.message`, the message is inferred from the response body or status.
|
|
128
|
+
*
|
|
129
|
+
* Unlike `ORPCError`, it reaches a handler's callers as a generic `INTERNAL_SERVER_ERROR`
|
|
130
|
+
* when thrown from the handler, so an upstream's response is not sent to them.
|
|
131
|
+
*
|
|
132
|
+
* @see {@link https://orpc.dev/docs/rpc/link#malformed-responses | RPC Link - Malformed Responses}
|
|
133
|
+
* @see {@link https://orpc.dev/docs/openapi/link#malformed-responses | OpenAPI Link - Malformed Responses}
|
|
134
|
+
*/
|
|
135
|
+
export declare class MalformedResponseError extends ResponseError {
|
|
136
|
+
override readonly name = "MalformedResponseError";
|
|
137
|
+
constructor(options: MalformedResponseErrorOptions);
|
|
138
|
+
}
|
package/dist/error.mjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { getConstructors, isPlainObject, resolveMaybeOptionalOptions } from "@orpc/shared";
|
|
2
|
+
|
|
3
|
+
export const COMMON_ERROR_STATUS_MAP = {
|
|
4
|
+
BAD_REQUEST: 400,
|
|
5
|
+
UNAUTHORIZED: 401,
|
|
6
|
+
PAYMENT_REQUIRED: 402,
|
|
7
|
+
FORBIDDEN: 403,
|
|
8
|
+
NOT_FOUND: 404,
|
|
9
|
+
METHOD_NOT_SUPPORTED: 405,
|
|
10
|
+
NOT_ACCEPTABLE: 406,
|
|
11
|
+
TIMEOUT: 408,
|
|
12
|
+
CONFLICT: 409,
|
|
13
|
+
GONE: 410,
|
|
14
|
+
LENGTH_REQUIRED: 411,
|
|
15
|
+
PRECONDITION_FAILED: 412,
|
|
16
|
+
PAYLOAD_TOO_LARGE: 413,
|
|
17
|
+
UNSUPPORTED_MEDIA_TYPE: 415,
|
|
18
|
+
UNPROCESSABLE_CONTENT: 422,
|
|
19
|
+
PRECONDITION_REQUIRED: 428,
|
|
20
|
+
TOO_MANY_REQUESTS: 429,
|
|
21
|
+
CLIENT_CLOSED_REQUEST: 499,
|
|
22
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
23
|
+
NOT_IMPLEMENTED: 501,
|
|
24
|
+
BAD_GATEWAY: 502,
|
|
25
|
+
SERVICE_UNAVAILABLE: 503,
|
|
26
|
+
GATEWAY_TIMEOUT: 504
|
|
27
|
+
};
|
|
28
|
+
let ORPCErrorConstructors;
|
|
29
|
+
|
|
30
|
+
export class ORPCError extends Error {
|
|
31
|
+
|
|
32
|
+
static {
|
|
33
|
+
|
|
34
|
+
const ORPC_ERROR_CONSTRUCTORS_SYMBOL = Symbol.for("ORPC_ERROR_CONSTRUCTORS");
|
|
35
|
+
void (globalThis[ORPC_ERROR_CONSTRUCTORS_SYMBOL] ??= new WeakSet());
|
|
36
|
+
ORPCErrorConstructors = globalThis[ORPC_ERROR_CONSTRUCTORS_SYMBOL];
|
|
37
|
+
ORPCErrorConstructors.add(ORPCError);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
name = "ORPCError";
|
|
41
|
+
|
|
42
|
+
defined = false;
|
|
43
|
+
code;
|
|
44
|
+
data;
|
|
45
|
+
constructor(code, ...rest) {
|
|
46
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
47
|
+
const message = options.message ?? codeToMessage(code);
|
|
48
|
+
super(message, options);
|
|
49
|
+
this.code = code;
|
|
50
|
+
this.data = options.data;
|
|
51
|
+
}
|
|
52
|
+
toJSON() {
|
|
53
|
+
return {
|
|
54
|
+
defined: this.defined,
|
|
55
|
+
code: this.code,
|
|
56
|
+
message: this.message,
|
|
57
|
+
data: this.data
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static [Symbol.hasInstance](instance) {
|
|
62
|
+
if (!ORPCErrorConstructors.has(this)) {
|
|
63
|
+
|
|
64
|
+
return super[Symbol.hasInstance](instance);
|
|
65
|
+
}
|
|
66
|
+
for (const constructor of getConstructors(instance)) {
|
|
67
|
+
if (ORPCErrorConstructors.has(constructor)) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export class ResponseError extends Error {
|
|
76
|
+
name = "ResponseError";
|
|
77
|
+
response;
|
|
78
|
+
constructor(options) {
|
|
79
|
+
super(options.message ?? `Received a ${options.response.status} response.`, options);
|
|
80
|
+
this.response = options.response;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export class MalformedResponseError extends ResponseError {
|
|
85
|
+
name = "MalformedResponseError";
|
|
86
|
+
constructor(options) {
|
|
87
|
+
super({
|
|
88
|
+
...options,
|
|
89
|
+
message: options.message ?? inferMalformedResponseMessage(options.response)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const INFERRED_MESSAGE_MAX_LENGTH = 256;
|
|
95
|
+
function inferMalformedResponseMessage(response) {
|
|
96
|
+
const candidate = isPlainObject(response.body) ? response.body.message : response.body;
|
|
97
|
+
if (typeof candidate === "string" && candidate.length > 0 && candidate.length <= INFERRED_MESSAGE_MAX_LENGTH) {
|
|
98
|
+
return candidate;
|
|
99
|
+
}
|
|
100
|
+
const commonCode = Object.entries(COMMON_ERROR_STATUS_MAP).find(([, status]) => status === response.status)?.[0];
|
|
101
|
+
return commonCode ? codeToMessage(commonCode) : "Malformed oRPC Response";
|
|
102
|
+
}
|
|
103
|
+
function codeToMessage(code) {
|
|
104
|
+
return code.split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(" ");
|
|
105
|
+
}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,144 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
input: TInput;
|
|
13
|
-
}
|
|
14
|
-
type ORPCClientInterceptor<TClientContext extends ClientContext, TInput, TOutput, TError> = Interceptor<ORPCClientInterceptorOptions<TClientContext, TInput>, PromiseWithError<TOutput, TError>>;
|
|
15
|
-
interface ORPCClientScopedOptions<TClientContext extends ClientContext, TInput, TOutput, TError> {
|
|
16
|
-
/**
|
|
17
|
-
* Interceptors that wrap the entire client call lifecycle.
|
|
18
|
-
*/
|
|
19
|
-
interceptors?: ORPCClientInterceptor<TClientContext, TInput, TOutput, TError>[] | undefined;
|
|
20
|
-
}
|
|
21
|
-
type ORPCClientScoped<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? ORPCClientScopedOptions<UClientContext, UInput, UOutput, UError> : {
|
|
22
|
-
[K in keyof T]?: (T[K] extends AnyNestedClient ? ORPCClientScoped<T[K]> : never) | undefined;
|
|
23
|
-
};
|
|
24
|
-
interface ORPCClientOptions<T extends AnyNestedClient> {
|
|
25
|
-
/**
|
|
26
|
-
* Use as base path for all procedure, useful when you only want to call a subset of the procedure.
|
|
27
|
-
*/
|
|
28
|
-
path?: string[] | undefined;
|
|
29
|
-
/**
|
|
30
|
-
* Interceptors that wrap the entire client call lifecycle, applied to every procedure call.
|
|
31
|
-
*/
|
|
32
|
-
interceptors?: ORPCClientInterceptor<InferClientContext<T>, unknown, unknown, InferClientError<T>>[] | undefined;
|
|
33
|
-
/**
|
|
34
|
-
* Per-procedure options following the shape of the router.
|
|
35
|
-
* Allows fine-grained configuration (e.g. additional interceptors) for individual procedures
|
|
36
|
-
* without affecting the rest of the router.
|
|
37
|
-
*/
|
|
38
|
-
scoped?: ORPCClientScoped<T> | undefined;
|
|
39
|
-
}
|
|
1
|
+
export * from "./async-iterator-object.mjs";
|
|
2
|
+
export * from "./client.mjs";
|
|
3
|
+
export * from "./client-safe.mjs";
|
|
4
|
+
export * from "./consts.mjs";
|
|
5
|
+
export * from "./dynamic-link.mjs";
|
|
6
|
+
export * from "./error.mjs";
|
|
7
|
+
export * from "./error-utils.mjs";
|
|
8
|
+
export * from "./rpc-json-serializer.mjs";
|
|
9
|
+
export * from "./rpc-serializer.mjs";
|
|
10
|
+
export * from "./types.mjs";
|
|
11
|
+
export type {
|
|
40
12
|
/**
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
declare function resolveClientRest<TClientContext extends ClientContext, TInput>(rest: ClientRest<TClientContext, TInput>): [input: TInput, options: ClientOptions<TClientContext>];
|
|
50
|
-
type SafeResult<TOutput, TError> = [error: null, data: TOutput, definedError: null, isSuccess: true] & {
|
|
51
|
-
error: null;
|
|
52
|
-
data: TOutput;
|
|
53
|
-
definedError: null;
|
|
54
|
-
isSuccess: true;
|
|
55
|
-
} | [error: Exclude<TError, AnyORPCError>, data: undefined, definedError: null, isSuccess: false] & {
|
|
56
|
-
error: Exclude<TError, AnyORPCError>;
|
|
57
|
-
data: undefined;
|
|
58
|
-
definedError: null;
|
|
59
|
-
isSuccess: false;
|
|
60
|
-
} | [error: Extract<TError, AnyORPCError>, data: undefined, definedError: Extract<TError, AnyORPCError>, isSuccess: false] & {
|
|
61
|
-
error: Extract<TError, AnyORPCError>;
|
|
62
|
-
data: undefined;
|
|
63
|
-
definedError: Extract<TError, AnyORPCError>;
|
|
64
|
-
isSuccess: false;
|
|
65
|
-
};
|
|
13
|
+
* @deprecated Use `InferClientError` instead.
|
|
14
|
+
*/
|
|
15
|
+
InferClientError as InferClientErrorUnion } from "./types.mjs";
|
|
16
|
+
export { resolveClientRest, safe } from "./utils.mjs";
|
|
17
|
+
export type { SafeResult } from "./utils.mjs";
|
|
18
|
+
export type { AsyncCleanupFn, AsyncIteratorClassNextFn, MaybeOptionalOptions, PromiseWithError, Registry, ThrowableError } from "@orpc/shared";
|
|
19
|
+
export { AsyncIteratorClass, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, consumeAsyncIterator,
|
|
66
20
|
/**
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
* ```ts
|
|
71
|
-
* const [error, data, definedError, isSuccess] = await safe(client(...))
|
|
72
|
-
* // or const { error, data, definedError, isSuccess } = await safe(client(...))
|
|
73
|
-
*
|
|
74
|
-
* if (definedError) {
|
|
75
|
-
* console.log(definedError) // or error, both are well typed
|
|
76
|
-
* }
|
|
77
|
-
* ```
|
|
78
|
-
*
|
|
79
|
-
* @see {@link https://orpc.dev/docs/client/error-handling#using-safe-and-isdefinederror | Client Error Handling - Using safe and isDefinedError}
|
|
80
|
-
*/
|
|
81
|
-
declare function safe<TOutput, TError = ThrowableError>(promise: PromiseWithError<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
|
|
82
|
-
|
|
83
|
-
type SafeClient<T extends AnyNestedClient> = T extends Client<infer UContext, infer UInput, infer UOutput, infer UError> ? (...rest: ClientRest<UContext, UInput>) => Promise<SafeResult<UOutput, UError>> : {
|
|
84
|
-
[K in keyof T]: T[K] extends AnyNestedClient ? SafeClient<T[K]> : never;
|
|
85
|
-
};
|
|
21
|
+
* @deprecated Use `consumeAsyncIterator` instead.
|
|
22
|
+
*/
|
|
23
|
+
consumeAsyncIterator as consumeEventIterator,
|
|
86
24
|
/**
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
* ```ts
|
|
91
|
-
* const safeClient = createSafeClient(client)
|
|
92
|
-
* const { error, data, definedError, isSuccess } = await safeClient.doSomething({ id: '123' })
|
|
93
|
-
* // or const [error, data, definedError, isSuccess] = await safeClient.doSomething({ id: '123' })
|
|
94
|
-
* ```
|
|
95
|
-
*
|
|
96
|
-
* @see {@link https://orpc.dev/docs/client/error-handling#safe-client | Client Error Handling - Safe Client}
|
|
97
|
-
*/
|
|
98
|
-
declare function createSafeClient<T extends AnyNestedClient>(client: T): SafeClient<T>;
|
|
99
|
-
|
|
25
|
+
* @deprecated Use `asyncIteratorToStream` instead.
|
|
26
|
+
*/
|
|
27
|
+
asyncIteratorToStream as eventIteratorToStream,
|
|
100
28
|
/**
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
* These properties are commonly accessed automatically by JavaScript runtimes,
|
|
105
|
-
* language features, or third-party libraries. Returning another recursive
|
|
106
|
-
* proxy for them can cause unexpected behavior, compatibility issues, or
|
|
107
|
-
* infinite proxy chains.
|
|
108
|
-
*/
|
|
109
|
-
declare const RECURSIVE_CLIENT_UNWRAP_KEYS: Set<PropertyKey>;
|
|
110
|
-
|
|
29
|
+
* @deprecated Use `asyncIteratorToUnproxiedDataStream` instead.
|
|
30
|
+
*/
|
|
31
|
+
asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess, streamToAsyncIteratorObject,
|
|
111
32
|
/**
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
*/
|
|
117
|
-
declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
|
|
118
|
-
private readonly linkResolver;
|
|
119
|
-
constructor(linkResolver: (options: ClientOptions<TClientContext>, path: string[], input: unknown) => Promisable<ClientLink<TClientContext>>);
|
|
120
|
-
call(path: string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
|
|
121
|
-
}
|
|
122
|
-
|
|
33
|
+
* @deprecated Use `streamToAsyncIteratorObject` instead.
|
|
34
|
+
*/
|
|
35
|
+
streamToAsyncIteratorObject as streamToEventIterator } from "@orpc/shared";
|
|
36
|
+
export type {
|
|
123
37
|
/**
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
declare function isDefinedError<T>(error: T): error is Extract<T, AnyORPCError>;
|
|
130
|
-
declare function toORPCError<T>(error: T): Extract<T, AnyORPCError> | ORPCError<'INTERNAL_SERVER_ERROR', undefined>;
|
|
131
|
-
declare function isORPCErrorJson(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
|
|
132
|
-
declare function createORPCErrorFromJson<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
|
|
38
|
+
* @deprecated Use `PromiseWithError` instead.
|
|
39
|
+
*/
|
|
40
|
+
PromiseWithError as ClientPromiseResult } from "@orpc/shared";
|
|
41
|
+
export type { EventMeta, StandardBody, StandardBodyHint, StandardHeaders, StandardLazyRequest, StandardLazyResponse, StandardMethod, StandardRequest, StandardResponse, StandardUrl } from "@standard-server/core";
|
|
42
|
+
export { ErrorEvent,
|
|
133
43
|
/**
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
*/
|
|
141
|
-
declare function cloneORPCError<T extends AnyORPCError>(error: T): T;
|
|
142
|
-
|
|
143
|
-
export { AnyNestedClient, AnyORPCError, Client, ClientContext, ClientLink, ClientOptions, ClientRest, DynamicLink, InferClientContext, InferClientError, InferClientError as InferClientErrorUnion, ORPCError, ORPCErrorCode, ORPCErrorJSON, RECURSIVE_CLIENT_UNWRAP_KEYS, cloneORPCError, createORPCClient, createORPCErrorFromJson, createSafeClient, isDefinedError, isORPCErrorJson, resolveClientRest, safe, toORPCError, wrapAsyncIteratorPreservingEventMeta };
|
|
144
|
-
export type { ORPCClientInterceptor, ORPCClientInterceptorOptions, ORPCClientOptions, ORPCClientScoped, ORPCClientScopedOptions, SafeClient, SafeResult };
|
|
44
|
+
* Retrieves event metadata (such as the event id and retry interval)
|
|
45
|
+
* attached to a single iterator event value.
|
|
46
|
+
*
|
|
47
|
+
* @see {@link https://orpc.dev/docs/client/async-iterator-object#event-metadata | AsyncIteratorObject in Client - Event Metadata}
|
|
48
|
+
*/
|
|
49
|
+
getEventMeta, unwrapEvent, withEventMeta } from "@standard-server/core";
|
package/dist/index.mjs
CHANGED
|
@@ -1,147 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
1
|
+
export * from "./async-iterator-object.mjs";
|
|
2
|
+
export * from "./client.mjs";
|
|
3
|
+
export * from "./client-safe.mjs";
|
|
4
|
+
export * from "./consts.mjs";
|
|
5
|
+
export * from "./dynamic-link.mjs";
|
|
6
|
+
export * from "./error.mjs";
|
|
7
|
+
export * from "./error-utils.mjs";
|
|
8
|
+
export * from "./rpc-json-serializer.mjs";
|
|
9
|
+
export * from "./rpc-serializer.mjs";
|
|
10
|
+
export * from "./types.mjs";
|
|
11
|
+
export { resolveClientRest, safe } from "./utils.mjs";
|
|
12
|
+
export { AsyncIteratorClass, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, consumeAsyncIterator,
|
|
7
13
|
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Prevents the client from being treated as a thenable when users
|
|
11
|
-
* accidentally write `await client`.
|
|
12
|
-
*/
|
|
13
|
-
"then",
|
|
14
|
-
/**
|
|
15
|
-
* Commonly used by libraries to bind functions to a specific `this`
|
|
16
|
-
* context.
|
|
17
|
-
*/
|
|
18
|
-
"bind",
|
|
19
|
-
/**
|
|
20
|
-
* Commonly used by libraries to invoke functions with a specific `this`
|
|
21
|
-
* context.
|
|
22
|
-
*/
|
|
23
|
-
"call",
|
|
24
|
-
/**
|
|
25
|
-
* Commonly used by libraries to invoke functions with a specific `this`
|
|
26
|
-
* context and an arguments array.
|
|
27
|
-
*/
|
|
28
|
-
"apply",
|
|
29
|
-
/**
|
|
30
|
-
* Commonly accessed during primitive conversion, inspection, and logging.
|
|
31
|
-
*/
|
|
32
|
-
"valueOf",
|
|
33
|
-
/**
|
|
34
|
-
* Commonly accessed during string conversion, inspection, and logging.
|
|
35
|
-
*/
|
|
36
|
-
"toString",
|
|
37
|
-
/**
|
|
38
|
-
* Commonly accessed by serializers such as `JSON.stringify`.
|
|
39
|
-
*/
|
|
40
|
-
"toJSON"
|
|
41
|
-
]);
|
|
14
|
+
consumeAsyncIterator as consumeEventIterator,
|
|
42
15
|
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
...options,
|
|
46
|
-
context: options.context ?? {}
|
|
47
|
-
// Context only optional if all fields are optional
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
function resolveClientRest(rest) {
|
|
51
|
-
return [
|
|
52
|
-
rest[0],
|
|
53
|
-
// rest[0] can be undefined if TInput is optional,
|
|
54
|
-
resolveFriendlyClientOptions(rest[1] ?? {})
|
|
55
|
-
// rest[1] can be undefined if all fields of FriendlyClientOptions are optional
|
|
56
|
-
];
|
|
57
|
-
}
|
|
58
|
-
async function safe(promise) {
|
|
59
|
-
try {
|
|
60
|
-
const output = await promise;
|
|
61
|
-
return Object.assign(
|
|
62
|
-
[null, output, null, true],
|
|
63
|
-
{ error: null, data: output, definedError: null, isSuccess: true }
|
|
64
|
-
);
|
|
65
|
-
} catch (e) {
|
|
66
|
-
const error = e;
|
|
67
|
-
if (isDefinedError(error)) {
|
|
68
|
-
return Object.assign(
|
|
69
|
-
[error, void 0, error, false],
|
|
70
|
-
{ error, data: void 0, definedError: error, isSuccess: false }
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
return Object.assign(
|
|
74
|
-
[error, void 0, null, false],
|
|
75
|
-
{ error, data: void 0, definedError: null, isSuccess: false }
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
16
|
+
asyncIteratorToStream as eventIteratorToStream,
|
|
79
17
|
|
|
80
|
-
|
|
81
|
-
const procedureClient = (...rest) => {
|
|
82
|
-
const [input, callOptions] = resolveClientRest(rest);
|
|
83
|
-
const interceptors = [
|
|
84
|
-
...toArray(options.interceptors),
|
|
85
|
-
...toArray(options.scoped?.interceptors)
|
|
86
|
-
];
|
|
87
|
-
return intercept(
|
|
88
|
-
interceptors,
|
|
89
|
-
{ ...callOptions, input, path },
|
|
90
|
-
({ path: path2, input: input2, ...callOptions2 }) => link.call(path2, input2, callOptions2)
|
|
91
|
-
);
|
|
92
|
-
};
|
|
93
|
-
const cache = /* @__PURE__ */ new Map();
|
|
94
|
-
const recursive = new Proxy(procedureClient, {
|
|
95
|
-
get(target, key) {
|
|
96
|
-
if (typeof key !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(key)) {
|
|
97
|
-
return Reflect.get(target, key);
|
|
98
|
-
}
|
|
99
|
-
let client = cache.get(key);
|
|
100
|
-
if (client === void 0) {
|
|
101
|
-
client = createORPCClient(link, {
|
|
102
|
-
...options,
|
|
103
|
-
path: [...path, key],
|
|
104
|
-
scoped: options.scoped?.[key]
|
|
105
|
-
});
|
|
106
|
-
cache.set(key, client);
|
|
107
|
-
}
|
|
108
|
-
return client;
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
return recursive;
|
|
112
|
-
}
|
|
18
|
+
asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess, streamToAsyncIteratorObject,
|
|
113
19
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const proxy = new Proxy((...args) => safe(client(...args)), {
|
|
117
|
-
get(target, prop) {
|
|
118
|
-
if (typeof prop !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
|
|
119
|
-
return Reflect.get(target, prop);
|
|
120
|
-
}
|
|
121
|
-
let safeClient = cache.get(prop);
|
|
122
|
-
if (safeClient === void 0) {
|
|
123
|
-
const value = client[prop];
|
|
124
|
-
if (!isTypescriptObject(value)) {
|
|
125
|
-
return value;
|
|
126
|
-
}
|
|
127
|
-
safeClient = createSafeClient(value);
|
|
128
|
-
cache.set(prop, safeClient);
|
|
129
|
-
}
|
|
130
|
-
return safeClient;
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
return proxy;
|
|
134
|
-
}
|
|
20
|
+
streamToAsyncIteratorObject as streamToEventIterator } from "@orpc/shared";
|
|
21
|
+
export { ErrorEvent,
|
|
135
22
|
|
|
136
|
-
|
|
137
|
-
constructor(linkResolver) {
|
|
138
|
-
this.linkResolver = linkResolver;
|
|
139
|
-
}
|
|
140
|
-
async call(path, input, options) {
|
|
141
|
-
const resolvedLink = await this.linkResolver(options, path, input);
|
|
142
|
-
const output = await resolvedLink.call(path, input, options);
|
|
143
|
-
return output;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export { DynamicLink, RECURSIVE_CLIENT_UNWRAP_KEYS, createORPCClient, createSafeClient, isDefinedError, resolveClientRest, safe };
|
|
23
|
+
getEventMeta, unwrapEvent, withEventMeta } from "@standard-server/core";
|