@orpc/client 0.0.0-next.a4ecb29 → 0.0.0-next.a55d49f

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 (34) hide show
  1. package/README.md +31 -22
  2. package/dist/adapters/fetch/index.d.mts +31 -15
  3. package/dist/adapters/fetch/index.d.ts +31 -15
  4. package/dist/adapters/fetch/index.mjs +26 -18
  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 +86 -0
  8. package/dist/adapters/standard/index.d.mts +9 -103
  9. package/dist/adapters/standard/index.d.ts +9 -103
  10. package/dist/adapters/standard/index.mjs +3 -3
  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 +46 -0
  14. package/dist/index.d.mts +107 -27
  15. package/dist/index.d.ts +107 -27
  16. package/dist/index.mjs +84 -36
  17. package/dist/plugins/index.d.mts +166 -25
  18. package/dist/plugins/index.d.ts +166 -25
  19. package/dist/plugins/index.mjs +341 -61
  20. package/dist/shared/client.BH1AYT_p.d.mts +83 -0
  21. package/dist/shared/client.BH1AYT_p.d.ts +83 -0
  22. package/dist/shared/client.BW3SBw-r.mjs +208 -0
  23. package/dist/shared/client.BxV-mzeR.d.ts +91 -0
  24. package/dist/shared/client.CPgZaUox.d.mts +45 -0
  25. package/dist/shared/client.D8lMmWVC.d.mts +91 -0
  26. package/dist/shared/client.De8SW4Kw.d.ts +45 -0
  27. package/dist/shared/{client.Be-O_tdu.mjs → client.ufURaY18.mjs} +106 -40
  28. package/package.json +16 -5
  29. package/dist/shared/client.BacCdg3F.mjs +0 -172
  30. package/dist/shared/client.CupM8eRP.d.mts +0 -30
  31. package/dist/shared/client.CupM8eRP.d.ts +0 -30
  32. package/dist/shared/client.CvnV7_uV.mjs +0 -12
  33. package/dist/shared/client.DrOAzyMB.d.mts +0 -45
  34. package/dist/shared/client.aGal-uGY.d.ts +0 -45
package/README.md CHANGED
@@ -17,32 +17,31 @@
17
17
  <a href="https://discord.gg/TXEbwRBvQn">
18
18
  <img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
19
19
  </a>
20
+ <a href="https://deepwiki.com/unnoq/orpc">
21
+ <img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki">
22
+ </a>
20
23
  </div>
21
24
 
22
25
  <h3 align="center">Typesafe APIs Made Simple 🪄</h3>
23
26
 
24
- **oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards, ensuring a smooth and enjoyable developer experience.
27
+ **oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
25
28
 
26
29
  ---
27
30
 
28
31
  ## Highlights
29
32
 
30
- - **End-to-End Type Safety 🔒**: Ensure complete type safety from inputs to outputs and errors, bridging server and client seamlessly.
31
- - **First-Class OpenAPI 📄**: Adheres to the OpenAPI standard out of the box, ensuring seamless integration and comprehensive API documentation.
32
- - **Contract-First Development 📜**: (Optional) Define your API contract upfront and implement it with confidence.
33
- - **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
34
- - **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
35
- - **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
36
- - **Server Actions ⚡️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
37
- - **Standard Schema Support 🗂️**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
38
- - **Fast & Lightweight 💨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
39
- - **Native Types 📦**: Enjoy built-in support for Date, File, Blob, BigInt, URL and more with no extra setup.
40
- - **Lazy Router ⏱️**: Improve cold start times with our lazy routing feature.
41
- - **SSE & Streaming 📡**: Provides SSE and streaming features – perfect for real-time notifications and AI-powered streaming responses.
42
- - **Reusability 🔄**: Write once and reuse your code across multiple purposes effortlessly.
43
- - **Extendability 🔌**: Easily enhance oRPC with plugins, middleware, and interceptors.
44
- - **Reliability 🛡️**: Well-tested, fully TypeScript, production-ready, and MIT licensed for peace of mind.
45
- - **Simplicity 💡**: Enjoy straightforward, clean code with no hidden magic.
33
+ - **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
34
+ - **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
35
+ - **📝 Contract-First Development**: Optionally define your API contract before implementation.
36
+ - **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
37
+ - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
38
+ - **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
39
+ - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
40
+ - **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
41
+ - **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
42
+ - **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
43
+ - **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
44
+ - **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
46
45
 
