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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/README.md +4 -4
  2. package/dist/adapters/fetch/index.d.ts +2 -0
  3. package/dist/adapters/fetch/index.js +2 -0
  4. package/dist/adapters/fetch/rpc-link.d.ts +14 -0
  5. package/dist/adapters/fetch/rpc-link.js +14 -0
  6. package/dist/adapters/fetch/transport.d.ts +34 -0
  7. package/dist/adapters/fetch/transport.js +65 -0
  8. package/dist/adapters/message-port/index.d.ts +3 -0
  9. package/dist/adapters/message-port/index.js +3 -0
  10. package/dist/adapters/message-port/message-port.d.ts +29 -0
  11. package/dist/adapters/message-port/message-port.js +56 -0
  12. package/dist/adapters/message-port/rpc-link.d.ts +15 -0
  13. package/dist/adapters/message-port/rpc-link.js +15 -0
  14. package/dist/adapters/message-port/transport.d.ts +38 -0
  15. package/dist/adapters/message-port/transport.js +52 -0
  16. package/dist/adapters/standard/codec.d.ts +14 -0
  17. package/dist/adapters/standard/index.d.ts +5 -0
  18. package/dist/adapters/standard/index.js +4 -0
  19. package/dist/adapters/standard/link.d.ts +40 -0
  20. package/dist/adapters/standard/link.js +85 -0
  21. package/dist/adapters/standard/plugin.d.ts +32 -0
  22. package/dist/adapters/standard/plugin.js +16 -0
  23. package/dist/adapters/standard/rpc-link-codec.d.ts +63 -0
  24. package/dist/adapters/standard/rpc-link-codec.js +104 -0
  25. package/dist/adapters/standard/transport.d.ts +14 -0
  26. package/dist/adapters/websocket/index.d.ts +2 -0
  27. package/dist/adapters/websocket/index.js +2 -0
  28. package/dist/adapters/websocket/rpc-link.d.ts +14 -0
  29. package/dist/adapters/websocket/rpc-link.js +14 -0
  30. package/dist/adapters/websocket/transport.d.ts +115 -0
  31. package/dist/adapters/websocket/transport.js +133 -0
  32. package/dist/async-iterator-object.d.ts +2 -0
  33. package/dist/async-iterator-object.js +29 -0
  34. package/dist/client-safe.d.ts +18 -0
  35. package/dist/client-safe.js +36 -0
  36. package/dist/client.d.ts +40 -0
  37. package/dist/client.js +39 -0
  38. package/dist/consts.d.ts +10 -0
  39. package/dist/consts.js +43 -0
  40. package/dist/dynamic-link.d.ts +13 -0
  41. package/dist/dynamic-link.js +17 -0
  42. package/dist/error-utils.d.ts +21 -0
  43. package/dist/error-utils.js +58 -0
  44. package/dist/error.d.ts +132 -0
  45. package/dist/error.js +178 -0
  46. package/dist/index.d.ts +53 -0
  47. package/dist/index.js +40 -0
  48. package/dist/plugins/batch.d.ts +185 -0
  49. package/dist/plugins/batch.js +401 -0
  50. package/dist/plugins/cookie-jar.d.ts +82 -0
  51. package/dist/plugins/cookie-jar.js +268 -0
  52. package/dist/plugins/dedupe.d.ts +64 -0
  53. package/dist/plugins/dedupe.js +226 -0
  54. package/dist/plugins/index.d.ts +28 -0
  55. package/dist/plugins/index.js +23 -0
  56. package/dist/plugins/request-compression.d.ts +44 -0
  57. package/dist/plugins/request-compression.js +178 -0
  58. package/dist/plugins/response-compression.d.ts +28 -0
  59. package/dist/plugins/response-compression.js +91 -0
  60. package/dist/plugins/retry-after.d.ts +50 -0
  61. package/dist/plugins/retry-after.js +88 -0
  62. package/dist/plugins/retry.d.ts +84 -0
  63. package/dist/plugins/retry.js +140 -0
  64. package/dist/plugins/timeout.d.ts +25 -0
  65. package/dist/plugins/timeout.js +37 -0
  66. package/dist/rpc-json-serializer.d.ts +99 -0
  67. package/dist/rpc-json-serializer.js +392 -0
  68. package/dist/rpc-serializer.d.ts +32 -0
  69. package/dist/rpc-serializer.js +130 -0
  70. package/dist/types.d.ts +97 -0
  71. package/dist/utils.d.ts +47 -0
  72. package/dist/utils.js +51 -0
  73. package/package.json +43 -45
  74. package/dist/adapters/fetch/index.d.mts +0 -2
  75. package/dist/adapters/fetch/index.mjs +0 -2
  76. package/dist/adapters/fetch/rpc-link.d.mts +0 -13
  77. package/dist/adapters/fetch/rpc-link.mjs +0 -10
  78. package/dist/adapters/fetch/transport.d.mts +0 -31
  79. package/dist/adapters/fetch/transport.mjs +0 -65
  80. package/dist/adapters/message-port/index.d.mts +0 -3
  81. package/dist/adapters/message-port/index.mjs +0 -3
  82. package/dist/adapters/message-port/message-port.d.mts +0 -29
  83. package/dist/adapters/message-port/message-port.mjs +0 -43
  84. package/dist/adapters/message-port/rpc-link.d.mts +0 -14
  85. package/dist/adapters/message-port/rpc-link.mjs +0 -10
  86. package/dist/adapters/message-port/transport.d.mts +0 -35
  87. package/dist/adapters/message-port/transport.mjs +0 -47
  88. package/dist/adapters/standard/codec.d.mts +0 -14
  89. package/dist/adapters/standard/index.d.mts +0 -5
  90. package/dist/adapters/standard/index.mjs +0 -4
  91. package/dist/adapters/standard/link.d.mts +0 -40
  92. package/dist/adapters/standard/link.mjs +0 -79
  93. package/dist/adapters/standard/plugin.d.mts +0 -32
  94. package/dist/adapters/standard/plugin.mjs +0 -16
  95. package/dist/adapters/standard/rpc-link-codec.d.mts +0 -55
  96. package/dist/adapters/standard/rpc-link-codec.mjs +0 -104
  97. package/dist/adapters/standard/transport.d.mts +0 -14
  98. package/dist/adapters/websocket/index.d.mts +0 -2
  99. package/dist/adapters/websocket/index.mjs +0 -2
  100. package/dist/adapters/websocket/rpc-link.d.mts +0 -13
  101. package/dist/adapters/websocket/rpc-link.mjs +0 -10
  102. package/dist/adapters/websocket/transport.d.mts +0 -119
  103. package/dist/adapters/websocket/transport.mjs +0 -123
  104. package/dist/async-iterator-object.d.mts +0 -7
  105. package/dist/async-iterator-object.mjs +0 -30
  106. package/dist/client-safe.d.mts +0 -16
  107. package/dist/client-safe.mjs +0 -23
  108. package/dist/client.d.mts +0 -51
  109. package/dist/client.mjs +0 -32
  110. package/dist/consts.d.mts +0 -10
  111. package/dist/consts.mjs +0 -10
  112. package/dist/dynamic-link.d.mts +0 -13
  113. package/dist/dynamic-link.mjs +0 -12
  114. package/dist/error-utils.d.mts +0 -24
  115. package/dist/error-utils.mjs +0 -44
  116. package/dist/error.d.mts +0 -138
  117. package/dist/error.mjs +0 -105
  118. package/dist/index.d.mts +0 -49
  119. package/dist/index.mjs +0 -23
  120. package/dist/plugins/batch.d.mts +0 -150
  121. package/dist/plugins/batch.mjs +0 -339
  122. package/dist/plugins/cookie-jar.d.mts +0 -82
  123. package/dist/plugins/cookie-jar.mjs +0 -229
  124. package/dist/plugins/dedupe.d.mts +0 -58
  125. package/dist/plugins/dedupe.mjs +0 -199
  126. package/dist/plugins/index.d.mts +0 -28
  127. package/dist/plugins/index.mjs +0 -17
  128. package/dist/plugins/request-compression.d.mts +0 -44
  129. package/dist/plugins/request-compression.mjs +0 -150
  130. package/dist/plugins/response-compression.d.mts +0 -28
  131. package/dist/plugins/response-compression.mjs +0 -78
  132. package/dist/plugins/retry-after.d.mts +0 -41
  133. package/dist/plugins/retry-after.mjs +0 -71
  134. package/dist/plugins/retry.d.mts +0 -82
  135. package/dist/plugins/retry.mjs +0 -127
  136. package/dist/plugins/timeout.d.mts +0 -25
  137. package/dist/plugins/timeout.mjs +0 -36
  138. package/dist/rpc-json-serializer.d.mts +0 -99
  139. package/dist/rpc-json-serializer.mjs +0 -365
  140. package/dist/rpc-serializer.d.mts +0 -32
  141. package/dist/rpc-serializer.mjs +0 -100
  142. package/dist/types.d.mts +0 -93
  143. package/dist/utils.d.mts +0 -46
  144. package/dist/utils.mjs +0 -53
  145. /package/dist/adapters/standard/{codec.mjs → codec.js} +0 -0
  146. /package/dist/adapters/standard/{transport.mjs → transport.js} +0 -0
  147. /package/dist/{types.mjs → types.js} +0 -0
