@orpc/client 2.0.0-beta.48 → 2.0.0-beta.49

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.
Files changed (147) hide show
  1. package/README.md +4 -4
  2. package/dist/adapters/fetch/index.d.ts +2 -0
  3. package/dist/adapters/fetch/index.js +2 -0
  4. package/dist/adapters/fetch/rpc-link.d.ts +14 -0
  5. package/dist/adapters/fetch/rpc-link.js +14 -0
  6. package/dist/adapters/fetch/transport.d.ts +34 -0
  7. package/dist/adapters/fetch/transport.js +65 -0
  8. package/dist/adapters/message-port/index.d.ts +3 -0
  9. package/dist/adapters/message-port/index.js +3 -0
  10. package/dist/adapters/message-port/message-port.d.ts +29 -0
  11. package/dist/adapters/message-port/message-port.js +56 -0
  12. package/dist/adapters/message-port/rpc-link.d.ts +15 -0
  13. package/dist/adapters/message-port/rpc-link.js +15 -0
  14. package/dist/adapters/message-port/transport.d.ts +38 -0
  15. package/dist/adapters/message-port/transport.js +52 -0
  16. package/dist/adapters/standard/codec.d.ts +14 -0
  17. package/dist/adapters/standard/index.d.ts +5 -0
  18. package/dist/adapters/standard/index.js +4 -0
  19. package/dist/adapters/standard/link.d.ts +40 -0
  20. package/dist/adapters/standard/link.js +85 -0
  21. package/dist/adapters/standard/plugin.d.ts +32 -0
  22. package/dist/adapters/standard/plugin.js +16 -0
  23. package/dist/adapters/standard/rpc-link-codec.d.ts +63 -0
  24. package/dist/adapters/standard/rpc-link-codec.js +104 -0
  25. package/dist/adapters/standard/transport.d.ts +14 -0
  26. package/dist/adapters/websocket/index.d.ts +2 -0
  27. package/dist/adapters/websocket/index.js +2 -0
  28. package/dist/adapters/websocket/rpc-link.d.ts +14 -0
  29. package/dist/adapters/websocket/rpc-link.js +14 -0
  30. package/dist/adapters/websocket/transport.d.ts +115 -0
  31. package/dist/adapters/websocket/transport.js +133 -0
  32. package/dist/async-iterator-object.d.ts +2 -0
  33. package/dist/async-iterator-object.js +29 -0
  34. package/dist/client-safe.d.ts +18 -0
  35. package/dist/client-safe.js +36 -0
  36. package/dist/client.d.ts +40 -0
  37. package/dist/client.js +39 -0
  38. package/dist/consts.d.ts +10 -0
  39. package/dist/consts.js +43 -0
  40. package/dist/dynamic-link.d.ts +13 -0
  41. package/dist/dynamic-link.js +17 -0
  42. package/dist/error-utils.d.ts +21 -0
  43. package/dist/error-utils.js +58 -0
  44. package/dist/error.d.ts +132 -0
  45. package/dist/error.js +178 -0
  46. package/dist/index.d.ts +53 -0
  47. package/dist/index.js +40 -0
  48. package/dist/plugins/batch.d.ts +185 -0
  49. package/dist/plugins/batch.js +401 -0
  50. package/dist/plugins/cookie-jar.d.ts +82 -0
  51. package/dist/plugins/cookie-jar.js +268 -0
  52. package/dist/plugins/dedupe.d.ts +64 -0
  53. package/dist/plugins/dedupe.js +226 -0
  54. package/dist/plugins/index.d.ts +28 -0
  55. package/dist/plugins/index.js +23 -0
  56. package/dist/plugins/request-compression.d.ts +44 -0
  57. package/dist/plugins/request-compression.js +178 -0
  58. package/dist/plugins/response-compression.d.ts +28 -0
  59. package/dist/plugins/response-compression.js +91 -0
  60. package/dist/plugins/retry-after.d.ts +50 -0
  61. package/dist/plugins/retry-after.js +88 -0
  62. package/dist/plugins/retry.d.ts +84 -0
  63. package/dist/plugins/retry.js +140 -0
  64. package/dist/plugins/timeout.d.ts +25 -0
  65. package/dist/plugins/timeout.js +37 -0
  66. package/dist/rpc-json-serializer.d.ts +99 -0
  67. package/dist/rpc-json-serializer.js +392 -0
  68. package/dist/rpc-serializer.d.ts +32 -0
  69. package/dist/rpc-serializer.js +130 -0
  70. package/dist/types.d.ts +97 -0
  71. package/dist/utils.d.ts +47 -0
  72. package/dist/utils.js +51 -0
  73. package/package.json +43 -45
  74. package/dist/adapters/fetch/index.d.mts +0 -2
  75. package/dist/adapters/fetch/index.mjs +0 -2
  76. package/dist/adapters/fetch/rpc-link.d.mts +0 -13
  77. package/dist/adapters/fetch/rpc-link.mjs +0 -10
  78. package/dist/adapters/fetch/transport.d.mts +0 -31
  79. package/dist/adapters/fetch/transport.mjs +0 -65
  80. package/dist/adapters/message-port/index.d.mts +0 -3
  81. package/dist/adapters/message-port/index.mjs +0 -3
  82. package/dist/adapters/message-port/message-port.d.mts +0 -29
  83. package/dist/adapters/message-port/message-port.mjs +0 -43
  84. package/dist/adapters/message-port/rpc-link.d.mts +0 -14
  85. package/dist/adapters/message-port/rpc-link.mjs +0 -10
  86. package/dist/adapters/message-port/transport.d.mts +0 -35
  87. package/dist/adapters/message-port/transport.mjs +0 -47
  88. package/dist/adapters/standard/codec.d.mts +0 -14
  89. package/dist/adapters/standard/index.d.mts +0 -5
  90. package/dist/adapters/standard/index.mjs +0 -4
  91. package/dist/adapters/standard/link.d.mts +0 -40
  92. package/dist/adapters/standard/link.mjs +0 -79
  93. package/dist/adapters/standard/plugin.d.mts +0 -32
  94. package/dist/adapters/standard/plugin.mjs +0 -16
  95. package/dist/adapters/standard/rpc-link-codec.d.mts +0 -55
  96. package/dist/adapters/standard/rpc-link-codec.mjs +0 -104
  97. package/dist/adapters/standard/transport.d.mts +0 -14
  98. package/dist/adapters/websocket/index.d.mts +0 -2
  99. package/dist/adapters/websocket/index.mjs +0 -2
  100. package/dist/adapters/websocket/rpc-link.d.mts +0 -13
  101. package/dist/adapters/websocket/rpc-link.mjs +0 -10
  102. package/dist/adapters/websocket/transport.d.mts +0 -119
  103. package/dist/adapters/websocket/transport.mjs +0 -123
  104. package/dist/async-iterator-object.d.mts +0 -7
  105. package/dist/async-iterator-object.mjs +0 -30
  106. package/dist/client-safe.d.mts +0 -16
  107. package/dist/client-safe.mjs +0 -23
  108. package/dist/client.d.mts +0 -51
  109. package/dist/client.mjs +0 -32
  110. package/dist/consts.d.mts +0 -10
  111. package/dist/consts.mjs +0 -10
  112. package/dist/dynamic-link.d.mts +0 -13
  113. package/dist/dynamic-link.mjs +0 -12
  114. package/dist/error-utils.d.mts +0 -24
  115. package/dist/error-utils.mjs +0 -44
  116. package/dist/error.d.mts +0 -138
  117. package/dist/error.mjs +0 -105
  118. package/dist/index.d.mts +0 -49
  119. package/dist/index.mjs +0 -23
  120. package/dist/plugins/batch.d.mts +0 -150
  121. package/dist/plugins/batch.mjs +0 -339
  122. package/dist/plugins/cookie-jar.d.mts +0 -82
  123. package/dist/plugins/cookie-jar.mjs +0 -229
  124. package/dist/plugins/dedupe.d.mts +0 -58
  125. package/dist/plugins/dedupe.mjs +0 -199
  126. package/dist/plugins/index.d.mts +0 -28
  127. package/dist/plugins/index.mjs +0 -17
  128. package/dist/plugins/request-compression.d.mts +0 -44
  129. package/dist/plugins/request-compression.mjs +0 -150
  130. package/dist/plugins/response-compression.d.mts +0 -28
  131. package/dist/plugins/response-compression.mjs +0 -78
  132. package/dist/plugins/retry-after.d.mts +0 -41
  133. package/dist/plugins/retry-after.mjs +0 -71
  134. package/dist/plugins/retry.d.mts +0 -82
  135. package/dist/plugins/retry.mjs +0 -127
  136. package/dist/plugins/timeout.d.mts +0 -25
  137. package/dist/plugins/timeout.mjs +0 -36
  138. package/dist/rpc-json-serializer.d.mts +0 -99
  139. package/dist/rpc-json-serializer.mjs +0 -365
  140. package/dist/rpc-serializer.d.mts +0 -32
  141. package/dist/rpc-serializer.mjs +0 -100
  142. package/dist/types.d.mts +0 -93
  143. package/dist/utils.d.mts +0 -46
  144. package/dist/utils.mjs +0 -53
  145. /package/dist/adapters/standard/{codec.mjs → codec.js} +0 -0
  146. /package/dist/adapters/standard/{transport.mjs → transport.js} +0 -0
  147. /package/dist/{types.mjs → types.js} +0 -0
