@orpc/client 2.0.0-beta.5 → 2.0.0-beta.50

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 (94) hide show
  1. package/README.md +137 -102
  2. package/dist/adapters/fetch/index.d.ts +3 -65
  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 +17 -0
  6. package/dist/adapters/fetch/transport.d.ts +32 -0
  7. package/dist/adapters/fetch/transport.js +51 -0
  8. package/dist/adapters/message-port/index.d.ts +4 -75
  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 +38 -0
  12. package/dist/adapters/message-port/rpc-link.d.ts +15 -0
  13. package/dist/adapters/message-port/rpc-link.js +18 -0
  14. package/dist/adapters/message-port/transport.d.ts +35 -0
  15. package/dist/adapters/message-port/transport.js +38 -0
  16. package/dist/adapters/standard/codec.d.ts +14 -0
  17. package/dist/adapters/standard/index.d.ts +6 -61
  18. package/dist/adapters/standard/index.js +2 -0
  19. package/dist/adapters/standard/link.d.ts +40 -0
  20. package/dist/adapters/standard/link.js +101 -0
  21. package/dist/adapters/standard/plugin.d.ts +26 -0
  22. package/dist/adapters/standard/plugin.js +14 -0
  23. package/dist/adapters/standard/rpc-link-codec.d.ts +56 -0
  24. package/dist/adapters/standard/rpc-link-codec.js +100 -0
  25. package/dist/adapters/standard/transport.d.ts +14 -0
  26. package/dist/adapters/websocket/index.d.ts +3 -121
  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 +17 -0
  30. package/dist/adapters/websocket/transport.d.ts +116 -0
  31. package/dist/adapters/websocket/transport.js +105 -0
  32. package/dist/async-iterator-object.d.ts +2 -0
  33. package/dist/async-iterator-object.js +28 -0
  34. package/dist/client-safe.d.ts +16 -0
  35. package/dist/client-safe.js +31 -0
  36. package/dist/client.d.ts +38 -0
  37. package/dist/client.js +37 -0
  38. package/dist/consts.d.ts +10 -0
  39. package/dist/consts.js +20 -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 +20 -0
  43. package/dist/error-utils.js +54 -0
  44. package/dist/error.d.ts +132 -0
  45. package/dist/error.js +166 -0
  46. package/dist/index.d.ts +14 -155
  47. package/dist/index.js +12 -0
  48. package/dist/plugins/batch.d.ts +152 -0
  49. package/dist/plugins/batch.js +330 -0
  50. package/dist/plugins/cookie-jar.d.ts +85 -0
  51. package/dist/plugins/cookie-jar.js +231 -0
  52. package/dist/plugins/dedupe.d.ts +60 -0
  53. package/dist/plugins/dedupe.js +183 -0
  54. package/dist/plugins/index.d.ts +9 -242
  55. package/dist/plugins/index.js +8 -0
  56. package/dist/plugins/request-compression.d.ts +47 -0
  57. package/dist/plugins/request-compression.js +132 -0
  58. package/dist/plugins/response-compression.d.ts +31 -0
  59. package/dist/plugins/response-compression.js +67 -0
  60. package/dist/plugins/retry-after.d.ts +43 -0
  61. package/dist/plugins/retry-after.js +63 -0
  62. package/dist/plugins/retry.d.ts +84 -0
  63. package/dist/plugins/retry.js +121 -0
  64. package/dist/plugins/timeout.d.ts +27 -0
  65. package/dist/plugins/timeout.js +42 -0
  66. package/dist/rpc-json-serializer.d.ts +99 -0
  67. package/dist/rpc-json-serializer.js +347 -0
  68. package/dist/rpc-serializer.d.ts +31 -0
  69. package/dist/rpc-serializer.js +102 -0
  70. package/dist/types.d.ts +85 -0
  71. package/dist/utils.d.ts +36 -0
  72. package/dist/utils.js +67 -0
  73. package/package.json +47 -35
  74. package/dist/adapters/fetch/index.d.mts +0 -65
  75. package/dist/adapters/fetch/index.mjs +0 -68
  76. package/dist/adapters/message-port/index.d.mts +0 -75
  77. package/dist/adapters/message-port/index.mjs +0 -91
  78. package/dist/adapters/standard/index.d.mts +0 -61
  79. package/dist/adapters/standard/index.mjs +0 -6
  80. package/dist/adapters/websocket/index.d.mts +0 -121
  81. package/dist/adapters/websocket/index.mjs +0 -110
  82. package/dist/index.d.mts +0 -155
  83. package/dist/index.mjs +0 -154
  84. package/dist/plugins/index.d.mts +0 -242
  85. package/dist/plugins/index.mjs +0 -574
  86. package/dist/shared/client.8ug8I-zu.d.mts +0 -167
  87. package/dist/shared/client.8ug8I-zu.d.ts +0 -167
  88. package/dist/shared/client.BdItY5DT.d.mts +0 -111
  89. package/dist/shared/client.BdItY5DT.d.ts +0 -111
  90. package/dist/shared/client.CPF3hX6O.d.ts +0 -96
  91. package/dist/shared/client.Cby_-GGh.d.mts +0 -96
  92. package/dist/shared/client.DMXKFDyV.mjs +0 -343
  93. package/dist/shared/client.DXhchJ84.mjs +0 -174
  94. package/dist/shared/client.Dnfj8jnT.mjs +0 -92
