@orpc/client 0.0.0-next.e563486 → 0.0.0-next.e565e67
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.
- package/README.md +31 -22
- package/dist/adapters/fetch/index.d.mts +31 -15
- package/dist/adapters/fetch/index.d.ts +31 -15
- package/dist/adapters/fetch/index.mjs +27 -18
- package/dist/adapters/message-port/index.d.mts +80 -0
- package/dist/adapters/message-port/index.d.ts +80 -0
- package/dist/adapters/message-port/index.mjs +87 -0
- package/dist/adapters/standard/index.d.mts +9 -103
- package/dist/adapters/standard/index.d.ts +9 -103
- package/dist/adapters/standard/index.mjs +4 -3
- package/dist/adapters/websocket/index.d.mts +29 -0
- package/dist/adapters/websocket/index.d.ts +29 -0
- package/dist/adapters/websocket/index.mjs +47 -0
- package/dist/index.d.mts +107 -27
- package/dist/index.d.ts +107 -27
- package/dist/index.mjs +85 -36
- package/dist/plugins/index.d.mts +212 -25
- package/dist/plugins/index.d.ts +212 -25
- package/dist/plugins/index.mjs +419 -61
- package/dist/shared/client.BH1AYT_p.d.mts +83 -0
- package/dist/shared/client.BH1AYT_p.d.ts +83 -0
- package/dist/shared/client.BLtwTQUg.mjs +40 -0
- package/dist/shared/client.BxV-mzeR.d.ts +91 -0
- package/dist/shared/client.CPgZaUox.d.mts +45 -0
- package/dist/shared/{client.CjUckqXO.mjs → client.CdE4ChCn.mjs} +108 -47
- package/dist/shared/client.Cs5F1cjw.mjs +171 -0
- package/dist/shared/client.D8lMmWVC.d.mts +91 -0
- package/dist/shared/client.De8SW4Kw.d.ts +45 -0
- package/package.json +16 -5
- package/dist/shared/client.BacCdg3F.mjs +0 -172
- package/dist/shared/client.CupM8eRP.d.mts +0 -30
- package/dist/shared/client.CupM8eRP.d.ts +0 -30
- package/dist/shared/client.CvnV7_uV.mjs +0 -12
- package/dist/shared/client.DrOAzyMB.d.mts +0 -45
- 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
|
|
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
|
-
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
41
|
-
-
|
|
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 {
|
|
4
|
-
import {
|
|
5
|
-
import { StandardRPCLinkOptions } from '
|
|
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:
|
|
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(
|
|
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
|
|
33
|
+
interface RPCLinkOptions<T extends ClientContext> extends LinkFetchClientOptions<T>, Omit<StandardRPCLinkOptions<T>, 'plugins'> {
|
|
19
34
|
}
|
|
20
|
-
|
|
21
|
-
|
|
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 {
|
|
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 {
|
|
4
|
-
import {
|
|
5
|
-
import { StandardRPCLinkOptions } from '
|
|
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:
|
|
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(
|
|
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
|
|
33
|
+
interface RPCLinkOptions<T extends ClientContext> extends LinkFetchClientOptions<T>, Omit<StandardRPCLinkOptions<T>, 'plugins'> {
|
|
19
34
|
}
|
|
20
|
-
|
|
21
|
-
|
|
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 {
|
|
45
|
+
export { LinkFetchClient, RPCLink };
|
|
46
|
+
export type { LinkFetchClientOptions, LinkFetchInterceptorOptions, RPCLinkOptions };
|
|
@@ -1,36 +1,45 @@
|
|
|
1
|
+
import { toArray, intercept } from '@orpc/shared';
|
|
1
2
|
import { toFetchRequest, toStandardLazyResponse } from '@orpc/standard-server-fetch';
|
|
2
|
-
import {
|
|
3
|
-
import '@orpc/shared';
|
|
4
|
-
import '../../shared/client.CvnV7_uV.mjs';
|
|
3
|
+
import { C as CompositeStandardLinkPlugin, c as StandardRPCLink } from '../../shared/client.CdE4ChCn.mjs';
|
|
5
4
|
import '@orpc/standard-server';
|
|
6
|
-
import '../../shared/client.
|
|
5
|
+
import '../../shared/client.Cs5F1cjw.mjs';
|
|
6
|
+
import '../../shared/client.BLtwTQUg.mjs';
|
|
7
|
+
|
|
8
|
+
class CompositeLinkFetchPlugin extends CompositeStandardLinkPlugin {
|
|
9
|
+
initRuntimeAdapter(options) {
|
|
10
|
+
for (const plugin of this.plugins) {
|
|
11
|
+
plugin.initRuntimeAdapter?.(options);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
7
15
|
|
|
8
16
|
class LinkFetchClient {
|
|
9
17
|
fetch;
|
|
10
18
|
toFetchRequestOptions;
|
|
19
|
+
adapterInterceptors;
|
|
11
20
|
constructor(options) {
|
|
12
|
-
|
|
21
|
+
const plugin = new CompositeLinkFetchPlugin(options.plugins);
|
|
22
|
+
plugin.initRuntimeAdapter(options);
|
|
23
|
+
this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
13
24
|
this.toFetchRequestOptions = options;
|
|
25
|
+
this.adapterInterceptors = toArray(options.adapterInterceptors);
|
|
14
26
|
}
|
|
15
|
-
async call(
|
|
16
|
-
const
|
|
17
|
-
const fetchResponse = await
|
|
18
|
-
|
|
27
|
+
async call(standardRequest, options, path, input) {
|
|
28
|
+
const request = toFetchRequest(standardRequest, this.toFetchRequestOptions);
|
|
29
|
+
const fetchResponse = await intercept(
|
|
30
|
+
this.adapterInterceptors,
|
|
31
|
+
{ ...options, request, path, input, init: { redirect: "manual" } },
|
|
32
|
+
({ request: request2, path: path2, input: input2, init, ...options2 }) => this.fetch(request2, init, options2, path2, input2)
|
|
33
|
+
);
|
|
34
|
+
const lazyResponse = toStandardLazyResponse(fetchResponse, { signal: request.signal });
|
|
19
35
|
return lazyResponse;
|
|
20
36
|
}
|
|
21
37
|
}
|
|
22
38
|
|
|
23
|
-
class RPCLink {
|
|
24
|
-
standardLink;
|
|
39
|
+
class RPCLink extends StandardRPCLink {
|
|
25
40
|
constructor(options) {
|
|
26
|
-
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
|
27
|
-
const serializer = new StandardRPCSerializer(jsonSerializer);
|
|
28
|
-
const linkCodec = new StandardRPCLinkCodec(serializer, options);
|
|
29
41
|
const linkClient = new LinkFetchClient(options);
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
async call(path, input, options) {
|
|
33
|
-
return this.standardLink.call(path, input, options);
|
|
42
|
+
super(linkClient, options);
|
|
34
43
|
}
|
|
35
44
|
}
|
|
36
45
|
|
|
@@ -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,87 @@
|
|
|
1
|
+
import { value, isObject } from '@orpc/shared';
|
|
2
|
+
import { experimental_ClientPeerWithoutCodec, serializeRequestMessage, encodeRequestMessage, deserializeResponseMessage, decodeResponseMessage } from '@orpc/standard-server-peer';
|
|
3
|
+
import { c as StandardRPCLink } from '../../shared/client.CdE4ChCn.mjs';
|
|
4
|
+
import '@orpc/standard-server';
|
|
5
|
+
import '../../shared/client.Cs5F1cjw.mjs';
|
|
6
|
+
import '@orpc/standard-server-fetch';
|
|
7
|
+
import '../../shared/client.BLtwTQUg.mjs';
|
|
8
|
+
|
|
9
|
+
function postMessagePortMessage(port, data, transfer) {
|
|
10
|
+
if (transfer) {
|
|
11
|
+
port.postMessage(data, transfer);
|
|
12
|
+
} else {
|
|
13
|
+
port.postMessage(data);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function onMessagePortMessage(port, callback) {
|
|
17
|
+
if ("addEventListener" in port) {
|
|
18
|
+
port.addEventListener("message", (event) => {
|
|
19
|
+
callback(event.data);
|
|
20
|
+
});
|
|
21
|
+
} else if ("on" in port) {
|
|
22
|
+
port.on("message", (event) => {
|
|
23
|
+
callback(event?.data);
|
|
24
|
+
});
|
|
25
|
+
} else if ("onMessage" in port) {
|
|
26
|
+
port.onMessage.addListener((data) => {
|
|
27
|
+
callback(data);
|
|
28
|
+
});
|
|
29
|
+
} else {
|
|
30
|
+
throw new Error("Cannot find a addEventListener/on/onMessage method on the port");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function onMessagePortClose(port, callback) {
|
|
34
|
+
if ("addEventListener" in port) {
|
|
35
|
+
port.addEventListener("close", async () => {
|
|
36
|
+
callback();
|
|
37
|
+
});
|
|
38
|
+
} else if ("on" in port) {
|
|
39
|
+
port.on("close", async () => {
|
|
40
|
+
callback();
|
|
41
|
+
});
|
|
42
|
+
} else if ("onDisconnect" in port) {
|
|
43
|
+
port.onDisconnect.addListener(() => {
|
|
44
|
+
callback();
|
|
45
|
+
});
|
|
46
|
+
} else {
|
|
47
|
+
throw new Error("Cannot find a addEventListener/on/onDisconnect method on the port");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
class LinkMessagePortClient {
|
|
52
|
+
peer;
|
|
53
|
+
constructor(options) {
|
|
54
|
+
this.peer = new experimental_ClientPeerWithoutCodec(async (message) => {
|
|
55
|
+
const [id, type, payload] = message;
|
|
56
|
+
const transfer = await value(options.experimental_transfer, message, options.port);
|
|
57
|
+
if (transfer) {
|
|
58
|
+
postMessagePortMessage(options.port, serializeRequestMessage(id, type, payload), transfer);
|
|
59
|
+
} else {
|
|
60
|
+
postMessagePortMessage(options.port, await encodeRequestMessage(id, type, payload));
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
onMessagePortMessage(options.port, async (message) => {
|
|
64
|
+
if (isObject(message)) {
|
|
65
|
+
await this.peer.message(deserializeResponseMessage(message));
|
|
66
|
+
} else {
|
|
67
|
+
await this.peer.message(await decodeResponseMessage(message));
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
onMessagePortClose(options.port, () => {
|
|
71
|
+
this.peer.close();
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
async call(request, _options, _path, _input) {
|
|
75
|
+
const response = await this.peer.request(request);
|
|
76
|
+
return { ...response, body: () => Promise.resolve(response.body) };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
class RPCLink extends StandardRPCLink {
|
|
81
|
+
constructor(options) {
|
|
82
|
+
const linkClient = new LinkMessagePortClient(options);
|
|
83
|
+
super(linkClient, { ...options, url: "orpc://localhost" });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { LinkMessagePortClient, RPCLink, onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
|
|
@@ -1,105 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
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
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
37
|
-
#private;
|
|
38
|
-
private readonly jsonSerializer;
|
|
39
|
-
constructor(jsonSerializer: StandardRPCJsonSerializer);
|
|
40
|
-
serialize(data: unknown): unknown;
|
|
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 };
|