@orpc/client 1.14.9 → 1.14.11

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 (38) hide show
  1. package/README.md +52 -68
  2. package/dist/adapters/fetch/index.d.mts +54 -35
  3. package/dist/adapters/fetch/index.d.ts +54 -35
  4. package/dist/adapters/fetch/index.mjs +74 -27
  5. package/dist/adapters/message-port/index.d.mts +25 -30
  6. package/dist/adapters/message-port/index.d.ts +25 -30
  7. package/dist/adapters/message-port/index.mjs +39 -30
  8. package/dist/adapters/standard/index.d.mts +59 -9
  9. package/dist/adapters/standard/index.d.ts +59 -9
  10. package/dist/adapters/standard/index.mjs +5 -5
  11. package/dist/adapters/websocket/index.d.mts +113 -21
  12. package/dist/adapters/websocket/index.d.ts +113 -21
  13. package/dist/adapters/websocket/index.mjs +95 -37
  14. package/dist/index.d.mts +75 -184
  15. package/dist/index.d.ts +75 -184
  16. package/dist/index.mjs +74 -63
  17. package/dist/plugins/index.d.mts +191 -122
  18. package/dist/plugins/index.d.ts +191 -122
  19. package/dist/plugins/index.mjs +627 -281
  20. package/dist/shared/client.8f4DNmdE.d.mts +96 -0
  21. package/dist/shared/client.BBZBQID8.d.mts +167 -0
  22. package/dist/shared/client.BBZBQID8.d.ts +167 -0
  23. package/dist/shared/client.BMKYqpdy.d.ts +96 -0
  24. package/dist/shared/client.BRJnOJ0R.mjs +174 -0
  25. package/dist/shared/client.BdItY5DT.d.mts +111 -0
  26. package/dist/shared/client.BdItY5DT.d.ts +111 -0
  27. package/dist/shared/client.Dnfj8jnT.mjs +92 -0
  28. package/dist/shared/client.DqYwRDUO.mjs +343 -0
  29. package/package.json +7 -7
  30. package/dist/shared/client.2jUAqzYU.d.ts +0 -45
  31. package/dist/shared/client.B3pNRBih.d.ts +0 -91
  32. package/dist/shared/client.BFAVy68H.d.mts +0 -91
  33. package/dist/shared/client.BLtwTQUg.mjs +0 -40
  34. package/dist/shared/client.CFQL4ewg.mjs +0 -174
  35. package/dist/shared/client.CpCa3si8.d.mts +0 -45
  36. package/dist/shared/client.D2nvAALa.mjs +0 -404
  37. package/dist/shared/client.i2uoJbEp.d.mts +0 -83
  38. package/dist/shared/client.i2uoJbEp.d.ts +0 -83
package/README.md CHANGED
@@ -1,17 +1,16 @@
1
- <div align="center">
2
- <image align="center" src="https://orpc.dev/logo.webp" width=280 alt="oRPC logo" />
3
- </div>
4
-
5
- <h1></h1>
1
+ <h1 align="center">oRPC - Typesafe APIs Made Simple 🪄</h1>
6
2
 
7
3
  <div align="center">
8
4
  <a href="https://codecov.io/gh/middleapi/orpc">
9
- <img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/1.x/graph/badge.svg">
5
+ <img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
10
6
  </a>
11
7
  <a href="https://www.npmjs.com/package/@orpc/client">
12
8
  <img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fclient?logo=npm" />
13
9
  </a>
14
- <a href="https://github.com/middleapi/orpc/blob/1.x/LICENSE">
10
+ <a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
11
+ <img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
12
+ </a>
13
+ <a href="https://github.com/middleapi/orpc/blob/main/LICENSE">
15
14
  <img alt="MIT License" src="https://img.shields.io/github/license/middleapi/orpc?logo=open-source-initiative" />
16
15
  </a>
17
16
  <a href="https://discord.gg/TXEbwRBvQn">
@@ -22,75 +21,53 @@
22
21
  </a>
23
22
  </div>
24
23
 
