@orpc/client 2.0.0-beta.4 → 2.0.0-beta.40

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 (30) hide show
  1. package/README.md +97 -94
  2. package/dist/adapters/fetch/index.d.mts +16 -33
  3. package/dist/adapters/fetch/index.d.ts +16 -33
  4. package/dist/adapters/fetch/index.mjs +32 -30
  5. package/dist/adapters/message-port/index.d.mts +13 -8
  6. package/dist/adapters/message-port/index.d.ts +13 -8
  7. package/dist/adapters/message-port/index.mjs +22 -17
  8. package/dist/adapters/standard/index.d.mts +9 -10
  9. package/dist/adapters/standard/index.d.ts +9 -10
  10. package/dist/adapters/standard/index.mjs +5 -5
  11. package/dist/adapters/websocket/index.d.mts +21 -16
  12. package/dist/adapters/websocket/index.d.ts +21 -16
  13. package/dist/adapters/websocket/index.mjs +14 -14
  14. package/dist/index.d.mts +62 -61
  15. package/dist/index.d.ts +62 -61
  16. package/dist/index.mjs +47 -54
  17. package/dist/plugins/index.d.mts +134 -13
  18. package/dist/plugins/index.d.ts +134 -13
  19. package/dist/plugins/index.mjs +298 -27
  20. package/dist/shared/{client.Cby_-GGh.d.mts → client.BWkR2vPU.d.mts} +2 -2
  21. package/dist/shared/client.BYHnU_Z3.mjs +469 -0
  22. package/dist/shared/{client.CPF3hX6O.d.ts → client.BnpRXnLi.d.ts} +2 -2
  23. package/dist/shared/{client.8ug8I-zu.d.mts → client.BoXZCzuS.d.mts} +56 -16
  24. package/dist/shared/{client.8ug8I-zu.d.ts → client.BoXZCzuS.d.ts} +56 -16
  25. package/dist/shared/{client.Dnfj8jnT.mjs → client.Dtv1JPkU.mjs} +19 -15
  26. package/dist/shared/{client.DXhchJ84.mjs → client.VXsLhJtT.mjs} +25 -30
  27. package/dist/shared/{client.BdItY5DT.d.mts → client.n8K9dFhY.d.mts} +25 -4
  28. package/dist/shared/{client.BdItY5DT.d.ts → client.n8K9dFhY.d.ts} +25 -4
  29. package/package.json +20 -8
  30. package/dist/shared/client.DMXKFDyV.mjs +0 -343