@@ -1,14 +0,0 @@
1
- import type { ClientContext } from "../../types.mjs";
2
- import type { RPCLinkCodecOptions, StandardLinkOptions } from "../standard/index.mjs";
3
- import type { MessagePortLinkTransportOptions } from "./transport.mjs";
4
- import { StandardLink } from "../standard/index.mjs";
5
- export interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, MessagePortLinkTransportOptions<T>, RPCLinkCodecOptions<T> {}
6
- /**
7
- * Client link that communicates with an RPC Handler over a Message Port
8
- * (e.g. workers, iframes, browser extensions, Electron).
9
- *
10
- * @see {@link https://orpc.dev/docs/adapters/message-port | Message Port Adapter}
11
- */
12
- export declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
13
- constructor(options: RPCLinkOptions<T>);
14
- }
@@ -1,10 +0,0 @@
1
- import { RPCLinkCodec, StandardLink } from "../standard/index.mjs";
2
- import { MessagePortLinkTransport } from "./transport.mjs";
3
-
4
- export class RPCLink extends StandardLink {
5
- constructor(options) {
6
- const codec = new RPCLinkCodec(options);
7
- const transport = new MessagePortLinkTransport(options);
8
- super(codec, transport, options);
9
- }
10
- }
@@ -1,35 +0,0 @@
1
- import type { Promisable, Value } from "@orpc/shared";
2
- import type { StandardLazyResponse, StandardRequest } from "@standard-server/core";
3
- import type { DecodePeerMessageOptions, EncodePeerMessageOptions } from "@standard-server/peer";
4
- import type { ClientContext, ClientOptions } from "../../types.mjs";
5
- import type { StandardLinkTransport } from "../standard/index.mjs";
6
- import type { SupportedMessagePort } from "./message-port.mjs";
7
- import { ClientPeer } 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 {};
@@ -1,47 +0,0 @@
1
- import { AbortError, value } from "@orpc/shared";
2
- import { ClientPeer, decodePeerMessage, encodePeerMessage, isPeerMessage, isServerPeerSendMessage } from "@standard-server/peer";
3
- import { onMessagePortClose, onMessagePortMessage, postMessagePortMessage } from "./message-port.mjs";
4
- export class MessagePortLinkTransport {
5
- peer;
6
- constructor({ port, experimental_transfer, encodePeerMessage: encodePeerMessageOptions, decodePeerMessage: decodePeerMessageOptions }) {
7
- let closeReason;
8
- this.peer = new ClientPeer(async (message) => {
9
-
10
- if (closeReason) {
11
- throw closeReason;
12
- }
13
- const transfer = await value(experimental_transfer, message, port);
14
- if (transfer) {
15
- postMessagePortMessage(port, message, transfer);
16
- } else {
17
- postMessagePortMessage(port, await encodePeerMessage(message, encodePeerMessageOptions));
18
- }
19
- });
20
- onMessagePortMessage(port, async (data) => {
21
- let peerMessage;
22
- if (typeof data === "string" || data instanceof Uint8Array) {
23
-
24
- const result = decodePeerMessage(data, decodePeerMessageOptions);
25
- if (result.matched && isServerPeerSendMessage(result.message)) {
26
- peerMessage = result.message;
27
- }
28
- } else if (isPeerMessage(data) && isServerPeerSendMessage(data)) {
29
- peerMessage = data;
30
- }
31
- if (peerMessage === undefined) {
32
- return { matched: false };
33
- }
34
-
35
- await this.peer.message(peerMessage);
36
- return { matched: true };
37
- });
38
- onMessagePortClose(port, async () => {
39
- closeReason = new AbortError("MessagePort closed");
40
- await this.peer.close(closeReason);
41
- });
42
- }
43
- async send(standardRequest, _path, _options) {
44
- const standardResponse = await this.peer.request(standardRequest);
45
- return standardResponse;
46
- }
47
- }
@@ -1,14 +0,0 @@
1
- import type { Promisable } from "@orpc/shared";
2
- import type { StandardLazyResponse, StandardRequest } from "@standard-server/core";
3
- import type { ClientContext, ClientOptions } from "../../types.mjs";
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,5 +0,0 @@
1
- export * from "./codec.mjs";
2
- export * from "./link.mjs";
3
- export type { StandardLinkPlugin } from "./plugin.mjs";
4
- export * from "./rpc-link-codec.mjs";
5
- export * from "./transport.mjs";
@@ -1,4 +0,0 @@
1
- export * from "./codec.mjs";
2
- export * from "./link.mjs";
3
- export * from "./rpc-link-codec.mjs";
4
- export * from "./transport.mjs";
@@ -1,40 +0,0 @@
1
- import type { Interceptor } from "@orpc/shared";
2
- import type { StandardLazyResponse, StandardRequest } from "@standard-server/core";
3
- import type { ClientContext, ClientLink, ClientOptions } from "../../types.mjs";
4
- import type { StandardLinkCodec } from "./codec.mjs";
5
- import type { StandardLinkPlugin } from "./plugin.mjs";
6
- import type { StandardLinkTransport } from "./transport.mjs";
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
- }
@@ -1,79 +0,0 @@
1
- import { getTracer, intercept, isAsyncIteratorObject, ORPC_NAME, override, runWithSpan, traceAsyncIterator, traceReadableStream } from "@orpc/shared";
2
- import { CompositeStandardLinkPlugin } from "./plugin.mjs";
3
- export class StandardLink {
4
- codec;
5
- transport;
6
- interceptors;
7
- transportInterceptors;
8
- constructor(codec, transport, options = {}) {
9
- this.codec = codec;
10
- this.transport = transport;
11
- options = new CompositeStandardLinkPlugin(options.plugins).init(options);
12
- this.interceptors = options.interceptors;
13
- this.transportInterceptors = options.transportInterceptors;
14
- }
15
-
16
- call(path, input, options) {
17
- return runWithSpan(`${ORPC_NAME}.${path.join("/")}`, (span) => {
18
-
19
- span?.setAttribute("rpc.system", ORPC_NAME);
20
- span?.setAttribute("rpc.method", path.join("."));
21
- if (getTracer() && isAsyncIteratorObject(input)) {
22
-
23
- input = override(input, traceAsyncIterator("consume_async_iterator_object_input", input));
24
- } else if (getTracer() && input instanceof ReadableStream) {
25
-
26
- input = override(input, traceReadableStream("consume_octet_stream_input", input));
27
- }
28
- return intercept(this.interceptors, {
29
- ...options,
30
- path,
31
- input
32
- }, async ({ path, input, ...options }) => {
33
-
34
- const tracer = getTracer();
35
- const activeSpan = tracer?.getActiveSpan() ?? span;
36
- let request = await runWithSpan({
37
- name: "encode_input",
38
- parent: activeSpan
39
- }, () => this.codec.encodeInput(input, path, options));
40
- if (activeSpan && tracer?.inject) {
41
- const headers = { ...request.headers };
42
- tracer.inject(activeSpan, headers);
43
- request = {
44
- ...request,
45
- headers
46
- };
47
- }
48
- const response = await intercept(this.transportInterceptors, {
49
- ...options,
50
- path,
51
- request
52
- }, ({ path, request, ...options }) => {
53
-
54
- const activeTransportSpan = tracer?.getActiveSpan() ?? activeSpan;
55
- return runWithSpan({
56
- name: "send_request",
57
- parent: activeTransportSpan
58
- }, () => this.transport.send(request, path, options));
59
- });
60
- const decodedResult = await runWithSpan({
61
- name: "decode_response",
62
- parent: activeSpan
63
- }, () => this.codec.decodeResponse(response, path, options));
64
- if (decodedResult.kind === "error") {
65
- throw decodedResult.error;
66
- }
67
- const output = decodedResult.output;
68
- if (getTracer() && isAsyncIteratorObject(output)) {
69
-
70
- return override(output, traceAsyncIterator("consume_async_iterator_object_output", output));
71
- } else if (getTracer() && output instanceof ReadableStream) {
72
-
73
- return override(output, traceReadableStream("consume_octet_stream_output", output));
74
- }
75
- return output;
76
- });
77
- });
78
- }
79
- }
@@ -1,32 +0,0 @@
1
- import type { OrderablePlugin } from "@orpc/shared";
2
- import type { ClientContext } from "../../types.mjs";
3
- import type { StandardLinkOptions } from "./link.mjs";
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
- }
27
- export declare class CompositeStandardLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
28
- name: string;
29
- protected readonly plugins: StandardLinkPlugin<T>[];
30
- constructor(plugins?: StandardLinkPlugin<T>[]);
31
- init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
32
- }
@@ -1,16 +0,0 @@
1
- import { sortPlugins } from "@orpc/shared";
2
- export class CompositeStandardLinkPlugin {
3
- name = "~composite";
4
- plugins;
5
- constructor(plugins = []) {
6
- this.plugins = sortPlugins(plugins);
7
- }
8
- init(options) {
9
- for (const plugin of this.plugins) {
10
- if (plugin.init) {
11
- options = plugin.init(options);
12
- }
13
- }
14
- return options;
15
- }
16
- }
@@ -1,55 +0,0 @@
1
- import type { Promisable, Public, Value } from "@orpc/shared";
2
- import type { StandardHeaders, StandardLazyResponse, StandardRequest, StandardUrl } from "@standard-server/core";
3
- import type { ClientContext, ClientOptions } from "../../types.mjs";
4
- import type { StandardLinkCodec, StandardLinkCodecDecodedResponse } from "./index.mjs";
5
- import { RPCSerializer } from "../../rpc-serializer.mjs";
6
- export interface RPCLinkCodecOptions<T extends ClientContext> {
7
- /**
8
- * Base url for all requests (without origin). Should match with handler's prefix.
9
- *
10
- * @example '/rpc?base=1'
11
- *
12
- * @default '/'
13
- */
14
- url?: Value<Promisable<StandardUrl>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
15
- /**
16
- * The maximum length of the URL.
17
- *
18
- * 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.
19
- *
20
- * @default 2083
21
- */
22
- maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
23
- /**
24
- * The method used to make the request.
25
- *
26
- * @default 'POST'
27
- */
28
- method?: Value<Promisable<"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "QUERY">, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
29
- /**
30
- * The method to use when the payload cannot safely pass to the server with method return from method function.
31
- * GET is not allowed, it's very dangerous.
32
- *
33
- * @default 'POST'
34
- */
35
- fallbackMethod?: "POST" | "PUT" | "PATCH" | "DELETE" | "QUERY" | undefined;
36
- /**
37
- * Inject headers to the request.
38
- */
39
- headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
40
- /**
41
- * Override the default RPC serializer.
42
- */
43
- serializer?: Public<RPCSerializer> | undefined;
44
- }
45
- export declare class RPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
46
- private readonly baseUrl;
47
- private readonly maxUrlLength;
48
- private readonly fallbackMethod;
49
- private readonly expectedMethod;
50
- private readonly headers;
51
- private readonly serializer;
52
- constructor(options: RPCLinkCodecOptions<T>);
53
- encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promise<StandardRequest>;
54
- decodeResponse(response: StandardLazyResponse): Promise<StandardLinkCodecDecodedResponse>;
55
- }
@@ -1,104 +0,0 @@
1
- import { isAsyncIteratorObject, pathToHttpPath, stringifyJSON, value } from "@orpc/shared";
2
- import { mergeStandardHeaders, parseStandardUrl } from "@standard-server/core";
3
- import { toStandardHeaders } from "@standard-server/fetch";
4
- import { MalformedResponseError, ResponseError } from "../../error.mjs";
5
- import { createORPCErrorFromJson, isORPCErrorJson } from "../../error-utils.mjs";
6
- import { RPCSerializer } from "../../rpc-serializer.mjs";
7
- const END_SLASH_REGEX = /\/$/;
8
- export class RPCLinkCodec {
9
- baseUrl;
10
- maxUrlLength;
11
- fallbackMethod;
12
- expectedMethod;
13
- headers;
14
- serializer;
15
- constructor(options) {
16
- this.baseUrl = options.url ?? "/";
17
- this.maxUrlLength = options.maxUrlLength ?? 2083;
18
- this.fallbackMethod = options.fallbackMethod ?? "POST";
19
- this.expectedMethod = options.method ?? this.fallbackMethod;
20
- this.headers = options.headers ?? {};
21
- this.serializer = options.serializer ?? new RPCSerializer();
22
- }
23
- async encodeInput(input, path, options) {
24
- let headers = toResolvedStandardHeaders(await value(this.headers, options, path, input));
25
- if (options.lastEventId !== undefined) {
26
- headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
27
- }
28
- const expectedMethod = await value(this.expectedMethod, options, path, input);
29
- const baseUrl = await value(this.baseUrl, options, path, input);
30
- const [pathname, search, hash] = parseStandardUrl(baseUrl);
31
- const newPathname = `${pathname.replace(END_SLASH_REGEX, "")}${pathToHttpPath(path)}`;
32
- const serialized = this.serializer.serialize(input);
33
- if (expectedMethod === "GET" && !(serialized instanceof Blob) && !(serialized instanceof ReadableStream) && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
34
- const maxUrlLength = await value(this.maxUrlLength, options, path, input);
35
- const mergedSearch = new URLSearchParams(search);
36
- mergedSearch.append("data", stringifyJSON(serialized) ?? "");
37
- const url = `${newPathname}?${mergedSearch}${hash ?? ""}`;
38
- if (url.length <= maxUrlLength) {
39
- return {
40
- body: undefined,
41
- method: expectedMethod,
42
- headers,
43
- url,
44
- signal: options.signal
45
- };
46
- }
47
- }
48
- const url = `${newPathname}${search ?? ""}${hash ?? ""}`;
49
- return {
50
- url,
51
- method: expectedMethod === "GET" ? this.fallbackMethod : expectedMethod,
52
- headers,
53
- body: serialized,
54
- signal: options.signal
55
- };
56
- }
57
- async decodeResponse(response) {
58
- const isOk = response.status < 400;
59
- const body = await response.resolveBody();
60
- const deserialized = await (async () => {
61
- try {
62
- return this.serializer.deserialize(body);
63
- } catch (cause) {
64
- throw new MalformedResponseError({
65
- message: "Invalid RPC response format.",
66
- response: {
67
- status: response.status,
68
- headers: response.headers,
69
- body
70
- },
71
- cause
72
- });
73
- }
74
- })();
75
- if (!isOk) {
76
- if (isORPCErrorJson(deserialized)) {
77
- return {
78
- kind: "error",
79
- error: createORPCErrorFromJson(deserialized, { cause: new ResponseError({ response: {
80
- status: response.status,
81
- headers: response.headers,
82
- body
83
- } }) })
84
- };
85
- }
86
- throw new MalformedResponseError({ response: {
87
- headers: response.headers,
88
- status: response.status,
89
- body
90
- } });
91
- }
92
- return {
93
- kind: "output",
94
- output: deserialized
95
- };
96
- }
97
- }
98
- function toResolvedStandardHeaders(headers) {
99
-
100
- if (typeof headers.forEach === "function") {
101
- return toStandardHeaders(headers);
102
- }
103
- return headers;
104
- }
@@ -1,14 +0,0 @@
1
- import type { StandardLazyResponse, StandardRequest } from "@standard-server/core";
2
- import type { ClientContext, ClientOptions } from "../../types.mjs";
3
- /**
4
- * Handles the transport layer for sending requests and receiving responses.
5
- *
6
- * Implementations are responsible for the actual network communication,
7
- * such as HTTP fetch, WebSocket, or other transport mechanisms.
8
- */
9
- export interface StandardLinkTransport<T extends ClientContext> {
10
- /**
11
- * @throws Transport-level errors (network failures, timeouts, etc.)
12
- */
13
- send(request: StandardRequest, path: string[], options: ClientOptions<T>): Promise<StandardLazyResponse>;
14
- }
@@ -1,2 +0,0 @@
1
- export * from "./rpc-link.mjs";
2
- export * from "./transport.mjs";
@@ -1,2 +0,0 @@
1
- export * from "./rpc-link.mjs";
2
- export * from "./transport.mjs";
@@ -1,13 +0,0 @@
1
- import type { ClientContext } from "../../types.mjs";
2
- import type { RPCLinkCodecOptions, StandardLinkOptions } from "../standard/index.mjs";
3
- import type { WebSocketLinkTransportOptions } from "./transport.mjs";
4
- import { StandardLink } from "../standard/index.mjs";
5
- export interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, WebSocketLinkTransportOptions<T>, RPCLinkCodecOptions<T> {}
6
- /**
7
- * Client link that communicates with an RPC Handler over a WebSocket connection.
8
- *
9
- * @see {@link https://orpc.dev/docs/adapters/websocket | WebSocket Adapters}
10
- */
11
- export declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
12
- constructor(options: RPCLinkOptions<T>);
13
- }
@@ -1,10 +0,0 @@
1
- import { RPCLinkCodec, StandardLink } from "../standard/index.mjs";
2
- import { WebSocketLinkTransport } from "./transport.mjs";
3
-
4
- export class RPCLink extends StandardLink {
5
- constructor(options) {
6
- const codec = new RPCLinkCodec(options);
7
- const transport = new WebSocketLinkTransport(options);
8
- super(codec, transport, options);
9
- }
10
- }
@@ -1,119 +0,0 @@
1
- import type { Promisable } from "@orpc/shared";
2
- import type { StandardLazyResponse, StandardRequest } from "@standard-server/core";
3
- import type { DecodePeerMessageOptions, EncodePeerMessageOptions } from "@standard-server/peer";
4
- import type { ClientContext, ClientOptions } from "../../types.mjs";
5
- import type { StandardLinkTransport } from "../standard/index.mjs";
6
- export type WebSocketLike = Pick<WebSocket, "addEventListener" | "removeEventListener" | "send" | "readyState">;
7
- export interface WebSocketLinkTransportAttemptInfo {
8
- /**
9
- * Total number of connection attempts for this transport's lifetime.
10
- * Starts at 1 on the first attempt, increments on every subsequent
11
- * attempt, and never resets.
12
- */
13
- totalAttempt: number;
14
- /**
15
- * Attempt number within the current (re)connect cycle.
16
- * Starts at 1, increments on each consecutive failure, and resets to 1
17
- * once a connection succeeds or the cycle gives up. Use this for backoff calculations.
18
- */
19
- attempt: number;
20
- }
21
- export interface WebSocketLinkTransportReconnectOptions {
22
- /**
23
- * Whether to automatically reconnect when the connection is lost.
24
- *
25
- * @default false
26
- */
27
- enabled: boolean;
28
- /**
29
- * Delay before a (re)connect attempt, in milliseconds.
30
- *
31
- * @default info => info.attempt === 1 ? 0 : 2_000
32
- */
33
- delay?: ((info: WebSocketLinkTransportAttemptInfo) => number) | undefined;
34
- /**
35
- * Maximum number of consecutive failed attempts before giving up.
36
- * When reached, calls waiting for the connection fail, and the next
37
- * call starts a new reconnect cycle.
38
- * Should greater than 1
39
- *
40
- * @default Infinity
41
- */
42
- maxAttempt?: number | undefined;
43
- /**
44
- * Maximum number of connection attempts over the transport's lifetime,
45
- * successful ones included (see `totalAttempt`).
46
- * When reached, calls that need a new connection fail instead of connecting.
47
- *
48
- * @default Infinity
49
- */
50
- maxTotalAttempt?: number | undefined;
51
- /**
52
- * Whether to proactively reconnect right after the socket closes,
53
- * rather than waiting for the next call to trigger reconnection.
54
- * Reduces latency for the next request.
55
- *
56
- * @default { enabled: false }
57
- */
58
- onClose?: {
59
- /**
60
- * Whether to proactively reconnect right after the socket closes,
61
- * rather than waiting for the next call to trigger reconnection.
62
- * Reduces latency for the next request.
63
- *
64
- * @default false
65
- */
66
- enabled: boolean;
67
- /**
68
- * Delay before reconnecting after the socket closes, in milliseconds.
69
- *
70
- * @default 0
71
- */
72
- delay?: number | undefined;
73
- } | undefined;
74
- }
75
- export interface WebSocketLinkTransportOptions<_T extends ClientContext> {
76
- /**
77
- * Returns a WebSocket instance for peer communication.
78
- * Can be async for lazy resolution.
79
- */
80
- connect: (info: WebSocketLinkTransportAttemptInfo) => Promisable<WebSocketLike>;
81
- /**
82
- * Whether to connect immediately on initialization, instead of waiting
83
- * for the first call. Reduces latency for the first request.
84
- *
85
- * @default false
86
- */
87
- connectOnInit?: boolean | undefined;
88
- /**
89
- * Reconnection behavior when the connection is lost.
90
- *
91
- * @default { enabled: false }
92
- */
93
- reconnect?: WebSocketLinkTransportReconnectOptions | undefined;
94
- /**
95
- * Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
96
- */
97
- encodePeerMessage?: EncodePeerMessageOptions | undefined;
98
- /**
99
- * Options for decoding peer messages. such as `prefix` for distinguishing messages on the same channel..
100
- */
101
- decodePeerMessage?: DecodePeerMessageOptions | undefined;
102
- }
103
- export declare class WebSocketLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
104
- private readonly connect;
105
- private readonly reconnectEnabled;
106
- private readonly reconnectDelay;
107
- private readonly reconnectMaxAttempt;
108
- private readonly reconnectMaxTotalAttempt;
109
- private readonly reconnectOnCloseEnabled;
110
- private readonly reconnectOnCloseDelay;
111
- private readonly encodePeerMessageOptions;
112
- private readonly decodePeerMessageOptions;
113
- constructor(options: WebSocketLinkTransportOptions<T>);
114
- send(standardRequest: StandardRequest, _path: string[], _options: ClientOptions<T>): Promise<StandardLazyResponse>;
115
- private totalAttempt;
116
- private attempt;
117
- private current;
118
- private getConnectedPeer;
119
- }