25
- <h3 align="center">Typesafe APIs Made Simple 🪄</h3>
24
+ ## Documentation
26
25
 
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
26
+ You can read the documentation [here](https://orpc.dev).
28
27
 
29
- ---
28
+ ## Packages
30
29
 
31
- ## Highlights
30
+ **Core**
32
31
 
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.
32
+ - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Define API contract as the single source of truth.
33
+ - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build APIs or implement contracts.
34
+ - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume APIs with end-to-end type safety.
35
+ - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Add OpenAPI compatibility to APIs.
45
36
 
46
- ## Documentation
37
+ **Schema validation**
47
38
 
48
- You can find the full documentation [here](https://orpc.dev).
39
+ - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): Integrate with [Zod](https://zod.dev/).
40
+ - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): Integrate with [Valibot](https://valibot.dev/).
41
+ - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): Integrate with [ArkType](https://arktype.io/).
49
42
 
50
- ## Packages
43
+ **Built-in features**
44
+
45
+ - [@orpc/publisher](https://www.npmjs.com/package/@orpc/publisher): Pub/Sub with memory, Redis, and Upstash adapters.
46
+ - [@orpc/ratelimit](https://www.npmjs.com/package/@orpc/ratelimit): Rate limiting with memory, Redis, and Upstash adapters.
47
+ - [@orpc/json-schema](https://www.npmjs.com/package/@orpc/json-schema): Smart coercion for OpenAPI requests.
51
48
 
52
- - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
53
- - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
54
- - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
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.
63
- - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
64
- - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
65
- - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
66
-
67
- ## `@orpc/client`
68
-
69
- Consume your API on the client with type-safety. Read the [documentation](https://orpc.dev/docs/client/client-side) for more information.
70
-
71
- ```ts
72
- import { createORPCClient } from '@orpc/client'
73
- import { RPCLink } from '@orpc/client/fetch'
74
- import { ContractRouterClient } from '@orpc/contract'
75
- import { RouterClient } from '@orpc/server'
76
-
77
- const link = new RPCLink({
78
- url: 'http://localhost:3000/rpc',
79
- headers: () => ({
80
- authorization: 'Bearer token',
81
- }),
82
- // fetch: <-- provide fetch polyfill fetch if needed
83
- })
84
-
85
- // Create a client for your router
86
- const client: RouterClient<typeof router> = createORPCClient(link)
87
- // Or, create a client using a contract
88
- const client: ContractRouterClient<typeof contract> = createORPCClient(link)
89
- ```
49
+ **Framework & ecosystem integrations**
50
+
51
+ - [@orpc/next](https://www.npmjs.com/package/@orpc/next): Integrate with [Next.js Server Functions](https://nextjs.org/docs/app/getting-started/mutating-data).
52
+ - [@orpc/ai-sdk](https://www.npmjs.com/package/@orpc/ai-sdk): Turn contracts and procedures into [AI SDK](https://ai-sdk.dev/) tools.
53
+ - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): Integrate with [TanStack Query](https://tanstack.com/query/latest).
54
+ - [@orpc/pinia-colada](https://www.npmjs.com/package/@orpc/pinia-colada): Integrate with [Pinia Colada](https://pinia-colada.esm.dev/).
55
+ - [@orpc/swr](https://www.npmjs.com/package/@orpc/swr): Integrate with [SWR](https://swr.vercel.app/).
56
+ - [@orpc/experimental-effect](https://www.npmjs.com/package/@orpc/experimental-effect): Integrate with [Effect](https://effect.website/).
57
+ - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Implement your contract with [NestJS](https://nestjs.com/).
58
+ - [@orpc/bun](https://www.npmjs.com/package/@orpc/bun): Adapters for [Bun's Redis](https://bun.sh/).
59
+ - [@orpc/cloudflare](https://www.npmjs.com/package/@orpc/cloudflare): Adapters for [Cloudflare's RateLimit and Durable Objects](https://developers.cloudflare.com/workers/).
60
+ - [@orpc/trpc](https://www.npmjs.com/package/@orpc/trpc): Reuse existing [tRPC](https://trpc.io/) routers within oRPC.
61
+
62
+ **Observability**
63
+
64
+ - [@orpc/opentelemetry](https://www.npmjs.com/package/@orpc/opentelemetry): Integrate with [OpenTelemetry](https://opentelemetry.io/) for distributed tracing.
65
+ - [@orpc/pino](https://www.npmjs.com/package/@orpc/pino): Integrate with [Pino](https://getpino.io/) for logging.
66
+ - [@orpc/evlog](https://www.npmjs.com/package/@orpc/evlog): Integrate with [Evlog](https://evlog.dev/) for logging.
90
67
 
91
68
  ## Sponsors
92
69
 
93
- If you find oRPC valuable and would like to support its development, you can do so here: [GitHub Sponsors](https://github.com/sponsors/dinwwwh).
70
+ Like what we build over at [middleapi](https://github.com/middleapi)? You can help keep it going here: [GitHub Sponsors](https://github.com/sponsors/dinwwwh). Every bit helps! 🚀
94
71
 
95
72
  ### 🏆 Platinum Sponsor
96
73
 
@@ -132,11 +109,10 @@ If you find oRPC valuable and would like to support its development, you can do
132
109
  <td align="center"><a href="https://github.com/valerii15298?ref=orpc" target="_blank" rel="noopener" title="Valerii Petryniak"><img src="https://avatars.githubusercontent.com/u/44531564?u=88ac74d9bacd20401518441907acad21063cd397&amp;v=4" width="139" alt="Valerii Petryniak"/><br />Valerii Petryniak</a></td>
133
110
  <td align="center"><a href="https://github.com/letstri?ref=orpc" target="_blank" rel="noopener" title="Valerii Strilets"><img src="https://avatars.githubusercontent.com/u/13253748?u=c7b10399ccc8f8081e24db94ec32cd9858e86ac3&amp;v=4" width="139" alt="Valerii Strilets"/><br />Valerii Strilets</a></td>
134
111
  <td align="center"><a href="https://github.com/K-Mistele?ref=orpc" target="_blank" rel="noopener" title="Kyle Mistele"><img src="https://avatars.githubusercontent.com/u/18430555?u=3afebeb81de666e35aaac3ed46f14159d7603ffb&amp;v=4" width="139" alt="Kyle Mistele"/><br />Kyle Mistele</a></td>
135
- <td align="center"><a href="https://github.com/R44VC0RP?ref=orpc" target="_blank" rel="noopener" title="Ryan Vogel"><img src="https://avatars.githubusercontent.com/u/89211796?u=1857347b9787d8d8a7ea5bfc333f96be92d5a683&amp;v=4" width="139" alt="Ryan Vogel"/><br />Ryan Vogel</a></td>
136
112
  <td align="center"><a href="https://github.com/christ12938?ref=orpc" target="_blank" rel="noopener" title="christ12938"><img src="https://avatars.githubusercontent.com/u/25758598?v=4" width="139" alt="christ12938"/><br />christ12938</a></td>
113
+ <td align="center"><a href="https://github.com/Ryanjso?ref=orpc" target="_blank" rel="noopener" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&amp;v=4" width="139" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
137
114
  </tr>
138
115
  <tr>
139
- <td align="center"><a href="https://github.com/Ryanjso?ref=orpc" target="_blank" rel="noopener" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&amp;v=4" width="139" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
140
116
  <td align="center"><a href="https://github.com/itigoore01?ref=orpc" target="_blank" rel="noopener" title="shota"><img src="https://avatars.githubusercontent.com/u/11831107?u=c976a6dc7e055eb026304c46c99100ed22b0c8e0&amp;v=4" width="139" alt="shota"/><br />shota</a></td>
141
117
  </tr>
142
118
  </table>
@@ -196,6 +172,7 @@ If you find oRPC valuable and would like to support its development, you can do
196
172
  <a href="https://github.com/nguyenlc1993?ref=orpc" target="_blank" rel="noopener" title="Lê Cao Nguyên"><img src="https://avatars.githubusercontent.com/u/13871971?u=83c8b69d9e35b589c4e1f066cc113b1d9461386f&amp;v=4" width="32" height="32" alt="Lê Cao Nguyên" /></a>
197
173
  <a href="https://github.com/wobsoriano?ref=orpc" target="_blank" rel="noopener" title="Robert Soriano"><img src="https://avatars.githubusercontent.com/u/13049130?u=6d72104182e7c9ed25934815313fb69107332111&amp;v=4" width="32" height="32" alt="Robert Soriano" /></a>
198
174
  <a href="https://github.com/andrewpeters9?ref=orpc" target="_blank" rel="noopener" title="Andrew Peters"><img src="https://avatars.githubusercontent.com/u/36251325?v=4" width="32" height="32" alt="Andrew Peters" /></a>
175
+ <a href="https://github.com/R44VC0RP?ref=orpc" target="_blank" rel="noopener" title="Ryan Vogel"><img src="https://avatars.githubusercontent.com/u/89211796?u=1857347b9787d8d8a7ea5bfc333f96be92d5a683&amp;v=4" width="32" height="32" alt="Ryan Vogel" /></a>
199
176
  <a href="https://github.com/SKostyukovich?ref=orpc" target="_blank" rel="noopener" title="SKostyukovich"><img src="https://avatars.githubusercontent.com/u/10700067?v=4" width="32" height="32" alt="SKostyukovich" /></a>
200
177
  <a href="https://github.com/peter-adam-dy?ref=orpc" target="_blank" rel="noopener" title="Peter Adam"><img src="https://avatars.githubusercontent.com/u/132129459?u=4f3dbbb3b443990b56acb7d6a5d11ed2c555f6db&amp;v=4" width="32" height="32" alt="Peter Adam" /></a>
201
178
  <a href="https://github.com/FabworksHQ?ref=orpc" target="_blank" rel="noopener" title="Fabworks"><img src="https://avatars.githubusercontent.com/u/160179500?v=4" width="32" height="32" alt="Fabworks" /></a>
@@ -210,6 +187,13 @@ If you find oRPC valuable and would like to support its development, you can do
210
187
  <a href="https://github.com/plancraft?ref=orpc" target="_blank" rel="noopener" title="plancraft"><img src="https://avatars.githubusercontent.com/u/46482287?v=4" width="32" height="32" alt="plancraft" /></a>
211
188
  </p>
212
189
 
190
+ ## References
191
+
192
+ oRPC is inspired by existing solutions that prioritize type safety and developer experience. Special acknowledgments to:
193
+
194
+ - [tRPC](https://trpc.io): For pioneering the concept of end-to-end type-safe RPC and influencing the development of type-safe APIs.
195
+ - [ts-rest](https://ts-rest.com): For its emphasis on contract-first development and OpenAPI integration, which have greatly inspired oRPC's feature set.
196
+
213
197
  ## License
214
198
 
215
- Distributed under the MIT License. See [LICENSE](https://github.com/middleapi/orpc/blob/1.x/LICENSE) for more information.
199
+ Distributed under the MIT License. See [LICENSE](https://github.com/middleapi/orpc/blob/main/LICENSE) for more information.
@@ -1,46 +1,65 @@
1
- import { Interceptor } from '@orpc/shared';
2
- import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
3
- import { ToFetchRequestOptions } from '@orpc/standard-server-fetch';
4
- import { b as ClientContext, c as ClientOptions } from '../../shared/client.i2uoJbEp.mjs';
5
- import { a as StandardLinkPlugin, f as StandardLinkClient } from '../../shared/client.CpCa3si8.mjs';
6
- import { f as StandardRPCLinkOptions, g as StandardRPCLink } from '../../shared/client.BFAVy68H.mjs';
1
+ import { C as ClientContext, a as ClientOptions } from '../../shared/client.BBZBQID8.mjs';
2
+ import { Value, Promisable, Interceptor } from '@orpc/shared';
3
+ import { StandardRequest, StandardLazyResponse } from '@standardserver/core';
4
+ import { ToFetchBodyOptions } from '@standardserver/fetch';
5
+ import { S as StandardLinkTransport, c as StandardLinkPlugin, a as StandardLink, b as StandardLinkOptions } from '../../shared/client.8f4DNmdE.mjs';
6
+ import { RPCLinkCodecOptions } from '../standard/index.mjs';
7
+ import '../../shared/client.BdItY5DT.mjs';
7
8
 
8
- interface LinkFetchPlugin<T extends ClientContext> extends StandardLinkPlugin<T> {
9
- initRuntimeAdapter?(options: LinkFetchClientOptions<T>): void;
9
+ interface FetchLinkTransportFetchInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
10
+ path: string[];
11
+ url: string;
12
+ init: RequestInit;
10
13
  }
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
- }
20
- interface LinkFetchClientOptions<T extends ClientContext> extends ToFetchRequestOptions {
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>[];
14
+ type FetchLinkTransportFetchInterceptor<T extends ClientContext> = Interceptor<FetchLinkTransportFetchInterceptorOptions<T>, Promise<Response>>;
15
+ interface FetchLinkTransportOptions<T extends ClientContext> {
16
+ /**
17
+ * The origin to prepend to all request URLs, useful for CORS requests.
18
+ *
19
+ * @example 'https://api.example.com'
20
+ * @example 'http://localhost:3000'
21
+ */
22
+ origin?: Value<Promisable<`https://${string}` | `http://${string}` | ({} & string) | undefined>, [options: ClientOptions<T>, path: string[]]>;
23
+ /**
24
+ * Options for how to convert the Standard Request to a Fetch Request, like event stream options, etc.
25
+ */
26
+ toFetchRequest?: undefined | ToFetchBodyOptions;
27
+ /**
28
+ * Override the default fetch implementation.
29
+ *
30
+ * @default globalThis.fetch.bind(globalThis)
31
+ */
32
+ fetch?(url: string, init: RequestInit, options: ClientOptions<T>, path: string[]): Promise<Response>;
33
+ /**
34
+ * Interceptors that execute before the actual fetch call, useful for modifying the fetch parameters, adding logging, etc.
35
+ */
36
+ fetchInterceptors?: FetchLinkTransportFetchInterceptor<T>[];
37
+ plugins?: FetchLinkTransportPlugin<T>[];
24
38
  }
25
- declare class LinkFetchClient<T extends ClientContext> implements StandardLinkClient<T> {
39
+ declare class FetchLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
40
+ private readonly origin;
26
41
  private readonly fetch;
27
42
  private readonly toFetchRequestOptions;
28
- private readonly adapterInterceptors;
29
- constructor(options: LinkFetchClientOptions<T>);
30
- call(standardRequest: StandardRequest, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
43
+ private readonly fetchInterceptors;
44
+ constructor(options: FetchLinkTransportOptions<T>);
45
+ send(standardRequest: StandardRequest, path: string[], options: ClientOptions<T>): Promise<StandardLazyResponse>;
46
+ }
47
+
48
+ interface FetchLinkTransportPlugin<T extends ClientContext> extends StandardLinkPlugin<T> {
49
+ initFetchLinkTransportOptions?(options: FetchLinkTransportOptions<T>): FetchLinkTransportOptions<T>;
50
+ }
51
+ declare class CompositeFetchLinkTransportPlugin<T extends ClientContext> implements FetchLinkTransportPlugin<T> {
52
+ protected readonly plugins: FetchLinkTransportPlugin<T>[];
53
+ name: string;
54
+ constructor(plugins?: FetchLinkTransportPlugin<T>[]);
55
+ initFetchLinkTransportOptions(options: FetchLinkTransportOptions<T>): FetchLinkTransportOptions<T>;
31
56
  }
32
57
 
33
- interface RPCLinkOptions<T extends ClientContext> extends LinkFetchClientOptions<T>, Omit<StandardRPCLinkOptions<T>, 'plugins'> {
58
+ interface RPCLinkOptions<T extends ClientContext> extends Omit<StandardLinkOptions<T>, 'plugins'>, FetchLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
34
59
  }
35
- /**
36
- * The RPC Link communicates with the server using the RPC protocol.
37
- *
38
- * @see {@link https://orpc.dev/docs/client/rpc-link RPC Link Docs}
39
- * @see {@link https://orpc.dev/docs/advanced/rpc-protocol RPC Protocol Docs}
40
- */
41
- declare class RPCLink<T extends ClientContext> extends StandardRPCLink<T> {
60
+ declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
42
61
  constructor(options: RPCLinkOptions<T>);
43
62
  }
44
63
 
45
- export { LinkFetchClient, RPCLink };
46
- export type { LinkFetchClientOptions, LinkFetchInterceptorOptions, RPCLinkOptions };
64
+ export { CompositeFetchLinkTransportPlugin, FetchLinkTransport, RPCLink };
65
+ export type { FetchLinkTransportFetchInterceptor, FetchLinkTransportFetchInterceptorOptions, FetchLinkTransportOptions, FetchLinkTransportPlugin, RPCLinkOptions };
@@ -1,46 +1,65 @@
1
- import { Interceptor } from '@orpc/shared';
2
- import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
3
- import { ToFetchRequestOptions } from '@orpc/standard-server-fetch';
4
- import { b as ClientContext, c as ClientOptions } from '../../shared/client.i2uoJbEp.js';
5
- import { a as StandardLinkPlugin, f as StandardLinkClient } from '../../shared/client.2jUAqzYU.js';
6
- import { f as StandardRPCLinkOptions, g as StandardRPCLink } from '../../shared/client.B3pNRBih.js';
1
+ import { C as ClientContext, a as ClientOptions } from '../../shared/client.BBZBQID8.js';
2
+ import { Value, Promisable, Interceptor } from '@orpc/shared';
3
+ import { StandardRequest, StandardLazyResponse } from '@standardserver/core';
4
+ import { ToFetchBodyOptions } from '@standardserver/fetch';
5
+ import { S as StandardLinkTransport, c as StandardLinkPlugin, a as StandardLink, b as StandardLinkOptions } from '../../shared/client.BMKYqpdy.js';
6
+ import { RPCLinkCodecOptions } from '../standard/index.js';
7
+ import '../../shared/client.BdItY5DT.js';
7
8
 
8
- interface LinkFetchPlugin<T extends ClientContext> extends StandardLinkPlugin<T> {
9
- initRuntimeAdapter?(options: LinkFetchClientOptions<T>): void;
9
+ interface FetchLinkTransportFetchInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
10
+ path: string[];
11
+ url: string;
12
+ init: RequestInit;
10
13
  }
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
- }
20
- interface LinkFetchClientOptions<T extends ClientContext> extends ToFetchRequestOptions {
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>[];
14
+ type FetchLinkTransportFetchInterceptor<T extends ClientContext> = Interceptor<FetchLinkTransportFetchInterceptorOptions<T>, Promise<Response>>;
15
+ interface FetchLinkTransportOptions<T extends ClientContext> {
16
+ /**
17
+ * The origin to prepend to all request URLs, useful for CORS requests.
18
+ *
19
+ * @example 'https://api.example.com'
20
+ * @example 'http://localhost:3000'
21
+ */
22
+ origin?: Value<Promisable<`https://${string}` | `http://${string}` | ({} & string) | undefined>, [options: ClientOptions<T>, path: string[]]>;
23
+ /**
24
+ * Options for how to convert the Standard Request to a Fetch Request, like event stream options, etc.
25
+ */
26
+ toFetchRequest?: undefined | ToFetchBodyOptions;
27
+ /**
28
+ * Override the default fetch implementation.
29
+ *
30
+ * @default globalThis.fetch.bind(globalThis)
31
+ */
32
+ fetch?(url: string, init: RequestInit, options: ClientOptions<T>, path: string[]): Promise<Response>;
33
+ /**
34
+ * Interceptors that execute before the actual fetch call, useful for modifying the fetch parameters, adding logging, etc.
35
+ */
36
+ fetchInterceptors?: FetchLinkTransportFetchInterceptor<T>[];
37
+ plugins?: FetchLinkTransportPlugin<T>[];
24
38
  }
25
- declare class LinkFetchClient<T extends ClientContext> implements StandardLinkClient<T> {
39
+ declare class FetchLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
40
+ private readonly origin;
26
41
  private readonly fetch;
27
42
  private readonly toFetchRequestOptions;
28
- private readonly adapterInterceptors;
29
- constructor(options: LinkFetchClientOptions<T>);
30
- call(standardRequest: StandardRequest, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
43
+ private readonly fetchInterceptors;
44
+ constructor(options: FetchLinkTransportOptions<T>);
45
+ send(standardRequest: StandardRequest, path: string[], options: ClientOptions<T>): Promise<StandardLazyResponse>;
46
+ }
47
+
48
+ interface FetchLinkTransportPlugin<T extends ClientContext> extends StandardLinkPlugin<T> {
49
+ initFetchLinkTransportOptions?(options: FetchLinkTransportOptions<T>): FetchLinkTransportOptions<T>;
50
+ }
51
+ declare class CompositeFetchLinkTransportPlugin<T extends ClientContext> implements FetchLinkTransportPlugin<T> {
52
+ protected readonly plugins: FetchLinkTransportPlugin<T>[];
53
+ name: string;
54
+ constructor(plugins?: FetchLinkTransportPlugin<T>[]);
55
+ initFetchLinkTransportOptions(options: FetchLinkTransportOptions<T>): FetchLinkTransportOptions<T>;
31
56
  }
32
57
 
33
- interface RPCLinkOptions<T extends ClientContext> extends LinkFetchClientOptions<T>, Omit<StandardRPCLinkOptions<T>, 'plugins'> {
58
+ interface RPCLinkOptions<T extends ClientContext> extends Omit<StandardLinkOptions<T>, 'plugins'>, FetchLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
34
59
  }
35
- /**
36
- * The RPC Link communicates with the server using the RPC protocol.
37
- *
38
- * @see {@link https://orpc.dev/docs/client/rpc-link RPC Link Docs}
39
- * @see {@link https://orpc.dev/docs/advanced/rpc-protocol RPC Protocol Docs}
40
- */
41
- declare class RPCLink<T extends ClientContext> extends StandardRPCLink<T> {
60
+ declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
42
61
  constructor(options: RPCLinkOptions<T>);
43
62
  }
44
63
 
45
- export { LinkFetchClient, RPCLink };
46
- export type { LinkFetchClientOptions, LinkFetchInterceptorOptions, RPCLinkOptions };
64
+ export { CompositeFetchLinkTransportPlugin, FetchLinkTransport, RPCLink };
65
+ export type { FetchLinkTransportFetchInterceptor, FetchLinkTransportFetchInterceptorOptions, FetchLinkTransportOptions, FetchLinkTransportPlugin, RPCLinkOptions };
@@ -1,46 +1,93 @@
1
- import { toArray, intercept } from '@orpc/shared';
2
- import { toFetchRequest, toStandardLazyResponse } from '@orpc/standard-server-fetch';
3
- import { C as CompositeStandardLinkPlugin, c as StandardRPCLink } from '../../shared/client.D2nvAALa.mjs';
4
- import '@orpc/standard-server';
5
- import '../../shared/client.CFQL4ewg.mjs';
6
- import '../../shared/client.BLtwTQUg.mjs';
1
+ import { sortPlugins, value, intercept, once } from '@orpc/shared';
2
+ import { toFetchBody, toFetchHeaders, toStandardLazyResponse } from '@standardserver/fetch';
3
+ import { S as StandardLink, R as RPCLinkCodec } from '../../shared/client.BRJnOJ0R.mjs';
4
+ import '@standardserver/core';
5
+ import '../../shared/client.Dnfj8jnT.mjs';
6
+ import '../../shared/client.DqYwRDUO.mjs';
7
7
 
8
- class CompositeLinkFetchPlugin extends CompositeStandardLinkPlugin {
9
- initRuntimeAdapter(options) {
8
+ class CompositeFetchLinkTransportPlugin {
9
+ constructor(plugins = []) {
10
+ this.plugins = plugins;
11
+ this.plugins = sortPlugins(plugins);
12
+ }
13
+ name = "~composite/fetch-link-transport";
14
+ initFetchLinkTransportOptions(options) {
10
15
  for (const plugin of this.plugins) {
11
- plugin.initRuntimeAdapter?.(options);
16
+ if (plugin.initFetchLinkTransportOptions) {
17
+ options = plugin.initFetchLinkTransportOptions(options);
18
+ }
12
19
  }
20
+ return options;
13
21
  }
14
22
  }
15
23
 
16
- class LinkFetchClient {
24
+ const GET_SUPPORTED_DUPLEX_MODE = once(() => {
25
+ try {
26
+ let duplex;
27
+ void new Request(
28
+ "https://example.com",
29
+ {
30
+ method: "POST",
31
+ body: new ReadableStream(),
32
+ get duplex() {
33
+ duplex = "half";
34
+ return "half";
35
+ }
36
+ }
37
+ );
38
+ return duplex;
39
+ } catch {
40
+ return void 0;
41
+ }
42
+ });
43
+ class FetchLinkTransport {
44
+ origin;
17
45
  fetch;
18
46
  toFetchRequestOptions;
19
- adapterInterceptors;
47
+ fetchInterceptors;
20
48
  constructor(options) {
21
- const plugin = new CompositeLinkFetchPlugin(options.plugins);
22
- plugin.initRuntimeAdapter(options);
49
+ options = new CompositeFetchLinkTransportPlugin(options.plugins).initFetchLinkTransportOptions(options);
50
+ this.origin = options.origin;
23
51
  this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
24
- this.toFetchRequestOptions = options;
25
- this.adapterInterceptors = toArray(options.adapterInterceptors);
52
+ this.toFetchRequestOptions = options.toFetchRequest;
53
+ this.fetchInterceptors = options.fetchInterceptors;
26
54
  }
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)
55
+ async send(standardRequest, path, options) {
56
+ let origin = await value(this.origin, options, path);
57
+ if (origin?.endsWith("/")) {
58
+ origin = origin.slice(0, -1);
59
+ }
60
+ const url = `${origin ?? ""}${standardRequest.url}`;
61
+ const [body, standardHeaders] = toFetchBody(standardRequest.body, standardRequest.headers, this.toFetchRequestOptions);
62
+ const init = {
63
+ body,
64
+ headers: toFetchHeaders(standardHeaders),
65
+ method: standardRequest.method,
66
+ signal: options.signal,
67
+ redirect: "manual"
68
+ };
69
+ if (body instanceof ReadableStream) {
70
+ const duplex = GET_SUPPORTED_DUPLEX_MODE();
71
+ if (duplex !== void 0) {
72
+ init.duplex = duplex;
73
+ }
74
+ }
75
+ const response = await intercept(
76
+ this.fetchInterceptors,
77
+ { ...options, url, path, init },
78
+ ({ url: url2, path: path2, init: init2, ...options2 }) => this.fetch(url2, init2, options2, path2)
33
79
  );
34
- const lazyResponse = toStandardLazyResponse(fetchResponse, { signal: request.signal });
35
- return lazyResponse;
80
+ const standardResponse = toStandardLazyResponse(response);
81
+ return standardResponse;
36
82
  }
37
83
  }
38
84
 
39
- class RPCLink extends StandardRPCLink {
85
+ class RPCLink extends StandardLink {
40
86
  constructor(options) {
41
- const linkClient = new LinkFetchClient(options);
42
- super(linkClient, options);
87
+ const codec = new RPCLinkCodec(options);
88
+ const transport = new FetchLinkTransport(options);
89
+ super(codec, transport, options);
43
90
  }
44
91
  }
45
92
 
46
- export { LinkFetchClient, RPCLink };
93
+ export { CompositeFetchLinkTransportPlugin, FetchLinkTransport, RPCLink };
@@ -1,9 +1,10 @@
1
+ import { C as ClientContext, a as ClientOptions } from '../../shared/client.BBZBQID8.mjs';
1
2
  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.i2uoJbEp.mjs';
5
- import { f as StandardLinkClient } from '../../shared/client.CpCa3si8.mjs';
6
- import { f as StandardRPCLinkOptions, g as StandardRPCLink } from '../../shared/client.BFAVy68H.mjs';
3
+ import { StandardRequest, StandardLazyResponse } from '@standardserver/core';
4
+ import { ClientPeer, EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standardserver/peer';
5
+ import { S as StandardLinkTransport, a as StandardLink, b as StandardLinkOptions } from '../../shared/client.8f4DNmdE.mjs';
6
+ import { RPCLinkCodecOptions } from '../standard/index.mjs';
7
+ import '../../shared/client.BdItY5DT.mjs';
7
8
 
8
9
  /**
9
10
  * The message port used by electron in main process
@@ -28,53 +29,47 @@ interface BrowserPortLike {
28
29
  }
29
30
  type SupportedMessagePort = Pick<MessagePort, 'addEventListener' | 'postMessage'> | MessagePortMainLike | BrowserPortLike;
30
31
  /**
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
+ * 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
  */
33
34
  type SupportedMessagePortData = any;
34
35
  declare function postMessagePortMessage(port: SupportedMessagePort, data: SupportedMessagePortData, transfer?: any[]): void;
35
36
  declare function onMessagePortMessage(port: SupportedMessagePort, callback: (data: SupportedMessagePortData) => void): void;
36
37
  declare function onMessagePortClose(port: SupportedMessagePort, callback: () => void): void;
37
38
 
38
- interface LinkMessagePortClientOptions {
39
+ type DecodedRequestMessage = ConstructorParameters<typeof ClientPeer>[0] extends (message: infer TMessage) => unknown ? TMessage : never;
40
+ interface MessagePortLinkTransportOptions<_T extends ClientContext> {
39
41
  port: SupportedMessagePort;
40
42
  /**
41
43
  * 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),
44
+ * If needed, define this option to utilize full power of [MessagePort: postMessage() method](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/postMessage),
43
45
  * such as transferring ownership of objects to the other side or support unserializable objects like `OffscreenCanvas`.
44
46
  *
45
47
  * @remarks
46
48
  * - return null | undefined to disable this feature
47
49
  *
48
50
  * @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.dev/docs/adapters/message-port#transfer Message Port Transfer Docs}
58
51
  */
59
52
  experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedRequestMessage, port: SupportedMessagePort]>;
53
+ /**
54
+ * Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
55
+ */
56
+ encodePeerMessage?: EncodePeerMessageOptions | undefined;
57
+ /**
58
+ * Options for decoding peer messages, such as `prefix` for distinguishing messages on the same channel.
59
+ */
60
+ decodePeerMessage?: DecodePeerMessageOptions | undefined;
60
61
  }
61
- declare class LinkMessagePortClient<T extends ClientContext> implements StandardLinkClient<T> {
62
+ declare class MessagePortLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
62
63
  private readonly peer;
63
- constructor(options: LinkMessagePortClientOptions);
64
- call(request: StandardRequest, _options: ClientOptions<T>, _path: readonly string[], _input: unknown): Promise<StandardLazyResponse>;
64
+ constructor({ port, experimental_transfer, encodePeerMessage: encodePeerMessageOptions, decodePeerMessage: decodePeerMessageOptions }: MessagePortLinkTransportOptions<T>);
65
+ send(standardRequest: StandardRequest, _path: string[], _options: ClientOptions<T>): Promise<StandardLazyResponse>;
65
66
  }
66
67
 
67
- interface RPCLinkOptions<T extends ClientContext> extends Omit<StandardRPCLinkOptions<T>, 'url' | 'method' | 'fallbackMethod' | 'maxUrlLength'>, LinkMessagePortClientOptions {
68
+ interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, MessagePortLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
68
69
  }
69
- /**
70
- * The RPC Link for common message port implementations.
71
- *
72
- * @see {@link https://orpc.dev/docs/client/rpc-link RPC Link Docs}
73
- * @see {@link https://orpc.dev/docs/adapters/message-port Message Port Adapter Docs}
74
- */
75
- declare class RPCLink<T extends ClientContext> extends StandardRPCLink<T> {
70
+ declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
76
71
  constructor(options: RPCLinkOptions<T>);
77
72
  }
78
73
 
79
- export { LinkMessagePortClient, RPCLink, onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
80
- export type { BrowserPortLike, LinkMessagePortClientOptions, MessagePortMainLike, RPCLinkOptions, SupportedMessagePort, SupportedMessagePortData };
74
+ export { MessagePortLinkTransport, RPCLink, onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
75
+ export type { BrowserPortLike, MessagePortLinkTransportOptions, MessagePortMainLike, RPCLinkOptions, SupportedMessagePort, SupportedMessagePortData };