package/dist/consts.js ADDED
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Property names that should resolve to the underlying value instead of
3
+ * continuing recursive proxy traversal.
4
+ *
5
+ * These properties are commonly accessed automatically by JavaScript runtimes,
6
+ * language features, or third-party libraries. Returning another recursive
7
+ * proxy for them can cause unexpected behavior, compatibility issues, or
8
+ * infinite proxy chains.
9
+ */
10
+ export const RECURSIVE_CLIENT_UNWRAP_KEYS = /* @__PURE__ */ new Set([
11
+ /**
12
+ * Prevents the client from being treated as a thenable when users
13
+ * accidentally write `await client`.
14
+ */
15
+ 'then',
16
+ /**
17
+ * Commonly used by libraries to bind functions to a specific `this`
18
+ * context.
19
+ */
20
+ 'bind',
21
+ /**
22
+ * Commonly used by libraries to invoke functions with a specific `this`
23
+ * context.
24
+ */
25
+ 'call',
26
+ /**
27
+ * Commonly used by libraries to invoke functions with a specific `this`
28
+ * context and an arguments array.
29
+ */
30
+ 'apply',
31
+ /**
32
+ * Commonly accessed during primitive conversion, inspection, and logging.
33
+ */
34
+ 'valueOf',
35
+ /**
36
+ * Commonly accessed during string conversion, inspection, and logging.
37
+ */
38
+ 'toString',
39
+ /**
40
+ * Commonly accessed by serializers such as `JSON.stringify`.
41
+ */
42
+ 'toJSON',
43
+ ]);
@@ -0,0 +1,13 @@
1
+ import type { Promisable } from '@orpc/shared';
2
+ import type { ClientContext, ClientLink, ClientOptions } from './types.ts';
3
+ /**
4
+ * DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
5
+ * based on the request path, input, and context.
6
+ *
7
+ * @see {@link https://orpc.dev/docs/client/dynamic-link | DynamicLink}
8
+ */
9
+ export declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
10
+ private readonly linkResolver;
11
+ constructor(linkResolver: (options: ClientOptions<TClientContext>, path: string[], input: unknown) => Promisable<ClientLink<TClientContext>>);
12
+ call(path: string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
13
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
3
+ * based on the request path, input, and context.
4
+ *
5
+ * @see {@link https://orpc.dev/docs/client/dynamic-link | DynamicLink}
6
+ */
7
+ export class DynamicLink {
8
+ linkResolver;
9
+ constructor(linkResolver) {
10
+ this.linkResolver = linkResolver;
11
+ }
12
+ async call(path, input, options) {
13
+ const resolvedLink = await this.linkResolver(options, path, input);
14
+ const output = await resolvedLink.call(path, input, options);
15
+ return output;
16
+ }
17
+ }
@@ -0,0 +1,21 @@
1
+ import type { AnyORPCError, ORPCErrorCode, ORPCErrorJSON } from './error.ts';
2
+ import { ORPCError } from './error.ts';
3
+ /**
4
+ * Checks if an error is an `ORPCError` that matches a definition in the procedure's `.errors` map,
5
+ * narrowing it so `code` and `data` are fully typed.
6
+ *
7
+ * @see {@link https://orpc.dev/docs/client/error-handling#using-safe-and-isdefinederror | Client Error Handling - Using safe and isDefinedError}
8
+ */
9
+ export declare function isDefinedError<T>(error: T): error is Extract<T, AnyORPCError>;
10
+ export declare function toORPCError<T>(error: T): Extract<T, AnyORPCError> | ORPCError<'INTERNAL_SERVER_ERROR', undefined>;
11
+ export declare function isORPCErrorJson(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
12
+ export declare function createORPCErrorFromJson<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
13
+ /**
14
+ * Clones an `ORPCError` while preserving its prototype chain, so instances of
15
+ * `ORPCError` subclasses remain `instanceof` their class.
16
+ *
17
+ * Limitation: subclass constructors are not re-run, so private fields
18
+ * (`#field`) are not carried over and subclass members that read them
19
+ * will throw on the clone.
20
+ */
21
+ export declare function cloneORPCError<T extends AnyORPCError>(error: T): T;
@@ -0,0 +1,58 @@
1
+ import { isPlainObject } from '@orpc/shared';
2
+ import { ORPCError } from './error.js';
3
+ /**
4
+ * Checks if an error is an `ORPCError` that matches a definition in the procedure's `.errors` map,
5
+ * narrowing it so `code` and `data` are fully typed.
6
+ *
7
+ * @see {@link https://orpc.dev/docs/client/error-handling#using-safe-and-isdefinederror | Client Error Handling - Using safe and isDefinedError}
8
+ */
9
+ export function isDefinedError(error) {
10
+ return error instanceof ORPCError && error.defined;
11
+ }
12
+ export function toORPCError(error) {
13
+ return error instanceof ORPCError
14
+ ? error
15
+ : new ORPCError('INTERNAL_SERVER_ERROR', { cause: error });
16
+ }
17
+ export function isORPCErrorJson(json) {
18
+ if (!isPlainObject(json)) {
19
+ return false;
20
+ }
21
+ const validKeys = ['defined', 'code', 'message', 'data'];
22
+ if (Object.keys(json).some((k) => !validKeys.includes(k))) {
23
+ return false;
24
+ }
25
+ return ('defined' in json &&
26
+ typeof json.defined === 'boolean' &&
27
+ 'code' in json &&
28
+ typeof json.code === 'string' &&
29
+ 'message' in json &&
30
+ typeof json.message === 'string');
31
+ }
32
+ export function createORPCErrorFromJson(json, options = {}) {
33
+ const error = new ORPCError(json.code, {
34
+ ...json,
35
+ ...options,
36
+ });
37
+ error.defined = json.defined;
38
+ return error;
39
+ }
40
+ /**
41
+ * Clones an `ORPCError` while preserving its prototype chain, so instances of
42
+ * `ORPCError` subclasses remain `instanceof` their class.
43
+ *
44
+ * Limitation: subclass constructors are not re-run, so private fields
45
+ * (`#field`) are not carried over and subclass members that read them
46
+ * will throw on the clone.
47
+ */
48
+ export function cloneORPCError(error) {
49
+ const cloned = new ORPCError(error.code, {
50
+ message: error.message,
51
+ data: error.data,
52
+ cause: error.cause,
53
+ });
54
+ Object.setPrototypeOf(cloned, Object.getPrototypeOf(error));
55
+ Object.defineProperties(cloned, Object.getOwnPropertyDescriptors(error));
56
+ cloned.stack = error.stack;
57
+ return cloned;
58
+ }
@@ -0,0 +1,132 @@
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<TCode extends ORPCErrorCode, TData> extends Error {
52
+ /**
53
+ * @remarks
54
+ * **Note**: The `__branch` property is used for type branding, helping TypeScript distinguish
55
+ * an `ORPCError` instance from plain objects with a similar structure.
56
+ */
57
+ readonly name: 'ORPCError' & {
58
+ __branch: 'ORPCError';
59
+ };
60
+ /**
61
+ * Indicates whether the error matches a definition in the procedure's `.errors` map,
62
+ * which makes its type inferable on the client.
63
+ */
64
+ readonly defined: boolean;
65
+ code: TCode;
66
+ data: TData;
67
+ constructor(code: TCode, ...rest: MaybeOptionalOptions<ORPCErrorOptions<TData>>);
68
+ toJSON(): ORPCErrorJSON<TCode, TData>;
69
+ /**
70
+ * Workaround for Next.js where different contexts use separate
71
+ * dependency graphs, causing multiple ORPCError constructors existing and breaking
72
+ * `instanceof` checks across contexts.
73
+ *
74
+ * This is particularly problematic with "Optimized SSR", where orpc-client
75
+ * executes in one context but is invoked from another. When an error is thrown
76
+ * in the execution context, `instanceof ORPCError` checks fail in the
77
+ * invocation context due to separate class constructors.
78
+ *
79
+ * @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
80
+ */
81
+ static [Symbol.hasInstance](instance: unknown): boolean;
82
+ }
83
+ export interface ORPCErrorJSON<TCode extends string, TData> extends Pick<ORPCError<TCode, TData>, 'code' | 'message' | 'data'> {
84
+ /**
85
+ * remove readonly
86
+ */
87
+ defined: boolean;
88
+ }
89
+ export type AnyORPCError = ORPCError<any, any>;
90
+ export type AnyORPCErrorJSON = ORPCErrorJSON<any, any>;
91
+ export interface ResponseErrorOptions extends ErrorOptions {
92
+ /**
93
+ * @default inferred from the response status
94
+ */
95
+ message?: string | undefined;
96
+ response: StandardResponse;
97
+ }
98
+ /**
99
+ * Error carrying the resolved response a link received. Links set it as the `cause`
100
+ * of an `ORPCError` decoded from an error response, so details that are not part
101
+ * of the error itself, such as its status or headers, stay available.
102
+ *
103
+ * @see {@link https://orpc.dev/docs/rpc/link#error-responses | RPC Link - Error Responses}
104
+ * @see {@link https://orpc.dev/docs/openapi/link#error-responses | OpenAPI Link - Error Responses}
105
+ */
106
+ export declare class ResponseError extends Error {
107
+ readonly name: string;
108
+ response: StandardResponse;
109
+ constructor(options: ResponseErrorOptions);
110
+ }
111
+ export interface MalformedResponseErrorOptions extends ResponseErrorOptions {
112
+ /**
113
+ * @default inferred from the response body or status
114
+ */
115
+ message?: string | undefined;
116
+ }
117
+ /**
118
+ * Error thrown by links when a response does not follow the expected oRPC format,
119
+ * for example when a proxy or gateway answers instead of your handler. Extends
120
+ * `ResponseError`, so it carries the resolved response. Unless overridden via
121
+ * `options.message`, the message is inferred from the response body or status.
122
+ *
123
+ * Unlike `ORPCError`, it reaches a handler's callers as a generic `INTERNAL_SERVER_ERROR`
124
+ * when thrown from the handler, so an upstream's response is not sent to them.
125
+ *
126
+ * @see {@link https://orpc.dev/docs/rpc/link#malformed-responses | RPC Link - Malformed Responses}
127
+ * @see {@link https://orpc.dev/docs/openapi/link#malformed-responses | OpenAPI Link - Malformed Responses}
128
+ */
129
+ export declare class MalformedResponseError extends ResponseError {
130
+ readonly name = "MalformedResponseError";
131
+ constructor(options: MalformedResponseErrorOptions);
132
+ }
package/dist/error.js ADDED
@@ -0,0 +1,178 @@
1
+ import { getConstructors, isPlainObject, resolveMaybeOptionalOptions } from '@orpc/shared';
2
+ /**
3
+ * Default mapping between common oRPC error codes and HTTP status codes.
4
+ * Handlers use it to determine response status codes; spread it to build a custom `errorStatusMap`.
5
+ *
6
+ * @see {@link https://orpc.dev/docs/rpc/handler#custom-error-response | RPC Handler - Custom Error Response}
7
+ * @see {@link https://orpc.dev/docs/openapi/handler#custom-error-response | OpenAPI Handler - Custom Error Response}
8
+ */
9
+ export const COMMON_ERROR_STATUS_MAP = {
10
+ BAD_REQUEST: 400,
11
+ UNAUTHORIZED: 401,
12
+ PAYMENT_REQUIRED: 402,
13
+ FORBIDDEN: 403,
14
+ NOT_FOUND: 404,
15
+ METHOD_NOT_SUPPORTED: 405,
16
+ NOT_ACCEPTABLE: 406,
17
+ TIMEOUT: 408,
18
+ CONFLICT: 409,
19
+ GONE: 410,
20
+ LENGTH_REQUIRED: 411,
21
+ PRECONDITION_FAILED: 412,
22
+ PAYLOAD_TOO_LARGE: 413,
23
+ UNSUPPORTED_MEDIA_TYPE: 415,
24
+ UNPROCESSABLE_CONTENT: 422,
25
+ PRECONDITION_REQUIRED: 428,
26
+ TOO_MANY_REQUESTS: 429,
27
+ CLIENT_CLOSED_REQUEST: 499,
28
+ INTERNAL_SERVER_ERROR: 500,
29
+ NOT_IMPLEMENTED: 501,
30
+ BAD_GATEWAY: 502,
31
+ SERVICE_UNAVAILABLE: 503,
32
+ GATEWAY_TIMEOUT: 504,
33
+ };
34
+ let ORPCErrorConstructors;
35
+ /**
36
+ * Typed error carrying a `code`, a `message`, and optional `data`.
37
+ * Throw it from handlers or middleware to produce typed error responses on the client.
38
+ *
39
+ * @see {@link https://orpc.dev/docs/error-handling#orpcerror-class | Error Handling - ORPCError Class}
40
+ */
41
+ export class ORPCError extends Error {
42
+ /**
43
+ * Placed inside a static block (rather than at module level) to ensure this
44
+ * registration is treated as part of the class definition by bundlers.
45
+ *
46
+ * With `"sideEffects": false` in package.json, bundlers like webpack/Rollup
47
+ * are allowed to tree-shake any module-level statements that appear to have
48
+ * no consumers. A free-floating `globalORPCErrorConstructors.add(ORPCError)`
49
+ * at module level could be dropped entirely if the bundler decides the module
50
+ * is only partially used.
51
+ *
52
+ * By placing this inside `static {}`, the registration becomes inseparable
53
+ * from the class body itself — a bundler cannot include `ORPCError` without
54
+ * executing this block.
55
+ *
56
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks
57
+ */
58
+ static {
59
+ /**
60
+ * Store all ORPCError constructors
61
+ * for workaround of instanceof check in case multiple dependency graphs exist
62
+ */
63
+ const ORPC_ERROR_CONSTRUCTORS_SYMBOL = Symbol.for('ORPC_ERROR_CONSTRUCTORS');
64
+ void (globalThis[ORPC_ERROR_CONSTRUCTORS_SYMBOL] ??= new WeakSet());
65
+ ORPCErrorConstructors = globalThis[ORPC_ERROR_CONSTRUCTORS_SYMBOL];
66
+ ORPCErrorConstructors.add(ORPCError);
67
+ }
68
+ /**
69
+ * @remarks
70
+ * **Note**: The `__branch` property is used for type branding, helping TypeScript distinguish
71
+ * an `ORPCError` instance from plain objects with a similar structure.
72
+ */
73
+ name = 'ORPCError';
74
+ /**
75
+ * Indicates whether the error matches a definition in the procedure's `.errors` map,
76
+ * which makes its type inferable on the client.
77
+ */
78
+ defined = false;
79
+ code;
80
+ data;
81
+ constructor(code, ...rest) {
82
+ const options = resolveMaybeOptionalOptions(rest);
83
+ const message = options.message ?? codeToMessage(code);
84
+ super(message, options);
85
+ this.code = code;
86
+ this.data = options.data; // data only optional when TData is undefinable so can safely cast here
87
+ }
88
+ toJSON() {
89
+ return {
90
+ defined: this.defined,
91
+ code: this.code,
92
+ message: this.message,
93
+ data: this.data,
94
+ };
95
+ }
96
+ /**
97
+ * Workaround for Next.js where different contexts use separate
98
+ * dependency graphs, causing multiple ORPCError constructors existing and breaking
99
+ * `instanceof` checks across contexts.
100
+ *
101
+ * This is particularly problematic with "Optimized SSR", where orpc-client
102
+ * executes in one context but is invoked from another. When an error is thrown
103
+ * in the execution context, `instanceof ORPCError` checks fail in the
104
+ * invocation context due to separate class constructors.
105
+ *
106
+ * @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
107
+ */
108
+ static [Symbol.hasInstance](instance) {
109
+ if (!ORPCErrorConstructors.has(this)) {
110
+ // not applicable to extended classes
111
+ return super[Symbol.hasInstance](instance);
112
+ }
113
+ for (const constructor of getConstructors(instance)) {
114
+ if (ORPCErrorConstructors.has(constructor)) {
115
+ return true;
116
+ }
117
+ }
118
+ return false;
119
+ }
120
+ }
121
+ /**
122
+ * Error carrying the resolved response a link received. Links set it as the `cause`
123
+ * of an `ORPCError` decoded from an error response, so details that are not part
124
+ * of the error itself, such as its status or headers, stay available.
125
+ *
126
+ * @see {@link https://orpc.dev/docs/rpc/link#error-responses | RPC Link - Error Responses}
127
+ * @see {@link https://orpc.dev/docs/openapi/link#error-responses | OpenAPI Link - Error Responses}
128
+ */
129
+ export class ResponseError extends Error {
130
+ name = 'ResponseError';
131
+ response;
132
+ constructor(options) {
133
+ super(options.message ?? `Received a ${options.response.status} response.`, options);
134
+ this.response = options.response;
135
+ }
136
+ }
137
+ /**
138
+ * Error thrown by links when a response does not follow the expected oRPC format,
139
+ * for example when a proxy or gateway answers instead of your handler. Extends
140
+ * `ResponseError`, so it carries the resolved response. Unless overridden via
141
+ * `options.message`, the message is inferred from the response body or status.
142
+ *
143
+ * Unlike `ORPCError`, it reaches a handler's callers as a generic `INTERNAL_SERVER_ERROR`
144
+ * when thrown from the handler, so an upstream's response is not sent to them.
145
+ *
146
+ * @see {@link https://orpc.dev/docs/rpc/link#malformed-responses | RPC Link - Malformed Responses}
147
+ * @see {@link https://orpc.dev/docs/openapi/link#malformed-responses | OpenAPI Link - Malformed Responses}
148
+ */
149
+ export class MalformedResponseError extends ResponseError {
150
+ name = 'MalformedResponseError';
151
+ constructor(options) {
152
+ super({
153
+ ...options,
154
+ message: options.message ?? inferMalformedResponseMessage(options.response),
155
+ });
156
+ }
157
+ }
158
+ /**
159
+ * Longer body strings are not used as the error message,
160
+ * e.g. full HTML error pages from a proxy or gateway.
161
+ */
162
+ const INFERRED_MESSAGE_MAX_LENGTH = 256;
163
+ function inferMalformedResponseMessage(response) {
164
+ const candidate = isPlainObject(response.body) ? response.body.message : response.body;
165
+ if (typeof candidate === 'string' &&
166
+ candidate.length > 0 &&
167
+ candidate.length <= INFERRED_MESSAGE_MAX_LENGTH) {
168
+ return candidate;
169
+ }
170
+ const commonCode = Object.entries(COMMON_ERROR_STATUS_MAP).find(([, status]) => status === response.status)?.[0];
171
+ return commonCode ? codeToMessage(commonCode) : 'Malformed oRPC Response';
172
+ }
173
+ function codeToMessage(code) {
174
+ return code
175
+ .split('_')
176
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
177
+ .join(' ');
178
+ }
@@ -0,0 +1,53 @@
1
+ export * from './async-iterator-object.ts';
2
+ export * from './client.ts';
3
+ export * from './client-safe.ts';
4
+ export * from './consts.ts';
5
+ export * from './dynamic-link.ts';
6
+ export * from './error.ts';
7
+ export * from './error-utils.ts';
8
+ export * from './rpc-json-serializer.ts';
9
+ export * from './rpc-serializer.ts';
10
+ export * from './types.ts';
11
+ export type {
12
+ /**
13
+ * @deprecated Use `InferClientError` instead.
14
+ */
15
+ InferClientError as InferClientErrorUnion, } from './types.ts';
16
+ export { resolveClientRest, safe } from './utils.ts';
17
+ export type { SafeResult } from './utils.ts';
18
+ export type { AsyncCleanupFn, AsyncIteratorClassNextFn, MaybeOptionalOptions, PromiseWithError, Registry, ThrowableError, } from '@orpc/shared';
19
+ export { AsyncIteratorClass, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, consumeAsyncIterator,
20
+ /**
21
+ * @deprecated Use `consumeAsyncIterator` instead.
22
+ */
23
+ consumeAsyncIterator as consumeEventIterator,
24
+ /**
25
+ * @deprecated Use `asyncIteratorToStream` instead.
26
+ */
27
+ asyncIteratorToStream as eventIteratorToStream,
28
+ /**
29
+ * @deprecated Use `asyncIteratorToUnproxiedDataStream` instead.
30
+ */
31
+ asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess, streamToAsyncIteratorObject,
32
+ /**
33
+ * @deprecated Use `streamToAsyncIteratorObject` instead.
34
+ */
35
+ streamToAsyncIteratorObject as streamToEventIterator, } from '@orpc/shared';
36
+ export type {
37
+ /**
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 {
43
+ /**
44
+ * @deprecated Use `EventStreamErrorEvent` instead.
45
+ */
46
+ EventStreamErrorEvent as ErrorEvent, EventStreamErrorEvent,
47
+ /**
48
+ * Retrieves event metadata (such as the event id and retry interval)
49
+ * attached to a single iterator event value.
50
+ *
51
+ * @see {@link https://orpc.dev/docs/client/async-iterator-object#event-metadata | AsyncIteratorObject in Client - Event Metadata}
52
+ */
53
+ getEventMeta, unwrapEvent, withEventMeta, } from '@standard-server/core';
package/dist/index.js ADDED
@@ -0,0 +1,40 @@
1
+ export * from './async-iterator-object.js';
2
+ export * from './client.js';
3
+ export * from './client-safe.js';
4
+ export * from './consts.js';
5
+ export * from './dynamic-link.js';
6
+ export * from './error.js';
7
+ export * from './error-utils.js';
8
+ export * from './rpc-json-serializer.js';
9
+ export * from './rpc-serializer.js';
10
+ export * from './types.js';
11
+ export { resolveClientRest, safe } from './utils.js';
12
+ export { AsyncIteratorClass, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, consumeAsyncIterator,
13
+ /**
14
+ * @deprecated Use `consumeAsyncIterator` instead.
15
+ */
16
+ consumeAsyncIterator as consumeEventIterator,
17
+ /**
18
+ * @deprecated Use `asyncIteratorToStream` instead.
19
+ */
20
+ asyncIteratorToStream as eventIteratorToStream,
21
+ /**
22
+ * @deprecated Use `asyncIteratorToUnproxiedDataStream` instead.
23
+ */
24
+ asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess, streamToAsyncIteratorObject,
25
+ /**
26
+ * @deprecated Use `streamToAsyncIteratorObject` instead.
27
+ */
28
+ streamToAsyncIteratorObject as streamToEventIterator, } from '@orpc/shared';
29
+ export {
30
+ /**
31
+ * @deprecated Use `EventStreamErrorEvent` instead.
32
+ */
33
+ EventStreamErrorEvent as ErrorEvent, EventStreamErrorEvent,
34
+ /**
35
+ * Retrieves event metadata (such as the event id and retry interval)
36
+ * attached to a single iterator event value.
37
+ *
38
+ * @see {@link https://orpc.dev/docs/client/async-iterator-object#event-metadata | AsyncIteratorObject in Client - Event Metadata}
39
+ */
40
+ getEventMeta, unwrapEvent, withEventMeta, } from '@standard-server/core';