package/dist/index.d.mts CHANGED
@@ -1,15 +1,12 @@
1
- import { O as ORPCErrorCode, c as ORPCError, d as ORPCErrorJSON, A as AnyORPCError, C as ClientContext, a as ClientOptions, e as AnyNestedClient, I as InferClientContext, f as InferClientError, g as Client, b as ClientLink, h as ClientRest, F as FriendlyClientOptions } from './shared/client.8ug8I-zu.mjs';
2
- export { i as AnyORPCErrorJSON, j as COMMON_ERROR_STATUS_MAP, k as InferClientBodyInputs, l as InferClientBodyOutputs, m as InferClientErrors, n as InferClientInputs, o as InferClientOutputs, N as NestedClient, p as ORPCErrorOptions } from './shared/client.8ug8I-zu.mjs';
3
- import { Interceptor, PromiseWithError, ThrowableError, Promisable, WrapAsyncIteratorOptions, AsyncIteratorClass } from '@orpc/shared';
4
- export { AsyncCleanupFn, AsyncIteratorClass, AsyncIteratorClassNextFn, MaybeOptionalOptions, PromiseWithError, Registry, ThrowableError, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onError, onFinish, onStart, onSuccess, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
5
- export { R as RPCJsonSerialization, a as RPCJsonSerializationMeta, b as RPCJsonSerializer, c as RPCJsonSerializerHandler, d as RPCJsonSerializerOptions, e as RPCSerializer, f as RPCSerializerOptions, g as RPCSerializerSerializeOptions } from './shared/client.BdItY5DT.mjs';
6
- export { ErrorEvent, EventMeta, getEventMeta, unwrapEvent, withEventMeta } from '@standardserver/core';
1
+ import { WrapAsyncIteratorOptions, AsyncIteratorClass, Interceptor, PromiseWithError, ThrowableError, Promisable } from '@orpc/shared';
2
+ export { AsyncCleanupFn, AsyncIteratorClass, AsyncIteratorClassNextFn, PromiseWithError as ClientPromiseResult, MaybeOptionalOptions, PromiseWithError, Registry, ThrowableError, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, consumeAsyncIterator, consumeAsyncIterator as consumeEventIterator, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess, streamToAsyncIteratorObject, streamToAsyncIteratorObject as streamToEventIterator } from '@orpc/shared';
3
+ import { C as ClientContext, a as ClientOptions, c as AnyNestedClient, I as InferClientContext, d as InferClientError, e as Client, b as ClientLink, A as AnyORPCError, f as ClientRest, F as FriendlyClientOptions, O as ORPCErrorCode, g as ORPCErrorJSON, h as ORPCError, M as MalformedResponseErrorOptions } from './shared/client.BoXZCzuS.mjs';
4
+ export { i as AnyORPCErrorJSON, j as COMMON_ERROR_STATUS_MAP, k as InferClientBodyInputs, l as InferClientBodyOutputs, m as InferClientErrors, n as InferClientInputs, o as InferClientOutputs, p as MalformedResponseError, N as NestedClient, q as ORPCErrorOptions } from './shared/client.BoXZCzuS.mjs';
5
+ import { StandardResponse } from '@standard-server/core';
6
+ export { ErrorEvent, EventMeta, StandardBody, StandardBodyHint, StandardHeaders, StandardLazyRequest, StandardLazyResponse, StandardMethod, StandardRequest, StandardResponse, StandardUrl, getEventMeta, unwrapEvent, withEventMeta } from '@standard-server/core';
7
+ export { R as RPCJsonSerialization, a as RPCJsonSerializationMeta, b as RPCJsonSerializer, c as RPCJsonSerializerHandler, d as RPCJsonSerializerOptions, e as RPCSerializer, f as RPCSerializerOptions, g as RPCSerializerSerializeOptions } from './shared/client.n8K9dFhY.mjs';
7
8
 
8
- declare function isInferableError<T>(error: T): error is Extract<T, AnyORPCError>;
9
- declare function toORPCError<T>(error: T): Extract<T, AnyORPCError> | ORPCError<'INTERNAL_SERVER_ERROR', undefined>;
10
- declare function isORPCErrorJson(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
11
- declare function createORPCErrorFromJson<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
12
- declare function cloneORPCError<T extends ORPCErrorCode, TData>(error: ORPCError<T, TData>): ORPCError<T, TData>;
9
+ declare function wrapAsyncIteratorPreservingEventMeta<TYield, TReturn, TMappedYield = TYield, TMappedReturn = TReturn>(iterator: AsyncIterator<TYield, TReturn>, { mapResult, mapError, ...rest }: WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>): AsyncIteratorClass<TMappedYield, TMappedReturn>;
13
10
 
14
11
  interface ORPCClientInterceptorOptions<TClientContext extends ClientContext, TInput> extends ClientOptions<TClientContext> {
15
12
  path: string[];
@@ -41,68 +38,49 @@ interface ORPCClientOptions<T extends AnyNestedClient> {
41
38
  */
42
39
  scoped?: ORPCClientScoped<T>;
43
40
  }
41
+ /**
42
+ * Creates a fully typed oRPC client from a link.
43
+ * The returned client mirrors the shape of your router or contract,
44
+ * so calling a procedure is as simple as calling a function.
45
+ *
46
+ * @see {@link https://orpc.dev/docs/client/client-side | Client-Side Clients}
47
+ */
44
48
  declare function createORPCClient<T extends AnyNestedClient>(link: ClientLink<InferClientContext<T>>, { path, ...options }?: NoInfer<ORPCClientOptions<T>>): T;
45
49
 
46
50
  declare function resolveFriendlyClientOptions<T extends ClientContext>(options: FriendlyClientOptions<T>): ClientOptions<T>;
47
51
  declare function resolveClientRest<TClientContext extends ClientContext, TInput>(rest: ClientRest<TClientContext, TInput>): [input: TInput, options: ClientOptions<TClientContext>];
48
- type SafeResult<TOutput, TError> = [error: null, data: TOutput, inferableError: null, isSuccess: true] & {
52
+ type SafeResult<TOutput, TError> = [error: null, data: TOutput, definedError: null, isSuccess: true] & {
49
53
  error: null;
50
54
  data: TOutput;
51
- inferableError: null;
55
+ definedError: null;
52
56
  isSuccess: true;
53
- } | [error: Exclude<TError, AnyORPCError>, data: undefined, inferableError: null, isSuccess: false] & {
57
+ } | [error: Exclude<TError, AnyORPCError>, data: undefined, definedError: null, isSuccess: false] & {
54
58
  error: Exclude<TError, AnyORPCError>;
55
59
  data: undefined;
56
- inferableError: null;
60
+ definedError: null;
57
61
  isSuccess: false;
58
- } | [error: Extract<TError, AnyORPCError>, data: undefined, inferableError: Extract<TError, AnyORPCError>, isSuccess: false] & {
62
+ } | [error: Extract<TError, AnyORPCError>, data: undefined, definedError: Extract<TError, AnyORPCError>, isSuccess: false] & {
59
63
  error: Extract<TError, AnyORPCError>;
60
64
  data: undefined;
61
- inferableError: Extract<TError, AnyORPCError>;
65
+ definedError: Extract<TError, AnyORPCError>;
62
66
  isSuccess: false;
63
67
  };
64
68
  /**
65
- * Works like try/catch, but help you infer the error type if it is inferable ORPCError.
69
+ * Works like try/catch, but help you infer the error type if it is a defined ORPCError.
66
70
  *
67
71
  * @example
68
72
  * ```ts
69
- * const [error, data, inferableError, isSuccess] = await safe(client(...))
70
- * // or const { error, data, inferableError, isSuccess } = await safe(client(...))
73
+ * const [error, data, definedError, isSuccess] = await safe(client(...))
74
+ * // or const { error, data, definedError, isSuccess } = await safe(client(...))
71
75
  *
72
- * if (inferableError) {
73
- * console.log(inferableError) // or error, both are well typed
76
+ * if (definedError) {
77
+ * console.log(definedError) // or error, both are well typed
74
78
  * }
75
- */
76
- declare function safe<TOutput, TError = ThrowableError>(promise: PromiseWithError<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
77
- interface ConsumeEventIteratorOptions<T, TReturn, TError> {
78
- /**
79
- * Called on each event
80
- */
81
- onEvent: (event: T) => void;
82
- /**
83
- * Called once error happens
84
- */
85
- onError?: (error: TError) => void;
86
- /**
87
- * Called once event iterator is done
88
- *
89
- * @info If iterator is canceled, `undefined` can be passed on success
90
- */
91
- onSuccess?: (value: TReturn | undefined) => void;
92
- /**
93
- * Called once after onError or onSuccess
94
- *
95
- * @info If iterator is canceled, `undefined` can be passed on success
96
- */
97
- onFinish?: (state: [error: TError, data: undefined, isSuccess: false] | [error: null, data: TReturn | undefined, isSuccess: true]) => void;
98
- }
99
- /**
100
- * Consumes an event iterator with lifecycle callbacks
79
+ * ```
101
80
  *
102
- * @warning If no `onError` or `onFinish` is provided, error will be thrown into unhandled rejection channel.
103
- * @return unsubscribe callback
81
+ * @see {@link https://orpc.dev/docs/client/error-handling#using-safe-and-isdefinederror | Client Error Handling - Using safe and isDefinedError}
104
82
  */
105
- declare function consumeEventIterator<T, TReturn, TError = ThrowableError>(iterator: AsyncIterator<T, TReturn> | PromiseWithError<AsyncIterator<T, TReturn>, TError>, options: ConsumeEventIteratorOptions<T, TReturn, TError>): () => Promise<void>;
83
+ declare function safe<TOutput, TError = ThrowableError>(promise: PromiseWithError<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
106
84
 
107
85
  type SafeClient<T extends AnyNestedClient> = T extends Client<infer UContext, infer UInput, infer UOutput, infer UError> ? (...rest: ClientRest<UContext, UInput>) => Promise<SafeResult<UOutput, UError>> : {
108
86
  [K in keyof T]: T[K] extends AnyNestedClient ? SafeClient<T[K]> : never;
@@ -113,11 +91,11 @@ type SafeClient<T extends AnyNestedClient> = T extends Client<infer UContext, in
113
91
  * @example
114
92
  * ```ts
115
93
  * const safeClient = createSafeClient(client)
116
- * const { error, data, inferrableError, isSuccess } = await safeClient.doSomething({ id: '123' })
117
- * // or const [error, data, inferrableError, isSuccess] = await safeClient.doSomething({ id: '123' })
94
+ * const { error, data, definedError, isSuccess } = await safeClient.doSomething({ id: '123' })
95
+ * // or const [error, data, definedError, isSuccess] = await safeClient.doSomething({ id: '123' })
118
96
  * ```
119
97
  *
120
- * @see {@link https://orpc.dev/docs/client/error-handling#using-createsafeclient Safe Client Docs}
98
+ * @see {@link https://orpc.dev/docs/client/error-handling#safe-client | Client Error Handling - Safe Client}
121
99
  */
122
100
  declare function createSafeClient<T extends AnyNestedClient>(client: T): SafeClient<T>;
123
101
 
@@ -136,7 +114,7 @@ declare const RECURSIVE_CLIENT_UNWRAP_KEYS: Set<string>;
136
114
  * DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
137
115
  * based on the request path, input, and context.
138
116
  *
139
- * @see {@link https://orpc.dev/docs/client/dynamic-link Dynamic Link Docs}
117
+ * @see {@link https://orpc.dev/docs/client/dynamic-link | DynamicLink}
140
118
  */
141
119
  declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
142
120
  private readonly linkResolver;
@@ -144,12 +122,35 @@ declare class DynamicLink<TClientContext extends ClientContext> implements Clien
144
122
  call(path: string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
145
123
  }
146
124
 
147
- declare function wrapEventIteratorPreservingMeta<TYield, TReturn, TMappedYield = TYield, TMappedReturn = TReturn>(iterator: AsyncIterator<TYield, TReturn>, { mapResult, mapError, ...rest }: WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>): AsyncIteratorClass<TMappedYield, TMappedReturn>;
148
-
149
125
  /**
150
- * @deprecated Use `isInferableError` instead.
126
+ * Checks if an error is an `ORPCError` that matches a definition in the procedure's `.errors` map,
127
+ * narrowing it so `code` and `data` are fully typed.
128
+ *
129
+ * @see {@link https://orpc.dev/docs/client/error-handling#using-safe-and-isdefinederror | Client Error Handling - Using safe and isDefinedError}
130
+ */
131
+ declare function isDefinedError<T>(error: T): error is Extract<T, AnyORPCError>;
132
+ declare function toORPCError<T>(error: T): Extract<T, AnyORPCError> | ORPCError<'INTERNAL_SERVER_ERROR', undefined>;
133
+ declare function isORPCErrorJson(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
134
+ declare function createORPCErrorFromJson<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
135
+ /**
136
+ * Creates the `MALFORMED_ORPC_RESPONSE` `ORPCError` used when a response
137
+ * does not follow the expected oRPC format. Unless overridden via `options.message`,
138
+ * the message is inferred from the response body or status. The `cause` is a
139
+ * `MalformedResponseError` carrying the resolved response.
140
+ *
141
+ * @see {@link https://orpc.dev/docs/rpc/link#malformed-responses | RPC Link - Malformed Responses}
142
+ * @see {@link https://orpc.dev/docs/openapi/link#malformed-responses | OpenAPI Link - Malformed Responses}
143
+ */
144
+ declare function createORPCErrorFromMalformedResponse(options: MalformedResponseErrorOptions): ORPCError<'MALFORMED_ORPC_RESPONSE', StandardResponse>;
145
+ /**
146
+ * Clones an `ORPCError` while preserving its prototype chain, so instances of
147
+ * `ORPCError` subclasses remain `instanceof` their class.
148
+ *
149
+ * Limitation: subclass constructors are not re-run, so private fields
150
+ * (`#field`) are not carried over and subclass members that read them
151
+ * will throw on the clone.
151
152
  */
152
- declare const isDefinedError: typeof isInferableError;
153
+ declare function cloneORPCError<T extends AnyORPCError>(error: T): T;
153
154
 
154
- export { AnyNestedClient, AnyORPCError, Client, ClientContext, ClientLink, ClientOptions, ClientRest, DynamicLink, FriendlyClientOptions, InferClientContext, InferClientError, ORPCError, ORPCErrorCode, ORPCErrorJSON, RECURSIVE_CLIENT_UNWRAP_KEYS, cloneORPCError, consumeEventIterator, createORPCClient, createORPCErrorFromJson, createSafeClient, isDefinedError, isInferableError, isORPCErrorJson, resolveClientRest, resolveFriendlyClientOptions, safe, toORPCError, wrapEventIteratorPreservingMeta };
155
- export type { ConsumeEventIteratorOptions, ORPCClientInterceptor, ORPCClientInterceptorOptions, ORPCClientOptions, ORPCClientScoped, ORPCClientScopedOptions, SafeClient, SafeResult };
155
+ export { AnyNestedClient, AnyORPCError, Client, ClientContext, ClientLink, ClientOptions, ClientRest, DynamicLink, FriendlyClientOptions, InferClientContext, InferClientError, InferClientError as InferClientErrorUnion, MalformedResponseErrorOptions, ORPCError, ORPCErrorCode, ORPCErrorJSON, RECURSIVE_CLIENT_UNWRAP_KEYS, cloneORPCError, createORPCClient, createORPCErrorFromJson, createORPCErrorFromMalformedResponse, createSafeClient, isDefinedError, isORPCErrorJson, resolveClientRest, resolveFriendlyClientOptions, safe, toORPCError, wrapAsyncIteratorPreservingEventMeta };
156
+ export type { ORPCClientInterceptor, ORPCClientInterceptorOptions, ORPCClientOptions, ORPCClientScoped, ORPCClientScopedOptions, SafeClient, SafeResult };
package/dist/index.d.ts CHANGED
@@ -1,15 +1,12 @@
1
- import { O as ORPCErrorCode, c as ORPCError, d as ORPCErrorJSON, A as AnyORPCError, C as ClientContext, a as ClientOptions, e as AnyNestedClient, I as InferClientContext, f as InferClientError, g as Client, b as ClientLink, h as ClientRest, F as FriendlyClientOptions } from './shared/client.8ug8I-zu.js';
2
- export { i as AnyORPCErrorJSON, j as COMMON_ERROR_STATUS_MAP, k as InferClientBodyInputs, l as InferClientBodyOutputs, m as InferClientErrors, n as InferClientInputs, o as InferClientOutputs, N as NestedClient, p as ORPCErrorOptions } from './shared/client.8ug8I-zu.js';
3
- import { Interceptor, PromiseWithError, ThrowableError, Promisable, WrapAsyncIteratorOptions, AsyncIteratorClass } from '@orpc/shared';
4
- export { AsyncCleanupFn, AsyncIteratorClass, AsyncIteratorClassNextFn, MaybeOptionalOptions, PromiseWithError, Registry, ThrowableError, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onError, onFinish, onStart, onSuccess, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
5
- export { R as RPCJsonSerialization, a as RPCJsonSerializationMeta, b as RPCJsonSerializer, c as RPCJsonSerializerHandler, d as RPCJsonSerializerOptions, e as RPCSerializer, f as RPCSerializerOptions, g as RPCSerializerSerializeOptions } from './shared/client.BdItY5DT.js';
6
- export { ErrorEvent, EventMeta, getEventMeta, unwrapEvent, withEventMeta } from '@standardserver/core';
1
+ import { WrapAsyncIteratorOptions, AsyncIteratorClass, Interceptor, PromiseWithError, ThrowableError, Promisable } from '@orpc/shared';
2
+ export { AsyncCleanupFn, AsyncIteratorClass, AsyncIteratorClassNextFn, PromiseWithError as ClientPromiseResult, MaybeOptionalOptions, PromiseWithError, Registry, ThrowableError, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, consumeAsyncIterator, consumeAsyncIterator as consumeEventIterator, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess, streamToAsyncIteratorObject, streamToAsyncIteratorObject as streamToEventIterator } from '@orpc/shared';
3
+ import { C as ClientContext, a as ClientOptions, c as AnyNestedClient, I as InferClientContext, d as InferClientError, e as Client, b as ClientLink, A as AnyORPCError, f as ClientRest, F as FriendlyClientOptions, O as ORPCErrorCode, g as ORPCErrorJSON, h as ORPCError, M as MalformedResponseErrorOptions } from './shared/client.BoXZCzuS.js';
4
+ export { i as AnyORPCErrorJSON, j as COMMON_ERROR_STATUS_MAP, k as InferClientBodyInputs, l as InferClientBodyOutputs, m as InferClientErrors, n as InferClientInputs, o as InferClientOutputs, p as MalformedResponseError, N as NestedClient, q as ORPCErrorOptions } from './shared/client.BoXZCzuS.js';
5
+ import { StandardResponse } from '@standard-server/core';
6
+ export { ErrorEvent, EventMeta, StandardBody, StandardBodyHint, StandardHeaders, StandardLazyRequest, StandardLazyResponse, StandardMethod, StandardRequest, StandardResponse, StandardUrl, getEventMeta, unwrapEvent, withEventMeta } from '@standard-server/core';
7
+ export { R as RPCJsonSerialization, a as RPCJsonSerializationMeta, b as RPCJsonSerializer, c as RPCJsonSerializerHandler, d as RPCJsonSerializerOptions, e as RPCSerializer, f as RPCSerializerOptions, g as RPCSerializerSerializeOptions } from './shared/client.n8K9dFhY.js';
7
8
 
8
- declare function isInferableError<T>(error: T): error is Extract<T, AnyORPCError>;
9
- declare function toORPCError<T>(error: T): Extract<T, AnyORPCError> | ORPCError<'INTERNAL_SERVER_ERROR', undefined>;
10
- declare function isORPCErrorJson(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
11
- declare function createORPCErrorFromJson<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
12
- declare function cloneORPCError<T extends ORPCErrorCode, TData>(error: ORPCError<T, TData>): ORPCError<T, TData>;
9
+ declare function wrapAsyncIteratorPreservingEventMeta<TYield, TReturn, TMappedYield = TYield, TMappedReturn = TReturn>(iterator: AsyncIterator<TYield, TReturn>, { mapResult, mapError, ...rest }: WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>): AsyncIteratorClass<TMappedYield, TMappedReturn>;
13
10
 
14
11
  interface ORPCClientInterceptorOptions<TClientContext extends ClientContext, TInput> extends ClientOptions<TClientContext> {
15
12
  path: string[];
@@ -41,68 +38,49 @@ interface ORPCClientOptions<T extends AnyNestedClient> {
41
38
  */
42
39
  scoped?: ORPCClientScoped<T>;
43
40
  }
41
+ /**
42
+ * Creates a fully typed oRPC client from a link.
43
+ * The returned client mirrors the shape of your router or contract,
44
+ * so calling a procedure is as simple as calling a function.
45
+ *
46
+ * @see {@link https://orpc.dev/docs/client/client-side | Client-Side Clients}
47
+ */
44
48
  declare function createORPCClient<T extends AnyNestedClient>(link: ClientLink<InferClientContext<T>>, { path, ...options }?: NoInfer<ORPCClientOptions<T>>): T;
45
49
 
46
50
  declare function resolveFriendlyClientOptions<T extends ClientContext>(options: FriendlyClientOptions<T>): ClientOptions<T>;
47
51
  declare function resolveClientRest<TClientContext extends ClientContext, TInput>(rest: ClientRest<TClientContext, TInput>): [input: TInput, options: ClientOptions<TClientContext>];
48
- type SafeResult<TOutput, TError> = [error: null, data: TOutput, inferableError: null, isSuccess: true] & {
52
+ type SafeResult<TOutput, TError> = [error: null, data: TOutput, definedError: null, isSuccess: true] & {
49
53
  error: null;
50
54
  data: TOutput;
51
- inferableError: null;
55
+ definedError: null;
52
56
  isSuccess: true;
53
- } | [error: Exclude<TError, AnyORPCError>, data: undefined, inferableError: null, isSuccess: false] & {
57
+ } | [error: Exclude<TError, AnyORPCError>, data: undefined, definedError: null, isSuccess: false] & {
54
58
  error: Exclude<TError, AnyORPCError>;
55
59
  data: undefined;
56
- inferableError: null;
60
+ definedError: null;
57
61
  isSuccess: false;
58
- } | [error: Extract<TError, AnyORPCError>, data: undefined, inferableError: Extract<TError, AnyORPCError>, isSuccess: false] & {
62
+ } | [error: Extract<TError, AnyORPCError>, data: undefined, definedError: Extract<TError, AnyORPCError>, isSuccess: false] & {
59
63
  error: Extract<TError, AnyORPCError>;
60
64
  data: undefined;
61
- inferableError: Extract<TError, AnyORPCError>;
65
+ definedError: Extract<TError, AnyORPCError>;
62
66
  isSuccess: false;
63
67
  };
64
68
  /**
65
- * Works like try/catch, but help you infer the error type if it is inferable ORPCError.
69
+ * Works like try/catch, but help you infer the error type if it is a defined ORPCError.
66
70
  *
67
71
  * @example
68
72
  * ```ts
69
- * const [error, data, inferableError, isSuccess] = await safe(client(...))
70
- * // or const { error, data, inferableError, isSuccess } = await safe(client(...))
73
+ * const [error, data, definedError, isSuccess] = await safe(client(...))
74
+ * // or const { error, data, definedError, isSuccess } = await safe(client(...))
71
75
  *
72
- * if (inferableError) {
73
- * console.log(inferableError) // or error, both are well typed
76
+ * if (definedError) {
77
+ * console.log(definedError) // or error, both are well typed
74
78
  * }
75
- */
76
- declare function safe<TOutput, TError = ThrowableError>(promise: PromiseWithError<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
77
- interface ConsumeEventIteratorOptions<T, TReturn, TError> {
78
- /**
79
- * Called on each event
80
- */
81
- onEvent: (event: T) => void;
82
- /**
83
- * Called once error happens
84
- */
85
- onError?: (error: TError) => void;
86
- /**
87
- * Called once event iterator is done
88
- *
89
- * @info If iterator is canceled, `undefined` can be passed on success
90
- */
91
- onSuccess?: (value: TReturn | undefined) => void;
92
- /**
93
- * Called once after onError or onSuccess
94
- *
95
- * @info If iterator is canceled, `undefined` can be passed on success
96
- */
97
- onFinish?: (state: [error: TError, data: undefined, isSuccess: false] | [error: null, data: TReturn | undefined, isSuccess: true]) => void;
98
- }
99
- /**
100
- * Consumes an event iterator with lifecycle callbacks
79
+ * ```
101
80
  *
102
- * @warning If no `onError` or `onFinish` is provided, error will be thrown into unhandled rejection channel.
103
- * @return unsubscribe callback
81
+ * @see {@link https://orpc.dev/docs/client/error-handling#using-safe-and-isdefinederror | Client Error Handling - Using safe and isDefinedError}
104
82
  */
105
- declare function consumeEventIterator<T, TReturn, TError = ThrowableError>(iterator: AsyncIterator<T, TReturn> | PromiseWithError<AsyncIterator<T, TReturn>, TError>, options: ConsumeEventIteratorOptions<T, TReturn, TError>): () => Promise<void>;
83
+ declare function safe<TOutput, TError = ThrowableError>(promise: PromiseWithError<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
106
84
 
107
85
  type SafeClient<T extends AnyNestedClient> = T extends Client<infer UContext, infer UInput, infer UOutput, infer UError> ? (...rest: ClientRest<UContext, UInput>) => Promise<SafeResult<UOutput, UError>> : {
108
86
  [K in keyof T]: T[K] extends AnyNestedClient ? SafeClient<T[K]> : never;
@@ -113,11 +91,11 @@ type SafeClient<T extends AnyNestedClient> = T extends Client<infer UContext, in
113
91
  * @example
114
92
  * ```ts
115
93
  * const safeClient = createSafeClient(client)
116
- * const { error, data, inferrableError, isSuccess } = await safeClient.doSomething({ id: '123' })
117
- * // or const [error, data, inferrableError, isSuccess] = await safeClient.doSomething({ id: '123' })
94
+ * const { error, data, definedError, isSuccess } = await safeClient.doSomething({ id: '123' })
95
+ * // or const [error, data, definedError, isSuccess] = await safeClient.doSomething({ id: '123' })
118
96
  * ```
119
97
  *
120
- * @see {@link https://orpc.dev/docs/client/error-handling#using-createsafeclient Safe Client Docs}
98
+ * @see {@link https://orpc.dev/docs/client/error-handling#safe-client | Client Error Handling - Safe Client}
121
99
  */
122
100
  declare function createSafeClient<T extends AnyNestedClient>(client: T): SafeClient<T>;
123
101
 
@@ -136,7 +114,7 @@ declare const RECURSIVE_CLIENT_UNWRAP_KEYS: Set<string>;
136
114
  * DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
137
115
  * based on the request path, input, and context.
138
116
  *
139
- * @see {@link https://orpc.dev/docs/client/dynamic-link Dynamic Link Docs}
117
+ * @see {@link https://orpc.dev/docs/client/dynamic-link | DynamicLink}
140
118
  */
141
119
  declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
142
120
  private readonly linkResolver;
@@ -144,12 +122,35 @@ declare class DynamicLink<TClientContext extends ClientContext> implements Clien
144
122
  call(path: string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
145
123
  }
146
124
 
147
- declare function wrapEventIteratorPreservingMeta<TYield, TReturn, TMappedYield = TYield, TMappedReturn = TReturn>(iterator: AsyncIterator<TYield, TReturn>, { mapResult, mapError, ...rest }: WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>): AsyncIteratorClass<TMappedYield, TMappedReturn>;
148
-
149
125
  /**
150
- * @deprecated Use `isInferableError` instead.
126
+ * Checks if an error is an `ORPCError` that matches a definition in the procedure's `.errors` map,
127
+ * narrowing it so `code` and `data` are fully typed.
128
+ *
129
+ * @see {@link https://orpc.dev/docs/client/error-handling#using-safe-and-isdefinederror | Client Error Handling - Using safe and isDefinedError}
130
+ */
131
+ declare function isDefinedError<T>(error: T): error is Extract<T, AnyORPCError>;
132
+ declare function toORPCError<T>(error: T): Extract<T, AnyORPCError> | ORPCError<'INTERNAL_SERVER_ERROR', undefined>;
133
+ declare function isORPCErrorJson(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
134
+ declare function createORPCErrorFromJson<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
135
+ /**
136
+ * Creates the `MALFORMED_ORPC_RESPONSE` `ORPCError` used when a response
137
+ * does not follow the expected oRPC format. Unless overridden via `options.message`,
138
+ * the message is inferred from the response body or status. The `cause` is a
139
+ * `MalformedResponseError` carrying the resolved response.
140
+ *
141
+ * @see {@link https://orpc.dev/docs/rpc/link#malformed-responses | RPC Link - Malformed Responses}
142
+ * @see {@link https://orpc.dev/docs/openapi/link#malformed-responses | OpenAPI Link - Malformed Responses}
143
+ */
144
+ declare function createORPCErrorFromMalformedResponse(options: MalformedResponseErrorOptions): ORPCError<'MALFORMED_ORPC_RESPONSE', StandardResponse>;
145
+ /**
146
+ * Clones an `ORPCError` while preserving its prototype chain, so instances of
147
+ * `ORPCError` subclasses remain `instanceof` their class.
148
+ *
149
+ * Limitation: subclass constructors are not re-run, so private fields
150
+ * (`#field`) are not carried over and subclass members that read them
151
+ * will throw on the clone.
151
152
  */
152
- declare const isDefinedError: typeof isInferableError;
153
+ declare function cloneORPCError<T extends AnyORPCError>(error: T): T;
153
154
 
154
- export { AnyNestedClient, AnyORPCError, Client, ClientContext, ClientLink, ClientOptions, ClientRest, DynamicLink, FriendlyClientOptions, InferClientContext, InferClientError, ORPCError, ORPCErrorCode, ORPCErrorJSON, RECURSIVE_CLIENT_UNWRAP_KEYS, cloneORPCError, consumeEventIterator, createORPCClient, createORPCErrorFromJson, createSafeClient, isDefinedError, isInferableError, isORPCErrorJson, resolveClientRest, resolveFriendlyClientOptions, safe, toORPCError, wrapEventIteratorPreservingMeta };
155
- export type { ConsumeEventIteratorOptions, ORPCClientInterceptor, ORPCClientInterceptorOptions, ORPCClientOptions, ORPCClientScoped, ORPCClientScopedOptions, SafeClient, SafeResult };
155
+ export { AnyNestedClient, AnyORPCError, Client, ClientContext, ClientLink, ClientOptions, ClientRest, DynamicLink, FriendlyClientOptions, InferClientContext, InferClientError, InferClientError as InferClientErrorUnion, MalformedResponseErrorOptions, ORPCError, ORPCErrorCode, ORPCErrorJSON, RECURSIVE_CLIENT_UNWRAP_KEYS, cloneORPCError, createORPCClient, createORPCErrorFromJson, createORPCErrorFromMalformedResponse, createSafeClient, isDefinedError, isORPCErrorJson, resolveClientRest, resolveFriendlyClientOptions, safe, toORPCError, wrapAsyncIteratorPreservingEventMeta };
156
+ export type { ORPCClientInterceptor, ORPCClientInterceptorOptions, ORPCClientOptions, ORPCClientScoped, ORPCClientScopedOptions, SafeClient, SafeResult };
package/dist/index.mjs CHANGED
@@ -1,9 +1,9 @@
1
- import { a as isInferableError } from './shared/client.DMXKFDyV.mjs';
2
- export { b as RPCJsonSerializer, R as RPCSerializer, d as cloneORPCError, c as createORPCErrorFromJson, i as isORPCErrorJson, t as toORPCError, w as wrapEventIteratorPreservingMeta } from './shared/client.DMXKFDyV.mjs';
3
- import { getOrBind, toArray, intercept, isTypescriptObject } from '@orpc/shared';
4
- export { AsyncIteratorClass, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onError, onFinish, onStart, onSuccess, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
5
- export { C as COMMON_ERROR_STATUS_MAP, O as ORPCError } from './shared/client.Dnfj8jnT.mjs';
6
- export { ErrorEvent, getEventMeta, unwrapEvent, withEventMeta } from '@standardserver/core';
1
+ import { b as isDefinedError } from './shared/client.BYHnU_Z3.mjs';
2
+ export { d as RPCJsonSerializer, R as RPCSerializer, e as cloneORPCError, a as createORPCErrorFromJson, c as createORPCErrorFromMalformedResponse, i as isORPCErrorJson, t as toORPCError, w as wrapAsyncIteratorPreservingEventMeta } from './shared/client.BYHnU_Z3.mjs';
3
+ import { toArray, intercept, isTypescriptObject } from '@orpc/shared';
4
+ export { AsyncIteratorClass, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, consumeAsyncIterator, consumeAsyncIterator as consumeEventIterator, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess, streamToAsyncIteratorObject, streamToAsyncIteratorObject as streamToEventIterator } from '@orpc/shared';
5
+ export { C as COMMON_ERROR_STATUS_MAP, M as MalformedResponseError, O as ORPCError } from './shared/client.Dtv1JPkU.mjs';
6
+ export { ErrorEvent, getEventMeta, unwrapEvent, withEventMeta } from '@standard-server/core';
7
7
 
8
8
  const RECURSIVE_CLIENT_UNWRAP_KEYS = /* @__PURE__ */ new Set([
9
9
  /**
@@ -16,6 +16,16 @@ const RECURSIVE_CLIENT_UNWRAP_KEYS = /* @__PURE__ */ new Set([
16
16
  * context.
17
17
  */
18
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",
19
29
  /**
20
30
  * Commonly accessed during primitive conversion, inspection, and logging.
21
31
  */
@@ -50,51 +60,22 @@ async function safe(promise) {
50
60
  const output = await promise;
51
61
  return Object.assign(
52
62
  [null, output, null, true],
53
- { error: null, data: output, inferableError: null, isSuccess: true }
63
+ { error: null, data: output, definedError: null, isSuccess: true }
54
64
  );
55
65
  } catch (e) {
56
66
  const error = e;
57
- if (isInferableError(error)) {
67
+ if (isDefinedError(error)) {
58
68
  return Object.assign(
59
69
  [error, void 0, error, false],
60
- { error, data: void 0, inferableError: error, isSuccess: false }
70
+ { error, data: void 0, definedError: error, isSuccess: false }
61
71
  );
62
72
  }
63
73
  return Object.assign(
64
74
  [error, void 0, null, false],
65
- { error, data: void 0, inferableError: null, isSuccess: false }
75
+ { error, data: void 0, definedError: null, isSuccess: false }
66
76
  );
67
77
  }
68
78
  }
69
- function consumeEventIterator(iterator, options) {
70
- void (async () => {
71
- let onFinishState;
72
- try {
73
- const resolvedIterator = await iterator;
74
- while (true) {
75
- const { done, value } = await resolvedIterator.next();
76
- if (done) {
77
- const realValue = value;
78
- onFinishState = [null, realValue, true];
79
- options.onSuccess?.(realValue);
80
- break;
81
- }
82
- options.onEvent(value);
83
- }
84
- } catch (error) {
85
- onFinishState = [error, void 0, false];
86
- if (!options.onError && !options.onFinish) {
87
- throw error;
88
- }
89
- options.onError?.(error);
90
- } finally {
91
- options.onFinish?.(onFinishState);
92
- }
93
- })();
94
- return async () => {
95
- await (await iterator)?.return?.();
96
- };
97
- }
98
79
 
99
80
  function createORPCClient(link, { path = [], ...options } = {}) {
100
81
  const procedureClient = (...rest) => {
@@ -109,30 +90,44 @@ function createORPCClient(link, { path = [], ...options } = {}) {
109
90
  ({ path: path2, input: input2, ...callOptions2 }) => link.call(path2, input2, callOptions2)
110
91
  );
111
92
  };
93
+ const cache = /* @__PURE__ */ new Map();
112
94
  const recursive = new Proxy(procedureClient, {
113
95
  get(target, key) {
114
96
  if (typeof key !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(key)) {
115
- return getOrBind(target, 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);
116
107
  }
117
- const scoped = options.scoped === void 0 ? void 0 : options.scoped[key];
118
- return createORPCClient(link, {
119
- ...options,
120
- path: [...path, key],
121
- scoped
122
- });
108
+ return client;
123
109
  }
124
110
  });
125
111
  return recursive;
126
112
  }
127
113
 
128
114
  function createSafeClient(client) {
115
+ const cache = /* @__PURE__ */ new Map();
129
116
  const proxy = new Proxy((...args) => safe(client(...args)), {
130
- get(_, prop) {
131
- const value = getOrBind(client, prop);
132
- if (!isTypescriptObject(value)) {
133
- return value;
117
+ get(target, prop) {
118
+ if (typeof prop !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop)) {
119
+ return Reflect.get(target, prop);
134
120
  }
135
- return createSafeClient(value);
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;
136
131
  }
137
132
  });
138
133
  return proxy;
@@ -149,6 +144,4 @@ class DynamicLink {
149
144
  }
150
145
  }
151
146
 
152
- const isDefinedError = isInferableError;
153
-
154
- export { DynamicLink, RECURSIVE_CLIENT_UNWRAP_KEYS, consumeEventIterator, createORPCClient, createSafeClient, isDefinedError, isInferableError, resolveClientRest, resolveFriendlyClientOptions, safe };
147
+ export { DynamicLink, RECURSIVE_CLIENT_UNWRAP_KEYS, createORPCClient, createSafeClient, isDefinedError, resolveClientRest, resolveFriendlyClientOptions, safe };