@orpc/client 0.0.0-next.85df466 → 0.0.0-next.8606faa

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 (33) hide show
  1. package/README.md +33 -20
  2. package/dist/adapters/fetch/index.d.mts +38 -95
  3. package/dist/adapters/fetch/index.d.ts +38 -95
  4. package/dist/adapters/fetch/index.mjs +35 -110
  5. package/dist/adapters/message-port/index.d.mts +80 -0
  6. package/dist/adapters/message-port/index.d.ts +80 -0
  7. package/dist/adapters/message-port/index.mjs +87 -0
  8. package/dist/adapters/standard/index.d.mts +9 -20
  9. package/dist/adapters/standard/index.d.ts +9 -20
  10. package/dist/adapters/standard/index.mjs +4 -2
  11. package/dist/adapters/websocket/index.d.mts +29 -0
  12. package/dist/adapters/websocket/index.d.ts +29 -0
  13. package/dist/adapters/websocket/index.mjs +47 -0
  14. package/dist/index.d.mts +107 -30
  15. package/dist/index.d.ts +107 -30
  16. package/dist/index.mjs +85 -36
  17. package/dist/plugins/index.d.mts +249 -0
  18. package/dist/plugins/index.d.ts +249 -0
  19. package/dist/plugins/index.mjs +485 -0
  20. package/dist/shared/client.BBB2AT0F.mjs +171 -0
  21. package/dist/shared/client.BH1AYT_p.d.mts +83 -0
  22. package/dist/shared/client.BH1AYT_p.d.ts +83 -0
  23. package/dist/shared/client.BLtwTQUg.mjs +40 -0
  24. package/dist/shared/client.BxV-mzeR.d.ts +91 -0
  25. package/dist/shared/client.CPgZaUox.d.mts +45 -0
  26. package/dist/shared/client.Cju0ZXxP.mjs +398 -0
  27. package/dist/shared/client.D8lMmWVC.d.mts +91 -0
  28. package/dist/shared/client.De8SW4Kw.d.ts +45 -0
  29. package/package.json +21 -5
  30. package/dist/shared/client.DHJ8vRIG.mjs +0 -192
  31. package/dist/shared/client.D_CzLDyB.d.mts +0 -42
  32. package/dist/shared/client.D_CzLDyB.d.ts +0 -42
  33. package/dist/shared/client.Ly4zGQrc.mjs +0 -265