47
46
  ## Documentation
48
47
 
@@ -53,12 +52,14 @@ You can find the full documentation [here](https://orpc.unnoq.com).
53
52
  - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
54
53
  - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
55
54
  - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
56
- - [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
57
- - [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
58
- - [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
59
- - [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
60
- - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
61
55
  - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
56
+ - [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
57
+ - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
58
+ - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
59
+ - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
60
+ - [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
61
+ - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
62
+ - [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
62
63
  - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
63
64
  - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
64
65
  - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
@@ -87,6 +88,14 @@ const client: RouterClient<typeof router> = createORPCClient(link)
87
88
  const client: ContractRouterClient<typeof contract> = createORPCClient(link)
88
89
  ```
89
90
 
91
+ ## Sponsors
92
+
93
+ <p align="center">
94
+ <a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
95
+ <img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
96
+ </a>
97
+ </p>
98
+
90
99
  ## License
91
100
 
92
101
  Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
@@ -1,30 +1,46 @@
1
+ import { Interceptor } from '@orpc/shared';
1
2
  import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
2
3
  import { ToFetchRequestOptions } from '@orpc/standard-server-fetch';
3
- import { a as ClientContext, C as ClientOptionsOut, b as ClientLink } from '../../shared/client.CupM8eRP.mjs';
4
- import { b as StandardLinkClient } from '../../shared/client.DrOAzyMB.mjs';
5
- import { StandardRPCLinkOptions } from '../standard/index.mjs';
6
- import '@orpc/shared';
4
+ import { b as ClientContext, c as ClientOptions } from '../../shared/client.BH1AYT_p.mjs';
5
+ import { a as StandardLinkPlugin, f as StandardLinkClient } from '../../shared/client.CPgZaUox.mjs';
6
+ import { f as StandardRPCLinkOptions, g as StandardRPCLink } from '../../shared/client.D8lMmWVC.mjs';
7
7
 
8
+ interface LinkFetchPlugin<T extends ClientContext> extends StandardLinkPlugin<T> {
9
+ initRuntimeAdapter?(options: LinkFetchClientOptions<T>): void;
10
+ }
11
+
12
+ interface LinkFetchInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
13
+ request: Request;
14
+ init: {
15
+ redirect?: Request['redirect'];
16
+ };
17
+ path: readonly string[];
18
+ input: unknown;
19
+ }
8
20
  interface LinkFetchClientOptions<T extends ClientContext> extends ToFetchRequestOptions {
9
- fetch?: (request: Request, init: Record<never, never>, options: ClientOptionsOut<T>, path: readonly string[], input: unknown) => Promise<Response>;
21
+ fetch?: (request: Request, init: LinkFetchInterceptorOptions<T>['init'], options: ClientOptions<T>, path: readonly string[], input: unknown) => Promise<Response>;
22
+ adapterInterceptors?: Interceptor<LinkFetchInterceptorOptions<T>, Promise<Response>>[];
23
+ plugins?: LinkFetchPlugin<T>[];
10
24
  }
11
25
  declare class LinkFetchClient<T extends ClientContext> implements StandardLinkClient<T> {
12
26
  private readonly fetch;
13
27
  private readonly toFetchRequestOptions;
28
+ private readonly adapterInterceptors;
14
29
  constructor(options: LinkFetchClientOptions<T>);
15
- call(request: StandardRequest, options: ClientOptionsOut<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
30
+ call(standardRequest: StandardRequest, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
16
31
  }
17
32
 
18
- interface RPCLinkOptions<T extends ClientContext> extends StandardRPCLinkOptions<T>, LinkFetchClientOptions<T> {
33
+ interface RPCLinkOptions<T extends ClientContext> extends LinkFetchClientOptions<T>, Omit<StandardRPCLinkOptions<T>, 'plugins'> {
19
34
  }
20
- declare class RPCLink<T extends ClientContext> implements ClientLink<T> {
21
- private readonly standardLink;
35
+ /**
36
+ * The RPC Link communicates with the server using the RPC protocol.
37
+ *
38
+ * @see {@link https://orpc.unnoq.com/docs/client/rpc-link RPC Link Docs}
39
+ * @see {@link https://orpc.unnoq.com/docs/advanced/rpc-protocol RPC Protocol Docs}
40
+ */
41
+ declare class RPCLink<T extends ClientContext> extends StandardRPCLink<T> {
22
42
  constructor(options: RPCLinkOptions<T>);
23
- call(path: readonly string[], input: unknown, options: ClientOptionsOut<T>): Promise<unknown>;
24
- }
25
-
26
- interface FetchWithContext<TClientContext extends ClientContext> {
27
- (url: URL, init: RequestInit, options: ClientOptionsOut<TClientContext>, path: readonly string[], input: unknown): Promise<Response>;
28
43
  }
29
44
 
30
- export { type FetchWithContext, LinkFetchClient, type LinkFetchClientOptions, RPCLink, type RPCLinkOptions };
45
+ export { LinkFetchClient, RPCLink };
46
+ export type { LinkFetchClientOptions, LinkFetchInterceptorOptions, RPCLinkOptions };
@@ -1,30 +1,46 @@
1
+ import { Interceptor } from '@orpc/shared';
1
2
  import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
2
3
  import { ToFetchRequestOptions } from '@orpc/standard-server-fetch';
3
- import { a as ClientContext, C as ClientOptionsOut, b as ClientLink } from '../../shared/client.CupM8eRP.js';
4
- import { b as StandardLinkClient } from '../../shared/client.aGal-uGY.js';
5
- import { StandardRPCLinkOptions } from '../standard/index.js';
6
- import '@orpc/shared';
4
+ import { b as ClientContext, c as ClientOptions } from '../../shared/client.BH1AYT_p.js';
5
+ import { a as StandardLinkPlugin, f as StandardLinkClient } from '../../shared/client.De8SW4Kw.js';
6
+ import { f as StandardRPCLinkOptions, g as StandardRPCLink } from '../../shared/client.BxV-mzeR.js';
7
7
 
8
+ interface LinkFetchPlugin<T extends ClientContext> extends StandardLinkPlugin<T> {
9
+ initRuntimeAdapter?(options: LinkFetchClientOptions<T>): void;
10
+ }
11
+
12
+ interface LinkFetchInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
13
+ request: Request;
14
+ init: {
15
+ redirect?: Request['redirect'];
16
+ };
17
+ path: readonly string[];
18
+ input: unknown;
19
+ }
8
20
  interface LinkFetchClientOptions<T extends ClientContext> extends ToFetchRequestOptions {
9
- fetch?: (request: Request, init: Record<never, never>, options: ClientOptionsOut<T>, path: readonly string[], input: unknown) => Promise<Response>;
21
+ fetch?: (request: Request, init: LinkFetchInterceptorOptions<T>['init'], options: ClientOptions<T>, path: readonly string[], input: unknown) => Promise<Response>;
22
+ adapterInterceptors?: Interceptor<LinkFetchInterceptorOptions<T>, Promise<Response>>[];
23
+ plugins?: LinkFetchPlugin<T>[];
10
24
  }
11
25
  declare class LinkFetchClient<T extends ClientContext> implements StandardLinkClient<T> {
12
26
  private readonly fetch;
13
27
  private readonly toFetchRequestOptions;
28
+ private readonly adapterInterceptors;
14
29
  constructor(options: LinkFetchClientOptions<T>);
15
- call(request: StandardRequest, options: ClientOptionsOut<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
30
+ call(standardRequest: StandardRequest, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
16
31
  }
17
32
 
18
- interface RPCLinkOptions<T extends ClientContext> extends StandardRPCLinkOptions<T>, LinkFetchClientOptions<T> {
33
+ interface RPCLinkOptions<T extends ClientContext> extends LinkFetchClientOptions<T>, Omit<StandardRPCLinkOptions<T>, 'plugins'> {
19
34
  }
20
- declare class RPCLink<T extends ClientContext> implements ClientLink<T> {
21
- private readonly standardLink;
35
+ /**
36
+ * The RPC Link communicates with the server using the RPC protocol.
37
+ *
38
+ * @see {@link https://orpc.unnoq.com/docs/client/rpc-link RPC Link Docs}
39
+ * @see {@link https://orpc.unnoq.com/docs/advanced/rpc-protocol RPC Protocol Docs}
40
+ */
41
+ declare class RPCLink<T extends ClientContext> extends StandardRPCLink<T> {
22
42
  constructor(options: RPCLinkOptions<T>);
23
- call(path: readonly string[], input: unknown, options: ClientOptionsOut<T>): Promise<unknown>;
24
- }
25
-
26
- interface FetchWithContext<TClientContext extends ClientContext> {
27
- (url: URL, init: RequestInit, options: ClientOptionsOut<TClientContext>, path: readonly string[], input: unknown): Promise<Response>;
28
43
  }
29
44
 
30
- export { type FetchWithContext, LinkFetchClient, type LinkFetchClientOptions, RPCLink, type RPCLinkOptions };
45
+ export { LinkFetchClient, RPCLink };
46
+ export type { LinkFetchClientOptions, LinkFetchInterceptorOptions, RPCLinkOptions };
@@ -1,36 +1,44 @@
1
+ import { toArray, intercept } from '@orpc/shared';
1
2
  import { toFetchRequest, toStandardLazyResponse } from '@orpc/standard-server-fetch';
2
- import { b as StandardRPCJsonSerializer, c as StandardRPCLinkCodec, d as StandardRPCSerializer, S as StandardLink } from '../../shared/client.Be-O_tdu.mjs';
3
- import '@orpc/shared';
4
- import '../../shared/client.CvnV7_uV.mjs';
3
+ import { C as CompositeStandardLinkPlugin, c as StandardRPCLink } from '../../shared/client.ufURaY18.mjs';
5
4
  import '@orpc/standard-server';
6
- import '../../shared/client.BacCdg3F.mjs';
5
+ import '../../shared/client.BW3SBw-r.mjs';
6
+
7
+ class CompositeLinkFetchPlugin extends CompositeStandardLinkPlugin {
8
+ initRuntimeAdapter(options) {
9
+ for (const plugin of this.plugins) {
10
+ plugin.initRuntimeAdapter?.(options);
11
+ }
12
+ }
13
+ }
7
14
 
8
15
  class LinkFetchClient {
9
16
  fetch;
10
17
  toFetchRequestOptions;
18
+ adapterInterceptors;
11
19
  constructor(options) {
12
- this.fetch = options?.fetch ?? globalThis.fetch.bind(globalThis);
20
+ const plugin = new CompositeLinkFetchPlugin(options.plugins);
21
+ plugin.initRuntimeAdapter(options);
22
+ this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
13
23
  this.toFetchRequestOptions = options;
24
+ this.adapterInterceptors = toArray(options.adapterInterceptors);
14
25
  }
15
- async call(request, options, path, input) {
16
- const fetchRequest = toFetchRequest(request, this.toFetchRequestOptions);
17
- const fetchResponse = await this.fetch(fetchRequest, {}, options, path, input);
18
- const lazyResponse = toStandardLazyResponse(fetchResponse);
26
+ async call(standardRequest, options, path, input) {
27
+ const request = toFetchRequest(standardRequest, this.toFetchRequestOptions);
28
+ const fetchResponse = await intercept(
29
+ this.adapterInterceptors,
30
+ { ...options, request, path, input, init: { redirect: "manual" } },
31
+ ({ request: request2, path: path2, input: input2, init, ...options2 }) => this.fetch(request2, init, options2, path2, input2)
32
+ );
33
+ const lazyResponse = toStandardLazyResponse(fetchResponse, { signal: request.signal });
19
34
  return lazyResponse;
20
35
  }
21
36
  }
22
37
 
23
- class RPCLink {
24
- standardLink;
38
+ class RPCLink extends StandardRPCLink {
25
39
  constructor(options) {
26
- const jsonSerializer = new StandardRPCJsonSerializer(options);
27
- const serializer = new StandardRPCSerializer(jsonSerializer);
28
- const linkCodec = new StandardRPCLinkCodec(serializer, options);
29
40
  const linkClient = new LinkFetchClient(options);
30
- this.standardLink = new StandardLink(linkCodec, linkClient, options);
31
- }
32
- async call(path, input, options) {
33
- return this.standardLink.call(path, input, options);
41
+ super(linkClient, options);
34
42
  }
35
43
  }
36
44
 
@@ -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.mjs';
5
+ import { f as StandardLinkClient } from '../../shared/client.CPgZaUox.mjs';
6
+ import { f as StandardRPCLinkOptions, g as StandardRPCLink } from '../../shared/client.D8lMmWVC.mjs';
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,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,86 @@
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.ufURaY18.mjs';
4
+ import '@orpc/standard-server';
5
+ import '../../shared/client.BW3SBw-r.mjs';
6
+ import '@orpc/standard-server-fetch';
7
+
8
+ function postMessagePortMessage(port, data, transfer) {
9
+ if (transfer) {
10
+ port.postMessage(data, transfer);
11
+ } else {
12
+ port.postMessage(data);
13
+ }
14
+ }
15
+ function onMessagePortMessage(port, callback) {
16
+ if ("addEventListener" in port) {
17
+ port.addEventListener("message", (event) => {
18
+ callback(event.data);
19
+ });
20
+ } else if ("on" in port) {
21
+ port.on("message", (event) => {
22
+ callback(event?.data);
23
+ });
24
+ } else if ("onMessage" in port) {
25
+ port.onMessage.addListener((data) => {
26
+ callback(data);
27
+ });
28
+ } else {
29
+ throw new Error("Cannot find a addEventListener/on/onMessage method on the port");
30
+ }
31
+ }
32
+ function onMessagePortClose(port, callback) {
33
+ if ("addEventListener" in port) {
34
+ port.addEventListener("close", async () => {
35
+ callback();
36
+ });
37
+ } else if ("on" in port) {
38
+ port.on("close", async () => {
39
+ callback();
40
+ });
41
+ } else if ("onDisconnect" in port) {
42
+ port.onDisconnect.addListener(() => {
43
+ callback();
44
+ });
45
+ } else {
46
+ throw new Error("Cannot find a addEventListener/on/onDisconnect method on the port");
47
+ }
48
+ }
49
+
50
+ class LinkMessagePortClient {
51
+ peer;
52
+ constructor(options) {
53
+ this.peer = new experimental_ClientPeerWithoutCodec(async (message) => {
54
+ const [id, type, payload] = message;
55
+ const transfer = await value(options.experimental_transfer, message, options.port);
56
+ if (transfer) {
57
+ postMessagePortMessage(options.port, serializeRequestMessage(id, type, payload), transfer);
58
+ } else {
59
+ postMessagePortMessage(options.port, await encodeRequestMessage(id, type, payload));
60
+ }
61
+ });
62
+ onMessagePortMessage(options.port, async (message) => {
63
+ if (isObject(message)) {
64
+ await this.peer.message(deserializeResponseMessage(message));
65
+ } else {
66
+ await this.peer.message(await decodeResponseMessage(message));
67
+ }
68
+ });
69
+ onMessagePortClose(options.port, () => {
70
+ this.peer.close();
71
+ });
72
+ }
73
+ async call(request, _options, _path, _input) {
74
+ const response = await this.peer.request(request);
75
+ return { ...response, body: () => Promise.resolve(response.body) };
76
+ }
77
+ }
78
+
79
+ class RPCLink extends StandardRPCLink {
80
+ constructor(options) {
81
+ const linkClient = new LinkMessagePortClient(options);
82
+ super(linkClient, { ...options, url: "orpc://localhost" });
83
+ }
84
+ }
85
+
86
+ export { LinkMessagePortClient, RPCLink, onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
@@ -1,105 +1,11 @@
1
- import { c as StandardLinkCodec, S as StandardLinkOptions } from '../../shared/client.DrOAzyMB.mjs';
2
- export { I as InvalidEventIteratorRetryResponse, d as StandardLink, b as StandardLinkClient } from '../../shared/client.DrOAzyMB.mjs';
3
- import { Segment, Value } from '@orpc/shared';
4
- import { a as ClientContext, C as ClientOptionsOut } from '../../shared/client.CupM8eRP.mjs';
5
- import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
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';
6
6
 
7
- declare const STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES: {
8
- readonly BIGINT: 0;
9
- readonly DATE: 1;
10
- readonly NAN: 2;
11
- readonly UNDEFINED: 3;
12
- readonly URL: 4;
13
- readonly REGEXP: 5;
14
- readonly SET: 6;
15
- readonly MAP: 7;
16
- };
17
- type StandardRPCJsonSerializedMetaItem = readonly [type: number, ...path: Segment[]];
18
- type StandardRPCJsonSerialized = [json: unknown, meta: StandardRPCJsonSerializedMetaItem[], maps: Segment[][], blobs: Blob[]];
19
- interface StandardRPCCustomJsonSerializer {
20
- type: number;
21
- condition(data: unknown): boolean;
22
- serialize(data: any): unknown;
23
- deserialize(serialized: any): unknown;
24
- }
25
- interface StandardRPCJsonSerializerOptions {
26
- customJsonSerializers?: readonly StandardRPCCustomJsonSerializer[];
27
- }
28
- declare class StandardRPCJsonSerializer {
29
- private readonly customSerializers;
30
- constructor(options?: StandardRPCJsonSerializerOptions);
31
- serialize(data: unknown, segments?: Segment[], meta?: StandardRPCJsonSerializedMetaItem[], maps?: Segment[][], blobs?: Blob[]): StandardRPCJsonSerialized;
32
- deserialize(json: unknown, meta: readonly StandardRPCJsonSerializedMetaItem[]): unknown;
33
- deserialize(json: unknown, meta: readonly StandardRPCJsonSerializedMetaItem[], maps: readonly Segment[][], getBlob: (index: number) => Blob): unknown;
34
- }
7
+ declare function toHttpPath(path: readonly string[]): HTTPPath;
8
+ declare function toStandardHeaders(headers: Headers | StandardHeaders): StandardHeaders;
9
+ declare function getMalformedResponseErrorCode(status: number): string;
35
10
 
36
- declare class StandardRPCSerializer {
37
- #private;
38
- private readonly jsonSerializer;
39
- constructor(jsonSerializer: StandardRPCJsonSerializer);
40
- serialize(data: unknown): object;
41
- deserialize(data: unknown): unknown;
42
- }
43
-
44
- type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
45
- interface StandardRPCLinkCodecOptions<T extends ClientContext> {
46
- /**
47
- * Base url for all requests.
48
- */
49
- url: Value<string | URL, [
50
- options: ClientOptionsOut<T>,
51
- path: readonly string[],
52
- input: unknown
53
- ]>;
54
- /**
55
- * The maximum length of the URL.
56
- *
57
- * @default 2083
58
- */
59
- maxUrlLength?: Value<number, [
60
- options: ClientOptionsOut<T>,
61
- path: readonly string[],
62
- input: unknown
63
- ]>;
64
- /**
65
- * The method used to make the request.
66
- *
67
- * @default 'POST'
68
- */
69
- method?: Value<HTTPMethod, [
70
- options: ClientOptionsOut<T>,
71
- path: readonly string[],
72
- input: unknown
73
- ]>;
74
- /**
75
- * The method to use when the payload cannot safely pass to the server with method return from method function.
76
- * GET is not allowed, it's very dangerous.
77
- *
78
- * @default 'POST'
79
- */
80
- fallbackMethod?: Exclude<HTTPMethod, 'GET'>;
81
- /**
82
- * Inject headers to the request.
83
- */
84
- headers?: Value<StandardHeaders, [
85
- options: ClientOptionsOut<T>,
86
- path: readonly string[],
87
- input: unknown
88
- ]>;
89
- }
90
- declare class StandardRPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
91
- private readonly serializer;
92
- private readonly baseUrl;
93
- private readonly maxUrlLength;
94
- private readonly fallbackMethod;
95
- private readonly expectedMethod;
96
- private readonly headers;
97
- constructor(serializer: StandardRPCSerializer, options: StandardRPCLinkCodecOptions<T>);
98
- encode(path: readonly string[], input: unknown, options: ClientOptionsOut<any>): Promise<StandardRequest>;
99
- decode(response: StandardLazyResponse): Promise<unknown>;
100
- }
101
-
102
- interface StandardRPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardRPCLinkCodecOptions<T>, StandardRPCJsonSerializerOptions {
103
- }
104
-
105
- export { STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES, StandardLinkCodec, StandardLinkOptions, type StandardRPCCustomJsonSerializer, type StandardRPCJsonSerialized, type StandardRPCJsonSerializedMetaItem, StandardRPCJsonSerializer, type StandardRPCJsonSerializerOptions, StandardRPCLinkCodec, type StandardRPCLinkCodecOptions, type StandardRPCLinkOptions, StandardRPCSerializer };
11
+ export { getMalformedResponseErrorCode, toHttpPath, toStandardHeaders };