@@ -0,0 +1,51 @@
1
+ import { once, value } from "@orpc/shared";
2
+ import { toFetchBody, toFetchHeaders, toStandardLazyResponse } from "@standard-server/fetch";
3
+ // src/adapters/fetch/transport.ts
4
+ const GET_SUPPORTED_DUPLEX_MODE = /* @__PURE__ */ once(() => {
5
+ try {
6
+ let duplex;
7
+ new Request("https://example.com", {
8
+ method: "POST",
9
+ body: new ReadableStream(),
10
+ get duplex() {
11
+ duplex = "half";
12
+ return "half";
13
+ }
14
+ });
15
+ return duplex;
16
+ } catch {
17
+ return;
18
+ }
19
+ });
20
+ var FetchLinkTransport = class {
21
+ origin;
22
+ fetch;
23
+ toFetchRequestOptions;
24
+ constructor(options) {
25
+ this.origin = options.origin;
26
+ this.fetch = options.fetch ?? ((url, init) => globalThis.fetch(url, init));
27
+ this.toFetchRequestOptions = options.toFetchRequest;
28
+ }
29
+ async send(standardRequest, path, options) {
30
+ let origin = await value(this.origin, options, path);
31
+ if (origin?.endsWith("/")) origin = origin.slice(0, -1);
32
+ const url = `${origin ?? ""}${standardRequest.url}`;
33
+ const [body, standardHeaders] = toFetchBody(standardRequest.body, standardRequest.headers, this.toFetchRequestOptions);
34
+ const init = {
35
+ body,
36
+ headers: toFetchHeaders(standardHeaders),
37
+ method: standardRequest.method,
38
+ signal: options.signal
39
+ };
40
+ if (body instanceof ReadableStream) {
41
+ const duplex = GET_SUPPORTED_DUPLEX_MODE();
42
+ if (duplex !== void 0) init.duplex = duplex;
43
+ }
44
+ const fetch = this.fetch;
45
+ const response = await fetch(url, init, options, path);
46
+ if (response.type === "opaqueredirect") throw new TypeError("Unexpected opaque redirect response: browsers hide redirects under `redirect: 'manual'`.");
47
+ if (response.type === "opaque") throw new TypeError("Unexpected opaque response: the response is unreadable, likely due to a no-cors request.");
48
+ return toStandardLazyResponse(response);
49
+ }
50
+ };
51
+ export { FetchLinkTransport };
@@ -1,75 +1,4 @@
1
- import { C as ClientContext, a as ClientOptions } from '../../shared/client.8ug8I-zu.js';
2
- import { Value, Promisable } from '@orpc/shared';
3
- import { StandardRequest, StandardLazyResponse } from '@standardserver/core';
4
- import { ClientPeer, EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standardserver/peer';
5
- import { S as StandardLinkTransport, a as StandardLink, b as StandardLinkOptions } from '../../shared/client.CPF3hX6O.js';
6
- import { RPCLinkCodecOptions } from '../standard/index.js';
7
- import '../../shared/client.BdItY5DT.js';
8
-
9
- /**
10
- * The message port used by electron in main process
11
- */
12
- interface MessagePortMainLike {
13
- on: <T extends string>(event: T, callback: (event?: {
14
- data: any;
15
- }) => void) => void;
16
- postMessage: (data: any, transfer?: any[]) => void;
17
- }
18
- /**
19
- * The message port used by browser extension
20
- */
21
- interface BrowserPortLike {
22
- onMessage: {
23
- addListener: (callback: (data: any) => void) => void;
24
- };
25
- onDisconnect: {
26
- addListener: (callback: () => void) => void;
27
- };
28
- postMessage: (data: any) => void;
29
- }
30
- type SupportedMessagePort = Pick<MessagePort, 'addEventListener' | 'postMessage'> | MessagePortMainLike | BrowserPortLike;
31
- /**
32
- * Message port can support [The structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
33
- */
34
- type SupportedMessagePortData = any;
35
- declare function postMessagePortMessage(port: SupportedMessagePort, data: SupportedMessagePortData, transfer?: any[]): void;
36
- declare function onMessagePortMessage(port: SupportedMessagePort, callback: (data: SupportedMessagePortData) => void): void;
37
- declare function onMessagePortClose(port: SupportedMessagePort, callback: () => void): void;
38
-
39
- type DecodedRequestMessage = ConstructorParameters<typeof ClientPeer>[0] extends (message: infer TMessage) => unknown ? TMessage : never;
40
- interface MessagePortLinkTransportOptions<_T extends ClientContext> {
41
- port: SupportedMessagePort;
42
- /**
43
- * By default, oRPC serializes request/response messages to string/binary data before sending over message port.
44
- * If needed, define this option to utilize full power of [MessagePort: postMessage() method](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/postMessage),
45
- * such as transferring ownership of objects to the other side or support unserializable objects like `OffscreenCanvas`.
46
- *
47
- * @remarks
48
- * - return null | undefined to disable this feature
49
- *
50
- * @warning Make sure your message port supports `transfer` before using this feature.
51
- */
52
- experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedRequestMessage, port: SupportedMessagePort]>;
53
- /**
54
- * Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
55
- */
56
- encodePeerMessage?: EncodePeerMessageOptions | undefined;
57
- /**
58
- * Options for decoding peer messages, such as `prefix` for distinguishing messages on the same channel.
59
- */
60
- decodePeerMessage?: DecodePeerMessageOptions | undefined;
61
- }
62
- declare class MessagePortLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
63
- private readonly peer;
64
- constructor({ port, experimental_transfer, encodePeerMessage: encodePeerMessageOptions, decodePeerMessage: decodePeerMessageOptions }: MessagePortLinkTransportOptions<T>);
65
- send(standardRequest: StandardRequest, _path: string[], _options: ClientOptions<T>): Promise<StandardLazyResponse>;
66
- }
67
-
68
- interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, MessagePortLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
69
- }
70
- declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
71
- constructor(options: RPCLinkOptions<T>);
72
- }
73
-
74
- export { MessagePortLinkTransport, RPCLink, onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
75
- export type { BrowserPortLike, MessagePortLinkTransportOptions, MessagePortMainLike, RPCLinkOptions, SupportedMessagePort, SupportedMessagePortData };
1
+ import { BrowserPortLike, MessagePortMainLike, SupportedMessagePort, SupportedMessagePortData, onMessagePortClose, onMessagePortMessage, postMessagePortMessage } from "./message-port.js";
2
+ import { RPCLink, RPCLinkOptions } from "./rpc-link.js";
3
+ import { MessagePortLinkTransport, MessagePortLinkTransportOptions } from "./transport.js";
4
+ export { BrowserPortLike, MessagePortLinkTransport, MessagePortLinkTransportOptions, MessagePortMainLike, RPCLink, RPCLinkOptions, SupportedMessagePort, SupportedMessagePortData, onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
@@ -0,0 +1,3 @@
1
+ export { onMessagePortClose, onMessagePortMessage, postMessagePortMessage } from "./message-port.js";
2
+ export { RPCLink } from "./rpc-link.js";
3
+ export { MessagePortLinkTransport } from "./transport.js";
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The message port used by electron in main process
3
+ */
4
+ export interface MessagePortMainLike {
5
+ on: <T extends string>(event: T, callback: (event?: {
6
+ data: any;
7
+ }) => void) => void;
8
+ postMessage: (data: any, transfer?: any[]) => void;
9
+ }
10
+ /**
11
+ * The message port used by browser extension
12
+ */
13
+ export interface BrowserPortLike {
14
+ onMessage: {
15
+ addListener: (callback: (data: any) => void) => void;
16
+ };
17
+ onDisconnect: {
18
+ addListener: (callback: () => void) => void;
19
+ };
20
+ postMessage: (data: any) => void;
21
+ }
22
+ export type SupportedMessagePort = Pick<MessagePort, "addEventListener" | "postMessage"> | MessagePortMainLike | BrowserPortLike;
23
+ /**
24
+ * Message port can support [The structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
25
+ */
26
+ export type SupportedMessagePortData = any;
27
+ export declare function postMessagePortMessage(port: SupportedMessagePort, data: SupportedMessagePortData, transfer?: any[]): void;
28
+ export declare function onMessagePortMessage(port: SupportedMessagePort, callback: (data: SupportedMessagePortData) => void): void;
29
+ export declare function onMessagePortClose(port: SupportedMessagePort, callback: () => void): void;
@@ -0,0 +1,38 @@
1
+ // src/adapters/message-port/message-port.ts
2
+ function postMessagePortMessage(port, data, transfer) {
3
+ if (transfer) port.postMessage(data, transfer);
4
+ else port.postMessage(data);
5
+ }
6
+ function onMessagePortMessage(port, callback) {
7
+ /**
8
+ * MessagePort in node.js might have "on" method but it pass different parameters vs MessagePortMainLike
9
+ * So we need check "addEventListener" before "on"
10
+ */
11
+ if ("addEventListener" in port) port.addEventListener("message", (event) => {
12
+ callback(event.data);
13
+ });
14
+ else if ("on" in port) port.on("message", (event) => {
15
+ callback(event?.data);
16
+ });
17
+ else if ("onMessage" in port) port.onMessage.addListener((data) => {
18
+ callback(data);
19
+ });
20
+ else throw new Error("Cannot find a addEventListener/on/onMessage method on the port");
21
+ }
22
+ function onMessagePortClose(port, callback) {
23
+ /**
24
+ * MessagePort in node.js might have "on" method but it pass different parameters vs MessagePortMainLike
25
+ * So we need check "addEventListener" before "on"
26
+ */
27
+ if ("addEventListener" in port) port.addEventListener("close", () => {
28
+ callback();
29
+ });
30
+ else if ("on" in port) port.on("close", () => {
31
+ callback();
32
+ });
33
+ else if ("onDisconnect" in port) port.onDisconnect.addListener(() => {
34
+ callback();
35
+ });
36
+ else throw new Error("Cannot find a addEventListener/on/onDisconnect method on the port");
37
+ }
38
+ export { onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
@@ -0,0 +1,15 @@
1
+ import { ClientContext } from "../../types.js";
2
+ import { StandardLink, StandardLinkOptions } from "../standard/link.js";
3
+ import { RPCLinkCodecOptions } from "../standard/rpc-link-codec.js";
4
+ import "../standard/index.js";
5
+ import { MessagePortLinkTransportOptions } from "./transport.js";
6
+ export interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, MessagePortLinkTransportOptions<T>, RPCLinkCodecOptions<T> {}
7
+ /**
8
+ * Client link that communicates with an RPC Handler over a Message Port
9
+ * (e.g. workers, iframes, browser extensions, Electron).
10
+ *
11
+ * @see {@link https://orpc.dev/docs/adapters/message-port | Message Port Adapter}
12
+ */
13
+ export declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
14
+ constructor(options: RPCLinkOptions<T>);
15
+ }
@@ -0,0 +1,18 @@
1
+ import { RPCLinkCodec } from "../standard/rpc-link-codec.js";
2
+ import { StandardLink } from "../standard/link.js";
3
+ import { MessagePortLinkTransport } from "./transport.js";
4
+ // src/adapters/message-port/rpc-link.ts
5
+ /**
6
+ * Client link that communicates with an RPC Handler over a Message Port
7
+ * (e.g. workers, iframes, browser extensions, Electron).
8
+ *
9
+ * @see {@link https://orpc.dev/docs/adapters/message-port | Message Port Adapter}
10
+ */
11
+ var RPCLink = class extends StandardLink {
12
+ constructor(options) {
13
+ const codec = new RPCLinkCodec(options);
14
+ const transport = new MessagePortLinkTransport(options);
15
+ super(codec, transport, options);
16
+ }
17
+ };
18
+ export { RPCLink };
@@ -0,0 +1,35 @@
1
+ import { ClientContext, ClientOptions } from "../../types.js";
2
+ import { StandardLinkTransport } from "../standard/transport.js";
3
+ import "../standard/index.js";
4
+ import { SupportedMessagePort } from "./message-port.js";
5
+ import { Promisable, Value } from "@orpc/shared";
6
+ import { StandardLazyResponse, StandardRequest } from "@standard-server/core";
7
+ import { ClientPeer, DecodePeerMessageOptions, EncodePeerMessageOptions } from "@standard-server/peer";
8
+ type DecodedRequestMessage = ConstructorParameters<typeof ClientPeer>[0] extends ((message: infer TMessage) => unknown) ? TMessage : never;
9
+ export interface MessagePortLinkTransportOptions<_T extends ClientContext> {
10
+ port: SupportedMessagePort;
11
+ /**
12
+ * By default, oRPC serializes request/response messages to string/binary data before sending over message port.
13
+ * If needed, define this option to utilize full power of [MessagePort: postMessage() method](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/postMessage),
14
+ * such as transferring ownership of objects to the other side or support unserializable objects like `OffscreenCanvas`.
15
+ *
16
+ * @remarks
17
+ * **Note**: Returning `null` or `undefined` disables this feature.
18
+ * **Warning**: Make sure your message port supports `transfer` before using this feature.
19
+ */
20
+ experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedRequestMessage, port: SupportedMessagePort]> | undefined;
21
+ /**
22
+ * Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
23
+ */
24
+ encodePeerMessage?: EncodePeerMessageOptions | undefined;
25
+ /**
26
+ * Options for decoding peer messages, such as `prefix` for distinguishing messages on the same channel.
27
+ */
28
+ decodePeerMessage?: DecodePeerMessageOptions | undefined;
29
+ }
30
+ export declare class MessagePortLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
31
+ private readonly peer;
32
+ constructor({ port, experimental_transfer, encodePeerMessage: encodePeerMessageOptions, decodePeerMessage: decodePeerMessageOptions }: MessagePortLinkTransportOptions<T>);
33
+ send(standardRequest: StandardRequest, _path: string[], _options: ClientOptions<T>): Promise<StandardLazyResponse>;
34
+ }
35
+ export {};
@@ -0,0 +1,38 @@
1
+ import { onMessagePortClose, onMessagePortMessage, postMessagePortMessage } from "./message-port.js";
2
+ import { AbortError, value } from "@orpc/shared";
3
+ import { ClientPeer, decodePeerMessage, encodePeerMessage, isPeerMessage, isServerPeerSendMessage } from "@standard-server/peer";
4
+ // src/adapters/message-port/transport.ts
5
+ var MessagePortLinkTransport = class {
6
+ peer;
7
+ constructor({ port, experimental_transfer, encodePeerMessage: encodePeerMessageOptions, decodePeerMessage: decodePeerMessageOptions }) {
8
+ let closeReason;
9
+ this.peer = new ClientPeer(async (message) => {
10
+ if (closeReason) throw closeReason;
11
+ const transfer = await value(experimental_transfer, message, port);
12
+ if (transfer) postMessagePortMessage(port, message, transfer);
13
+ else postMessagePortMessage(port, await encodePeerMessage(message, encodePeerMessageOptions));
14
+ });
15
+ onMessagePortMessage(port, async (data) => {
16
+ let peerMessage;
17
+ if (typeof data === "string" || data instanceof Uint8Array) {
18
+ const result = decodePeerMessage(data, decodePeerMessageOptions);
19
+ if (result.matched && isServerPeerSendMessage(result.message)) peerMessage = result.message;
20
+ } else if (isPeerMessage(data) && isServerPeerSendMessage(data)) peerMessage = data;
21
+ if (peerMessage === void 0) return { matched: false };
22
+ /**
23
+ * Message order is important: loading -> decode -> .message.
24
+ * This flow must stay synchronous, or we need to use `sequential` helper
25
+ */
26
+ await this.peer.message(peerMessage);
27
+ return { matched: true };
28
+ });
29
+ onMessagePortClose(port, async () => {
30
+ closeReason = new AbortError("MessagePort closed");
31
+ await this.peer.close(closeReason);
32
+ });
33
+ }
34
+ async send(standardRequest, _path, _options) {
35
+ return await this.peer.request(standardRequest);
36
+ }
37
+ };
38
+ export { MessagePortLinkTransport };
@@ -0,0 +1,14 @@
1
+ import { ClientContext, ClientOptions } from "../../types.js";
2
+ import { Promisable } from "@orpc/shared";
3
+ import { StandardLazyResponse, StandardRequest } from "@standard-server/core";
4
+ export type StandardLinkCodecDecodedResponse = {
5
+ kind: "output";
6
+ output: unknown;
7
+ } | {
8
+ kind: "error";
9
+ error: Error;
10
+ };
11
+ export interface StandardLinkCodec<T extends ClientContext> {
12
+ encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promisable<StandardRequest>;
13
+ decodeResponse(response: StandardLazyResponse, path: string[], options: ClientOptions<T>): Promisable<StandardLinkCodecDecodedResponse>;
14
+ }
@@ -1,61 +1,6 @@
1
- import { f as StandardLinkCodec, g as StandardLinkCodecDecodedResponse } from '../../shared/client.CPF3hX6O.js';
2
- export { C as CompositeStandardLinkPlugin, a as StandardLink, h as StandardLinkInterceptor, e as StandardLinkInterceptorOptions, b as StandardLinkOptions, c as StandardLinkPlugin, S as StandardLinkTransport, i as StandardLinkTransportInterceptor, d as StandardLinkTransportInterceptorOptions } from '../../shared/client.CPF3hX6O.js';
3
- import { Value, Promisable } from '@orpc/shared';
4
- import { StandardUrl, StandardHeaders, StandardRequest, StandardLazyResponse } from '@standardserver/core';
5
- export { StandardBody, StandardBodyHint, StandardHeaders, StandardLazyRequest, StandardLazyResponse, StandardMethod, StandardRequest, StandardResponse, StandardUrl } from '@standardserver/core';
6
- import { C as ClientContext, a as ClientOptions } from '../../shared/client.8ug8I-zu.js';
7
- import { e as RPCSerializer } from '../../shared/client.BdItY5DT.js';
8
-
9
- interface RPCLinkCodecOptions<T extends ClientContext> {
10
- /**
11
- * Base url for all requests (without origin). Should match with handler's prefix.
12
- *
13
- * @example '/rpc?base=1'
14
- *
15
- * @default '/'
16
- */
17
- url?: Value<Promisable<StandardUrl>, [options: ClientOptions<T>, path: string[], input: unknown]>;
18
- /**
19
- * The maximum length of the URL.
20
- *
21
- * If the URL exceeds this length, the codec should use the `fallbackMethod` to send the request with the payload in the body instead of the URL.
22
- *
23
- * @default 2083
24
- */
25
- maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: string[], input: unknown]>;
26
- /**
27
- * The method used to make the request.
28
- *
29
- * @default 'POST'
30
- */
31
- method?: Value<Promisable<'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'>, [options: ClientOptions<T>, path: string[], input: unknown]>;
32
- /**
33
- * The method to use when the payload cannot safely pass to the server with method return from method function.
34
- * GET is not allowed, it's very dangerous.
35
- *
36
- * @default 'POST'
37
- */
38
- fallbackMethod?: 'POST' | 'PUT' | 'PATCH' | 'DELETE';
39
- /**
40
- * Inject headers to the request.
41
- */
42
- headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: string[], input: unknown]>;
43
- /**
44
- * Override the default RPC serializer.
45
- */
46
- serializer?: Pick<RPCSerializer, keyof RPCSerializer>;
47
- }
48
- declare class RPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
49
- private readonly baseUrl;
50
- private readonly maxUrlLength;
51
- private readonly fallbackMethod;
52
- private readonly expectedMethod;
53
- private readonly headers;
54
- private readonly serializer;
55
- constructor(options: RPCLinkCodecOptions<T>);
56
- encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promise<StandardRequest>;
57
- decodeResponse(response: StandardLazyResponse): Promise<StandardLinkCodecDecodedResponse>;
58
- }
59
-
60
- export { RPCLinkCodec, StandardLinkCodec, StandardLinkCodecDecodedResponse };
61
- export type { RPCLinkCodecOptions };
1
+ import { StandardLinkCodec, StandardLinkCodecDecodedResponse } from "./codec.js";
2
+ import { StandardLink, StandardLinkInterceptor, StandardLinkInterceptorOptions, StandardLinkOptions, StandardLinkTransportInterceptor, StandardLinkTransportInterceptorOptions } from "./link.js";
3
+ import { StandardLinkPlugin } from "./plugin.js";
4
+ import { RPCLinkCodec, RPCLinkCodecOptions } from "./rpc-link-codec.js";
5
+ import { StandardLinkTransport } from "./transport.js";
6
+ export { RPCLinkCodec, RPCLinkCodecOptions, StandardLink, StandardLinkCodec, StandardLinkCodecDecodedResponse, StandardLinkInterceptor, StandardLinkInterceptorOptions, StandardLinkOptions, type StandardLinkPlugin, StandardLinkTransport, StandardLinkTransportInterceptor, StandardLinkTransportInterceptorOptions };
@@ -0,0 +1,2 @@
1
+ export { StandardLink } from "./link.js";
2
+ export { RPCLinkCodec } from "./rpc-link-codec.js";
@@ -0,0 +1,40 @@
1
+ import { ClientContext, ClientLink, ClientOptions } from "../../types.js";
2
+ import { StandardLinkCodec } from "./codec.js";
3
+ import { StandardLinkPlugin } from "./plugin.js";
4
+ import { StandardLinkTransport } from "./transport.js";
5
+ import { Interceptor } from "@orpc/shared";
6
+ import { StandardLazyResponse, StandardRequest } from "@standard-server/core";
7
+ export interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
8
+ path: string[];
9
+ input: unknown;
10
+ }
11
+ export type StandardLinkInterceptor<T extends ClientContext> = Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>;
12
+ export interface StandardLinkTransportInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
13
+ path: string[];
14
+ request: StandardRequest;
15
+ }
16
+ export type StandardLinkTransportInterceptor<T extends ClientContext> = Interceptor<StandardLinkTransportInterceptorOptions<T>, Promise<StandardLazyResponse>>;
17
+ export interface StandardLinkOptions<T extends ClientContext> {
18
+ /**
19
+ * Interceptors that execute around the entire call, including transport and codec.
20
+ * Useful for error handling, logging, metrics, ...
21
+ */
22
+ interceptors?: StandardLinkInterceptor<T>[] | undefined;
23
+ /**
24
+ * Interceptors that execute around the transport layer, after encoding and before decoding.
25
+ * Useful for modifying the request or response, adding transport-level logging, ...
26
+ */
27
+ transportInterceptors?: StandardLinkTransportInterceptor<T>[] | undefined;
28
+ plugins?: StandardLinkPlugin<T>[] | undefined;
29
+ }
30
+ export declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
31
+ private readonly codec;
32
+ private readonly transport;
33
+ private readonly interceptors;
34
+ private readonly transportInterceptors;
35
+ constructor(codec: StandardLinkCodec<T>, transport: StandardLinkTransport<T>, options?: StandardLinkOptions<T>);
36
+ /**
37
+ * @throws ORPCError, MalformedResponseError, transport-level errors (network failures, timeouts, etc.)
38
+ */
39
+ call(path: string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
40
+ }
@@ -0,0 +1,101 @@
1
+ import { CompositeStandardLinkPlugin } from "./plugin.js";
2
+ import { ORPC_NAME, getTracer, intercept, isAsyncIteratorObject, override, runWithSpan, traceAsyncIterator, traceReadableStream } from "@orpc/shared";
3
+ // src/adapters/standard/link.ts
4
+ var StandardLink = class {
5
+ codec;
6
+ transport;
7
+ interceptors;
8
+ transportInterceptors;
9
+ constructor(codec, transport, options = {}) {
10
+ this.codec = codec;
11
+ this.transport = transport;
12
+ options = new CompositeStandardLinkPlugin(options.plugins).init(options);
13
+ this.interceptors = options.interceptors;
14
+ this.transportInterceptors = options.transportInterceptors;
15
+ }
16
+ /**
17
+ * @throws ORPCError, MalformedResponseError, transport-level errors (network failures, timeouts, etc.)
18
+ */
19
+ call(path, input, options) {
20
+ return runWithSpan(`${ORPC_NAME}.${path.join("/")}`, (span) => {
21
+ /**
22
+ * [Semantic conventions for RPC spans](https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/)
23
+ */
24
+ span?.setAttribute("rpc.system", ORPC_NAME);
25
+ span?.setAttribute("rpc.method", path.join("."));
26
+ if (getTracer() && isAsyncIteratorObject(input))
27
+ /**
28
+ * @warning
29
+ * Remember use `override` for AsyncIteratorObject to remain other special properties
30
+ */
31
+ input = override(input, traceAsyncIterator("consume_async_iterator_object_input", input));
32
+ else if (getTracer() && input instanceof ReadableStream)
33
+ /**
34
+ * @warning
35
+ * Remember use `override` for ReadableStream to remain other special properties
36
+ */
37
+ input = override(input, traceReadableStream("consume_octet_stream_input", input));
38
+ return intercept(this.interceptors, {
39
+ ...options,
40
+ path,
41
+ input
42
+ }, async ({ path, input, ...options }) => {
43
+ /**
44
+ * In browsers, the OpenTelemetry context manager may not work reliably with async functions,
45
+ * so we should manually pass the parent span here.
46
+ */
47
+ const tracer = getTracer();
48
+ const activeSpan = tracer?.getActiveSpan() ?? span;
49
+ let request = await runWithSpan({
50
+ name: "encode_input",
51
+ parent: activeSpan
52
+ }, () => this.codec.encodeInput(input, path, options));
53
+ if (activeSpan && tracer?.inject) {
54
+ const headers = { ...request.headers };
55
+ tracer.inject(activeSpan, headers);
56
+ request = {
57
+ ...request,
58
+ headers
59
+ };
60
+ }
61
+ const response = await intercept(this.transportInterceptors, {
62
+ ...options,
63
+ path,
64
+ request
65
+ }, ({ path, request, ...options }) => {
66
+ /**
67
+ * In browsers, the OpenTelemetry context manager may not work reliably with async functions,
68
+ * so we should manually pass the parent span here.
69
+ */
70
+ const activeTransportSpan = tracer?.getActiveSpan() ?? activeSpan;
71
+ return runWithSpan({
72
+ name: "send_request",
73
+ parent: activeTransportSpan
74
+ }, () => this.transport.send(request, path, options));
75
+ });
76
+ const decodedResult = await runWithSpan({
77
+ name: "decode_response",
78
+ parent: activeSpan
79
+ }, () => this.codec.decodeResponse(response, path, options));
80
+ if (decodedResult.kind === "error") throw decodedResult.error;
81
+ const output = decodedResult.output;
82
+ if (getTracer() && isAsyncIteratorObject(output))
83
+ /**
84
+ * Do not pass the active span as parent here, as it is a lazy span.
85
+ *
86
+ * @warning
87
+ * Remember use `override` for AsyncIteratorObject to remain other special properties
88
+ */
89
+ return override(output, traceAsyncIterator("consume_async_iterator_object_output", output));
90
+ else if (getTracer() && output instanceof ReadableStream)
91
+ /**
92
+ * @warning
93
+ * Remember use `override` for ReadableStream to remain other special properties
94
+ */
95
+ return override(output, traceReadableStream("consume_octet_stream_output", output));
96
+ return output;
97
+ });
98
+ });
99
+ }
100
+ };
101
+ export { StandardLink };
@@ -0,0 +1,26 @@
1
+ import { ClientContext } from "../../types.js";
2
+ import { StandardLinkOptions } from "./link.js";
3
+ import { OrderablePlugin } from "@orpc/shared";
4
+ export interface StandardLinkPlugin<T extends ClientContext> extends OrderablePlugin {
5
+ /**
6
+ * Initializes the plugin and returns new link options.
7
+ * Called once per plugin instance during composition.
8
+ *
9
+ * This method allows plugins to wrap, extend, or transform link options
10
+ * such as interceptors, or configuration.
11
+ *
12
+ * @param options - The current link options from previous plugins or base configuration
13
+ * @returns Transformed link options with plugin's modifications applied
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * init(options) {
18
+ * return {
19
+ * ...options,
20
+ * interceptors: [...(options.interceptors || []), myInterceptor]
21
+ * }
22
+ * }
23
+ * ```
24
+ */
25
+ init?(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
26
+ }
@@ -0,0 +1,14 @@
1
+ import { sortPlugins } from "@orpc/shared";
2
+ // src/adapters/standard/plugin.ts
3
+ var CompositeStandardLinkPlugin = class {
4
+ name = "~composite";
5
+ plugins;
6
+ constructor(plugins = []) {
7
+ this.plugins = sortPlugins(plugins);
8
+ }
9
+ init(options) {
10
+ for (const plugin of this.plugins) if (plugin.init) options = plugin.init(options);
11
+ return options;
12
+ }
13
+ };
14
+ export { CompositeStandardLinkPlugin };
@@ -0,0 +1,56 @@
1
+ import { RPCSerializer } from "../../rpc-serializer.js";
2
+ import { StandardLinkCodec, StandardLinkCodecDecodedResponse } from "./codec.js";
3
+ import { ClientContext, ClientOptions } from "../../types.js";
4
+ import "./index.js";
5
+ import { Promisable, Public, Value } from "@orpc/shared";
6
+ import { StandardHeaders, StandardLazyResponse, StandardRequest, StandardUrl } from "@standard-server/core";
7
+ export interface RPCLinkCodecOptions<T extends ClientContext> {
8
+ /**
9
+ * Base url for all requests (without origin). Should match with handler's prefix.
10
+ *
11
+ * @example '/rpc?base=1'
12
+ *
13
+ * @default '/'
14
+ */
15
+ url?: Value<Promisable<StandardUrl>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
16
+ /**
17
+ * The maximum length of the URL.
18
+ *
19
+ * If the URL exceeds this length, the codec should use the `fallbackMethod` to send the request with the payload in the body instead of the URL.
20
+ *
21
+ * @default 2083
22
+ */
23
+ maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
24
+ /**
25
+ * The method used to make the request.
26
+ *
27
+ * @default 'POST'
28
+ */
29
+ method?: Value<Promisable<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "QUERY">, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
30
+ /**
31
+ * The method to use when the payload cannot safely pass to the server with method return from method function.
32
+ * GET is not allowed, it's very dangerous.
33
+ *
34
+ * @default 'POST'
35
+ */
36
+ fallbackMethod?: "POST" | "PUT" | "PATCH" | "DELETE" | "QUERY" | undefined;
37
+ /**
38
+ * Inject headers to the request.
39
+ */
40
+ headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
41
+ /**
42
+ * Override the default RPC serializer.
43
+ */
44
+ serializer?: Public<RPCSerializer> | undefined;
45
+ }
46
+ export declare class RPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
47
+ private readonly baseUrl;
48
+ private readonly maxUrlLength;
49
+ private readonly fallbackMethod;
50
+ private readonly expectedMethod;
51
+ private readonly headers;
52
+ private readonly serializer;
53
+ constructor(options: RPCLinkCodecOptions<T>);
54
+ encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promise<StandardRequest>;
55
+ decodeResponse(response: StandardLazyResponse): Promise<StandardLinkCodecDecodedResponse>;
56
+ }