@@ -0,0 +1,80 @@
1
+ import { Value, Promisable } from '@orpc/shared';
2
+ import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
3
+ import { DecodedRequestMessage } from '@orpc/standard-server-peer';
4
+ import { b as ClientContext, c as ClientOptions } from '../../shared/client.BH1AYT_p.js';
5
+ import { f as StandardLinkClient } from '../../shared/client.De8SW4Kw.js';
6
+ import { f as StandardRPCLinkOptions, g as StandardRPCLink } from '../../shared/client.BxV-mzeR.js';
7
+
8
+ /**
9
+ * The message port used by electron in main process
10
+ */
11
+ interface MessagePortMainLike {
12
+ on: <T extends string>(event: T, callback: (event?: {
13
+ data: any;
14
+ }) => void) => void;
15
+ postMessage: (data: any, transfer?: any[]) => void;
16
+ }
17
+ /**
18
+ * The message port used by browser extension
19
+ */
20
+ interface BrowserPortLike {
21
+ onMessage: {
22
+ addListener: (callback: (data: any) => void) => void;
23
+ };
24
+ onDisconnect: {
25
+ addListener: (callback: () => void) => void;
26
+ };
27
+ postMessage: (data: any) => void;
28
+ }
29
+ type SupportedMessagePort = Pick<MessagePort, 'addEventListener' | 'postMessage'> | MessagePortMainLike | BrowserPortLike;
30
+ /**
31
+ * Message port can support [The structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
32
+ */
33
+ type SupportedMessagePortData = any;
34
+ declare function postMessagePortMessage(port: SupportedMessagePort, data: SupportedMessagePortData, transfer?: any[]): void;
35
+ declare function onMessagePortMessage(port: SupportedMessagePort, callback: (data: SupportedMessagePortData) => void): void;
36
+ declare function onMessagePortClose(port: SupportedMessagePort, callback: () => void): void;
37
+
38
+ interface LinkMessagePortClientOptions {
39
+ port: SupportedMessagePort;
40
+ /**
41
+ * By default, oRPC serializes request/response messages to string/binary data before sending over message port.
42
+ * If needed, you can define the this option to utilize full power of [MessagePort: postMessage() method](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/postMessage),
43
+ * such as transferring ownership of objects to the other side or support unserializable objects like `OffscreenCanvas`.
44
+ *
45
+ * @remarks
46
+ * - return null | undefined to disable this feature
47
+ *
48
+ * @warning Make sure your message port supports `transfer` before using this feature.
49
+ * @example
50
+ * ```ts
51
+ * experimental_transfer: (message, port) => {
52
+ * const transfer = deepFindTransferableObjects(message) // implement your own logic
53
+ * return transfer.length ? transfer : null // only enable when needed
54
+ * }
55
+ * ```
56
+ *
57
+ * @see {@link https://orpc.unnoq.com/docs/adapters/message-port#transfer Message Port Transfer Docs}
58
+ */
59
+ experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedRequestMessage, port: SupportedMessagePort]>;
60
+ }
61
+ declare class LinkMessagePortClient<T extends ClientContext> implements StandardLinkClient<T> {
62
+ private readonly peer;
63
+ constructor(options: LinkMessagePortClientOptions);
64
+ call(request: StandardRequest, _options: ClientOptions<T>, _path: readonly string[], _input: unknown): Promise<StandardLazyResponse>;
65
+ }
66
+
67
+ interface RPCLinkOptions<T extends ClientContext> extends Omit<StandardRPCLinkOptions<T>, 'url' | 'method' | 'fallbackMethod' | 'maxUrlLength'>, LinkMessagePortClientOptions {
68
+ }
69
+ /**
70
+ * The RPC Link for common message port implementations.
71
+ *
72
+ * @see {@link https://orpc.unnoq.com/docs/client/rpc-link RPC Link Docs}
73
+ * @see {@link https://orpc.unnoq.com/docs/adapters/message-port Message Port Adapter Docs}
74
+ */
75
+ declare class RPCLink<T extends ClientContext> extends StandardRPCLink<T> {
76
+ constructor(options: RPCLinkOptions<T>);
77
+ }
78
+
79
+ export { LinkMessagePortClient, RPCLink, onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
80
+ export type { BrowserPortLike, LinkMessagePortClientOptions, MessagePortMainLike, RPCLinkOptions, SupportedMessagePort, SupportedMessagePortData };
@@ -0,0 +1,87 @@
1
+ import { value, isObject } from '@orpc/shared';
2
+ import { experimental_ClientPeerWithoutCodec, serializeRequestMessage, encodeRequestMessage, deserializeResponseMessage, decodeResponseMessage } from '@orpc/standard-server-peer';
3
+ import { c as StandardRPCLink } from '../../shared/client.Cju0ZXxP.mjs';
4
+ import '@orpc/standard-server';
5
+ import '../../shared/client.BBB2AT0F.mjs';
6
+ import '@orpc/standard-server-fetch';
7
+ import '../../shared/client.BLtwTQUg.mjs';
8
+
9
+ function postMessagePortMessage(port, data, transfer) {
10
+ if (transfer) {
11
+ port.postMessage(data, transfer);
12
+ } else {
13
+ port.postMessage(data);
14
+ }
15
+ }
16
+ function onMessagePortMessage(port, callback) {
17
+ if ("addEventListener" in port) {
18
+ port.addEventListener("message", (event) => {
19
+ callback(event.data);
20
+ });
21
+ } else if ("on" in port) {
22
+ port.on("message", (event) => {
23
+ callback(event?.data);
24
+ });
25
+ } else if ("onMessage" in port) {
26
+ port.onMessage.addListener((data) => {
27
+ callback(data);
28
+ });
29
+ } else {
30
+ throw new Error("Cannot find a addEventListener/on/onMessage method on the port");
31
+ }
32
+ }
33
+ function onMessagePortClose(port, callback) {
34
+ if ("addEventListener" in port) {
35
+ port.addEventListener("close", async () => {
36
+ callback();
37
+ });
38
+ } else if ("on" in port) {
39
+ port.on("close", async () => {
40
+ callback();
41
+ });
42
+ } else if ("onDisconnect" in port) {
43
+ port.onDisconnect.addListener(() => {
44
+ callback();
45
+ });
46
+ } else {
47
+ throw new Error("Cannot find a addEventListener/on/onDisconnect method on the port");
48
+ }
49
+ }
50
+
51
+ class LinkMessagePortClient {
52
+ peer;
53
+ constructor(options) {
54
+ this.peer = new experimental_ClientPeerWithoutCodec(async (message) => {
55
+ const [id, type, payload] = message;
56
+ const transfer = await value(options.experimental_transfer, message, options.port);
57
+ if (transfer) {
58
+ postMessagePortMessage(options.port, serializeRequestMessage(id, type, payload), transfer);
59
+ } else {
60
+ postMessagePortMessage(options.port, await encodeRequestMessage(id, type, payload));
61
+ }
62
+ });
63
+ onMessagePortMessage(options.port, async (message) => {
64
+ if (isObject(message)) {
65
+ await this.peer.message(deserializeResponseMessage(message));
66
+ } else {
67
+ await this.peer.message(await decodeResponseMessage(message));
68
+ }
69
+ });
70
+ onMessagePortClose(options.port, () => {
71
+ this.peer.close();
72
+ });
73
+ }
74
+ async call(request, _options, _path, _input) {
75
+ const response = await this.peer.request(request);
76
+ return { ...response, body: () => Promise.resolve(response.body) };
77
+ }
78
+ }
79
+
80
+ class RPCLink extends StandardRPCLink {
81
+ constructor(options) {
82
+ const linkClient = new LinkMessagePortClient(options);
83
+ super(linkClient, { ...options, url: "orpc://localhost" });
84
+ }
85
+ }
86
+
87
+ export { LinkMessagePortClient, RPCLink, onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
@@ -1,22 +1,11 @@
1
- import { Segment } from '@orpc/shared';
1
+ export { C as CompositeStandardLinkPlugin, d as StandardLink, f as StandardLinkClient, S as StandardLinkClientInterceptorOptions, e as StandardLinkCodec, c as StandardLinkInterceptorOptions, b as StandardLinkOptions, a as StandardLinkPlugin } from '../../shared/client.CPgZaUox.mjs';
2
+ export { S as STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES, c as StandardRPCCustomJsonSerializer, b as StandardRPCJsonSerialized, a as StandardRPCJsonSerializedMetaItem, e as StandardRPCJsonSerializer, d as StandardRPCJsonSerializerOptions, g as StandardRPCLink, i as StandardRPCLinkCodec, h as StandardRPCLinkCodecOptions, f as StandardRPCLinkOptions, j as StandardRPCSerializer } from '../../shared/client.D8lMmWVC.mjs';
3
+ import { StandardHeaders } from '@orpc/standard-server';
4
+ import { H as HTTPPath } from '../../shared/client.BH1AYT_p.mjs';
5
+ import '@orpc/shared';
2
6
 
3
- type RPCJsonSerializedMeta = [
4
- 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7,
5
- Segment[]
6
- ][];
7
- type RPCJsonSerialized = [json: unknown, meta: RPCJsonSerializedMeta, maps: Segment[][], blobs: Blob[]];
8
- declare class RPCJsonSerializer {
9
- serialize(data: unknown, segments?: Segment[], meta?: RPCJsonSerializedMeta, maps?: Segment[][], blobs?: Blob[]): RPCJsonSerialized;
10
- deserialize(json: unknown, meta: RPCJsonSerializedMeta): unknown;
11
- deserialize(json: unknown, meta: RPCJsonSerializedMeta, maps: Segment[][], getBlob: (index: number) => Blob): unknown;
12
- }
7
+ declare function toHttpPath(path: readonly string[]): HTTPPath;
8
+ declare function toStandardHeaders(headers: Headers | StandardHeaders): StandardHeaders;
9
+ declare function getMalformedResponseErrorCode(status: number): string;
13
10
 
14
- declare class RPCSerializer {
15
- #private;
16
- private readonly jsonSerializer;
17
- constructor(jsonSerializer?: RPCJsonSerializer);
18
- serialize(data: unknown): unknown;
19
- deserialize(data: unknown): unknown;
20
- }
21
-
22
- export { type RPCJsonSerialized, type RPCJsonSerializedMeta, RPCJsonSerializer, RPCSerializer };
11
+ export { getMalformedResponseErrorCode, toHttpPath, toStandardHeaders };
@@ -1,22 +1,11 @@
1
- import { Segment } from '@orpc/shared';
1
+ export { C as CompositeStandardLinkPlugin, d as StandardLink, f as StandardLinkClient, S as StandardLinkClientInterceptorOptions, e as StandardLinkCodec, c as StandardLinkInterceptorOptions, b as StandardLinkOptions, a as StandardLinkPlugin } from '../../shared/client.De8SW4Kw.js';
2
+ export { S as STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES, c as StandardRPCCustomJsonSerializer, b as StandardRPCJsonSerialized, a as StandardRPCJsonSerializedMetaItem, e as StandardRPCJsonSerializer, d as StandardRPCJsonSerializerOptions, g as StandardRPCLink, i as StandardRPCLinkCodec, h as StandardRPCLinkCodecOptions, f as StandardRPCLinkOptions, j as StandardRPCSerializer } from '../../shared/client.BxV-mzeR.js';
3
+ import { StandardHeaders } from '@orpc/standard-server';
4
+ import { H as HTTPPath } from '../../shared/client.BH1AYT_p.js';
5
+ import '@orpc/shared';
2
6
 
3
- type RPCJsonSerializedMeta = [
4
- 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7,
5
- Segment[]
6
- ][];
7
- type RPCJsonSerialized = [json: unknown, meta: RPCJsonSerializedMeta, maps: Segment[][], blobs: Blob[]];
8
- declare class RPCJsonSerializer {
9
- serialize(data: unknown, segments?: Segment[], meta?: RPCJsonSerializedMeta, maps?: Segment[][], blobs?: Blob[]): RPCJsonSerialized;
10
- deserialize(json: unknown, meta: RPCJsonSerializedMeta): unknown;
11
- deserialize(json: unknown, meta: RPCJsonSerializedMeta, maps: Segment[][], getBlob: (index: number) => Blob): unknown;
12
- }
7
+ declare function toHttpPath(path: readonly string[]): HTTPPath;
8
+ declare function toStandardHeaders(headers: Headers | StandardHeaders): StandardHeaders;
9
+ declare function getMalformedResponseErrorCode(status: number): string;
13
10
 
14
- declare class RPCSerializer {
15
- #private;
16
- private readonly jsonSerializer;
17
- constructor(jsonSerializer?: RPCJsonSerializer);
18
- serialize(data: unknown): unknown;
19
- deserialize(data: unknown): unknown;
20
- }
21
-
22
- export { type RPCJsonSerialized, type RPCJsonSerializedMeta, RPCJsonSerializer, RPCSerializer };
11
+ export { getMalformedResponseErrorCode, toHttpPath, toStandardHeaders };
@@ -1,4 +1,6 @@
1
- export { R as RPCJsonSerializer, a as RPCSerializer } from '../../shared/client.DHJ8vRIG.mjs';
1
+ export { C as CompositeStandardLinkPlugin, a as STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES, S as StandardLink, b as StandardRPCJsonSerializer, c as StandardRPCLink, d as StandardRPCLinkCodec, e as StandardRPCSerializer, g as getMalformedResponseErrorCode, t as toHttpPath, f as toStandardHeaders } from '../../shared/client.Cju0ZXxP.mjs';
2
2
  import '@orpc/shared';
3
3
  import '@orpc/standard-server';
4
- import '../../shared/client.Ly4zGQrc.mjs';
4
+ import '../../shared/client.BBB2AT0F.mjs';
5
+ import '@orpc/standard-server-fetch';
6
+ import '../../shared/client.BLtwTQUg.mjs';
@@ -0,0 +1,29 @@
1
+ import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
2
+ import { b as ClientContext, c as ClientOptions } from '../../shared/client.BH1AYT_p.mjs';
3
+ import { f as StandardLinkClient } from '../../shared/client.CPgZaUox.mjs';
4
+ import { f as StandardRPCLinkOptions, g as StandardRPCLink } from '../../shared/client.D8lMmWVC.mjs';
5
+ import '@orpc/shared';
6
+
7
+ interface LinkWebsocketClientOptions {
8
+ websocket: Pick<WebSocket, 'addEventListener' | 'send' | 'readyState'>;
9
+ }
10
+ declare class LinkWebsocketClient<T extends ClientContext> implements StandardLinkClient<T> {
11
+ private readonly peer;
12
+ constructor(options: LinkWebsocketClientOptions);
13
+ call(request: StandardRequest, _options: ClientOptions<T>, _path: readonly string[], _input: unknown): Promise<StandardLazyResponse>;
14
+ }
15
+
16
+ interface RPCLinkOptions<T extends ClientContext> extends Omit<StandardRPCLinkOptions<T>, 'url' | 'method' | 'fallbackMethod' | 'maxUrlLength'>, LinkWebsocketClientOptions {
17
+ }
18
+ /**
19
+ * The RPC Link communicates with the server using the RPC protocol over WebSocket.
20
+ *
21
+ * @see {@link https://orpc.unnoq.com/docs/client/rpc-link RPC Link Docs}
22
+ * @see {@link https://orpc.unnoq.com/docs/adapters/websocket WebSocket Adapter Docs}
23
+ */
24
+ declare class RPCLink<T extends ClientContext> extends StandardRPCLink<T> {
25
+ constructor(options: RPCLinkOptions<T>);
26
+ }
27
+
28
+ export { LinkWebsocketClient, RPCLink };
29
+ export type { LinkWebsocketClientOptions, RPCLinkOptions };
@@ -0,0 +1,29 @@
1
+ import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
2
+ import { b as ClientContext, c as ClientOptions } from '../../shared/client.BH1AYT_p.js';
3
+ import { f as StandardLinkClient } from '../../shared/client.De8SW4Kw.js';
4
+ import { f as StandardRPCLinkOptions, g as StandardRPCLink } from '../../shared/client.BxV-mzeR.js';
5
+ import '@orpc/shared';
6
+
7
+ interface LinkWebsocketClientOptions {
8
+ websocket: Pick<WebSocket, 'addEventListener' | 'send' | 'readyState'>;
9
+ }
10
+ declare class LinkWebsocketClient<T extends ClientContext> implements StandardLinkClient<T> {
11
+ private readonly peer;
12
+ constructor(options: LinkWebsocketClientOptions);
13
+ call(request: StandardRequest, _options: ClientOptions<T>, _path: readonly string[], _input: unknown): Promise<StandardLazyResponse>;
14
+ }
15
+
16
+ interface RPCLinkOptions<T extends ClientContext> extends Omit<StandardRPCLinkOptions<T>, 'url' | 'method' | 'fallbackMethod' | 'maxUrlLength'>, LinkWebsocketClientOptions {
17
+ }
18
+ /**
19
+ * The RPC Link communicates with the server using the RPC protocol over WebSocket.
20
+ *
21
+ * @see {@link https://orpc.unnoq.com/docs/client/rpc-link RPC Link Docs}
22
+ * @see {@link https://orpc.unnoq.com/docs/adapters/websocket WebSocket Adapter Docs}
23
+ */
24
+ declare class RPCLink<T extends ClientContext> extends StandardRPCLink<T> {
25
+ constructor(options: RPCLinkOptions<T>);
26
+ }
27
+
28
+ export { LinkWebsocketClient, RPCLink };
29
+ export type { LinkWebsocketClientOptions, RPCLinkOptions };
@@ -0,0 +1,47 @@
1
+ import { readAsBuffer } from '@orpc/shared';
2
+ import { ClientPeer } from '@orpc/standard-server-peer';
3
+ import { c as StandardRPCLink } from '../../shared/client.Cju0ZXxP.mjs';
4
+ import '@orpc/standard-server';
5
+ import '../../shared/client.BBB2AT0F.mjs';
6
+ import '@orpc/standard-server-fetch';
7
+ import '../../shared/client.BLtwTQUg.mjs';
8
+
9
+ const WEBSOCKET_CONNECTING = 0;
10
+ class LinkWebsocketClient {
11
+ peer;
12
+ constructor(options) {
13
+ const untilOpen = new Promise((resolve) => {
14
+ if (options.websocket.readyState === WEBSOCKET_CONNECTING) {
15
+ options.websocket.addEventListener("open", () => {
16
+ resolve();
17
+ }, { once: true });
18
+ } else {
19
+ resolve();
20
+ }
21
+ });
22
+ this.peer = new ClientPeer(async (message) => {
23
+ await untilOpen;
24
+ return options.websocket.send(message);
25
+ });
26
+ options.websocket.addEventListener("message", async (event) => {
27
+ const message = event.data instanceof Blob ? await readAsBuffer(event.data) : event.data;
28
+ this.peer.message(message);
29
+ });
30
+ options.websocket.addEventListener("close", () => {
31
+ this.peer.close();
32
+ });
33
+ }
34
+ async call(request, _options, _path, _input) {
35
+ const response = await this.peer.request(request);
36
+ return { ...response, body: () => Promise.resolve(response.body) };
37
+ }
38
+ }
39
+
40
+ class RPCLink extends StandardRPCLink {
41
+ constructor(options) {
42
+ const linkClient = new LinkWebsocketClient(options);
43
+ super(linkClient, { ...options, url: "orpc://localhost" });
44
+ }
45
+ }
46
+
47
+ export { LinkWebsocketClient, RPCLink };
package/dist/index.d.mts CHANGED
@@ -1,25 +1,21 @@
1
- import { N as NestedClient, b as ClientLink, I as InferClientContext, C as ClientContext, a as ClientOptionsOut, c as ClientPromiseResult } from './shared/client.D_CzLDyB.mjs';
2
- export { g as Client, e as ClientOptions, f as ClientRest, E as EventIteratorReconnectOptions, d as createAutoRetryEventIterator, m as mapEventIterator } from './shared/client.D_CzLDyB.mjs';
3
- import { Promisable, MaybeOptionalOptions } from '@orpc/shared';
1
+ import { N as NestedClient, C as ClientLink, I as InferClientContext, a as ClientPromiseResult, b as ClientContext, F as FriendlyClientOptions, c as ClientOptions, d as Client, e as ClientRest } from './shared/client.BH1AYT_p.mjs';
2
+ export { f as HTTPMethod, H as HTTPPath, h as InferClientBodyInputs, j as InferClientBodyOutputs, l as InferClientErrorUnion, k as InferClientErrors, g as InferClientInputs, i as InferClientOutputs } from './shared/client.BH1AYT_p.mjs';
3
+ import { MaybeOptionalOptions, ThrowableError, OnFinishState, Promisable, AsyncIteratorClass } from '@orpc/shared';
4
+ export { AsyncIteratorClass, EventPublisher, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, Registry, ThrowableError, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onError, onFinish, onStart, onSuccess, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
4
5
  export { ErrorEvent } from '@orpc/standard-server';
5
6
 
6
7
  interface createORPCClientOptions {
7
8
  /**
8
9
  * Use as base path for all procedure, useful when you only want to call a subset of the procedure.
9
10
  */
10
- path?: string[];
11
+ path?: readonly string[];
11
12
  }
12
- declare function createORPCClient<T extends NestedClient<any>>(link: ClientLink<InferClientContext<T>>, options?: createORPCClientOptions): T;
13
-
14
13
  /**
15
- * DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
16
- * based on the request path, input, and context.
14
+ * Create a oRPC client-side client from a link.
15
+ *
16
+ * @see {@link https://orpc.unnoq.com/docs/client/client-side Client-side Client Docs}
17
17
  */
18
- declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
19
- private readonly linkResolver;
20
- constructor(linkResolver: (options: ClientOptionsOut<TClientContext>, path: readonly string[], input: unknown) => Promisable<ClientLink<TClientContext>>);
21
- call(path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>): Promise<unknown>;
22
- }
18
+ declare function createORPCClient<T extends NestedClient<any>>(link: ClientLink<InferClientContext<T>>, options?: createORPCClientOptions): T;
23
19
 
24
20
  declare const COMMON_ORPC_ERROR_DEFS: {
25
21
  readonly BAD_REQUEST: {
@@ -117,37 +113,118 @@ declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
117
113
  readonly code: TCode;
118
114
  readonly status: number;
119
115
  readonly data: TData;
120
- constructor(code: TCode, ...[options]: MaybeOptionalOptions<ORPCErrorOptions<TData>>);
116
+ constructor(code: TCode, ...rest: MaybeOptionalOptions<ORPCErrorOptions<TData>>);
121
117
  toJSON(): ORPCErrorJSON<TCode, TData>;
122
- static fromJSON<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
123
- static isValidJSON(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
118
+ /**
119
+ * Workaround for Next.js where different contexts use separate
120
+ * dependency graphs, causing multiple ORPCError constructors existing and breaking
121
+ * `instanceof` checks across contexts.
122
+ *
123
+ * This is particularly problematic with "Optimized SSR", where orpc-client
124
+ * executes in one context but is invoked from another. When an error is thrown
125
+ * in the execution context, `instanceof ORPCError` checks fail in the
126
+ * invocation context due to separate class constructors.
127
+ *
128
+ * @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
129
+ */
130
+ static [Symbol.hasInstance](instance: unknown): boolean;
124
131
  }
125
132
  type ORPCErrorJSON<TCode extends string, TData> = Pick<ORPCError<TCode, TData>, 'defined' | 'code' | 'status' | 'message' | 'data'>;
126
133
  declare function isDefinedError<T>(error: T): error is Extract<T, ORPCError<any, any>>;
127
134
  declare function toORPCError(error: unknown): ORPCError<any, any>;
135
+ declare function isORPCErrorStatus(status: number): boolean;
136
+ declare function isORPCErrorJson(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
137
+ declare function createORPCErrorFromJson<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
128
138
 
129
- type ConnectionStatus = 'reconnecting' | 'connected' | 'closed';
130
- interface EventIteratorState {
131
- status: ConnectionStatus;
132
- listeners: Array<(newStatus: ConnectionStatus) => void>;
133
- }
134
- declare function registerEventIteratorState(iterator: AsyncIteratorObject<unknown, unknown, void>, state: EventIteratorState): void;
135
- declare function updateEventIteratorStatus(state: EventIteratorState, status: ConnectionStatus): void;
136
- declare function onEventIteratorStatusChange(iterator: AsyncIteratorObject<unknown, unknown, void>, callback: (status: ConnectionStatus) => void, notifyImmediately?: boolean): () => void;
137
-
138
- type SafeResult<TOutput, TError extends Error> = [error: null, data: TOutput, isDefined: false] & {
139
+ type SafeResult<TOutput, TError> = [error: null, data: TOutput, isDefined: false, isSuccess: true] & {
139
140
  error: null;
140
141
  data: TOutput;
141
142
  isDefined: false;
142
- } | [error: Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false] & {
143
+ isSuccess: true;
144
+ } | [error: Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false, isSuccess: false] & {
143
145
  error: Exclude<TError, ORPCError<any, any>>;
144
146
  data: undefined;
145
147
  isDefined: false;
146
- } | [error: Extract<TError, ORPCError<any, any>>, data: undefined, isDefined: true] & {
148
+ isSuccess: false;
149
+ } | [error: Extract<TError, ORPCError<any, any>>, data: undefined, isDefined: true, isSuccess: false] & {
147
150
  error: Extract<TError, ORPCError<any, any>>;
148
151
  data: undefined;
149
152
  isDefined: true;
153
+ isSuccess: false;
150
154
  };
151
- declare function safe<TOutput, TError extends Error>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
155
+ /**
156
+ * Works like try/catch, but can infer error types.
157
+ *
158
+ * @info support both tuple `[error, data, isDefined, isSuccess]` and object `{ error, data, isDefined, isSuccess }` styles.
159
+ * @see {@link https://orpc.unnoq.com/docs/client/error-handling Client Error Handling Docs}
160
+ */
161
+ declare function safe<TOutput, TError = ThrowableError>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
162
+ declare function resolveFriendlyClientOptions<T extends ClientContext>(options: FriendlyClientOptions<T>): ClientOptions<T>;
163
+ interface ConsumeEventIteratorOptions<T, TReturn, TError> {
164
+ /**
165
+ * Called on each event
166
+ */
167
+ onEvent: (event: T) => void;
168
+ /**
169
+ * Called once error happens
170
+ */
171
+ onError?: (error: TError) => void;
172
+ /**
173
+ * Called once event iterator is done
174
+ *
175
+ * @info If iterator is canceled, `undefined` can be passed on success
176
+ */
177
+ onSuccess?: (value: TReturn | undefined) => void;
178
+ /**
179
+ * Called once after onError or onSuccess
180
+ *
181
+ * @info If iterator is canceled, `undefined` can be passed on success
182
+ */
183
+ onFinish?: (state: OnFinishState<TReturn | undefined, TError>) => void;
184
+ }
185
+ /**
186
+ * Consumes an event iterator with lifecycle callbacks
187
+ *
188
+ * @warning If no `onError` or `onFinish` is provided, unhandled rejections will be thrown
189
+ * @return unsubscribe callback
190
+ */
191
+ declare function consumeEventIterator<T, TReturn, TError = ThrowableError>(iterator: AsyncIterator<T, TReturn> | ClientPromiseResult<AsyncIterator<T, TReturn>, TError>, options: ConsumeEventIteratorOptions<T, TReturn, TError>): () => Promise<void>;
192
+
193
+ type SafeClient<T extends NestedClient<any>> = T extends Client<infer UContext, infer UInput, infer UOutput, infer UError> ? (...rest: ClientRest<UContext, UInput>) => Promise<SafeResult<UOutput, UError>> : {
194
+ [K in keyof T]: T[K] extends NestedClient<any> ? SafeClient<T[K]> : never;
195
+ };
196
+ /**
197
+ * Create a safe client that automatically wraps all procedure calls with the `safe` util.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * const safeClient = createSafeClient(client)
202
+ * const { error, data, isDefined } = await safeClient.doSomething({ id: '123' })
203
+ * ```
204
+ *
205
+ * @see {@link https://orpc.unnoq.com/docs/client/error-handling#using-createsafeclient Safe Client Docs}
206
+ */
207
+ declare function createSafeClient<T extends NestedClient<any>>(client: T): SafeClient<T>;
208
+
209
+ declare const ORPC_CLIENT_PACKAGE_NAME = "@orpc/client";
210
+ declare const ORPC_CLIENT_PACKAGE_VERSION = "0.0.0-next.8606faa";
211
+
212
+ /**
213
+ * DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
214
+ * based on the request path, input, and context.
215
+ *
216
+ * @see {@link https://orpc.unnoq.com/docs/client/dynamic-link Dynamic Link Docs}
217
+ */
218
+ declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
219
+ private readonly linkResolver;
220
+ constructor(linkResolver: (options: ClientOptions<TClientContext>, path: readonly string[], input: unknown) => Promisable<ClientLink<TClientContext>>);
221
+ call(path: readonly string[], input: unknown, options: ClientOptions<TClientContext>): Promise<unknown>;
222
+ }
223
+
224
+ declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
225
+ value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
226
+ error: (error: unknown) => Promise<unknown>;
227
+ }): AsyncIteratorClass<TMap, TMap, TNext>;
152
228
 
153
- export { COMMON_ORPC_ERROR_DEFS, ClientContext, ClientLink, ClientOptionsOut, ClientPromiseResult, type CommonORPCErrorCode, type ConnectionStatus, DynamicLink, type EventIteratorState, InferClientContext, NestedClient, ORPCError, type ORPCErrorCode, type ORPCErrorJSON, type ORPCErrorOptions, type SafeResult, createORPCClient, type createORPCClientOptions, fallbackORPCErrorMessage, fallbackORPCErrorStatus, isDefinedError, onEventIteratorStatusChange, registerEventIteratorState, safe, toORPCError, updateEventIteratorStatus };
229
+ export { COMMON_ORPC_ERROR_DEFS, Client, ClientContext, ClientLink, ClientOptions, ClientPromiseResult, ClientRest, DynamicLink, FriendlyClientOptions, InferClientContext, NestedClient, ORPCError, ORPC_CLIENT_PACKAGE_NAME, ORPC_CLIENT_PACKAGE_VERSION, consumeEventIterator, createORPCClient, createORPCErrorFromJson, createSafeClient, fallbackORPCErrorMessage, fallbackORPCErrorStatus, isDefinedError, isORPCErrorJson, isORPCErrorStatus, mapEventIterator, resolveFriendlyClientOptions, safe, toORPCError };
230
+ export type { CommonORPCErrorCode, ConsumeEventIteratorOptions, ORPCErrorCode, ORPCErrorJSON, ORPCErrorOptions, SafeClient, SafeResult, createORPCClientOptions };