@orpc/standard-server-fetch 0.0.0-next.e7ee5a9 → 0.0.0-next.e8524dc

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 CHANGED
@@ -21,28 +21,24 @@
21
21
 
22
22
  <h3 align="center">Typesafe APIs Made Simple 🪄</h3>
23
23
 
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.
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
25
25
 
26
26
  ---
27
27
 
28
28
  ## Highlights
29
29
 
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), 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.
30
+ - **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
31
+ - **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
32
+ - **📝 Contract-First Development**: Optionally define your API contract before implementation.
33
+ - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), Pinia Colada, and more.
34
+ - **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
35
+ - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
36
+ - **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
37
+ - **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
38
+ - **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
39
+ - **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
40
+ - **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
41
+ - **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
46
42
 
47
43
  ## Documentation
48
44
 
@@ -53,16 +49,28 @@ You can find the full documentation [here](https://orpc.unnoq.com).
53
49
  - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
54
50
  - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
55
51
  - [@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/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
59
52
  - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
53
+ - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
54
+ - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
55
+ - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
56
+ - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
57
+ - [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
60
58
  - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
59
+ - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
60
+ - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
61
61
 
62
62
  ## `@orpc/standard-server-fetch`
63
63
 
64
64
  [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) server adapter for oRPC.
65
65
 
66
+ ## Sponsors
67
+
68
+ <p align="center">
69
+ <a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
70
+ <img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
71
+ </a>
72
+ </p>
73
+
66
74
  ## License
67
75
 
68
76
  Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
package/dist/index.d.mts CHANGED
@@ -1,25 +1,26 @@
1
- import { StandardBody, StandardHeaders, StandardRequest, StandardResponse } from '@orpc/standard-server';
1
+ import { StandardBody, StandardHeaders, StandardLazyRequest, StandardRequest, StandardResponse, StandardLazyResponse } from '@orpc/standard-server';
2
+ import { AsyncIteratorClass } from '@orpc/shared';
2
3
 
3
- declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
4
+ declare function toEventIterator(stream: ReadableStream<Uint8Array> | null): AsyncIteratorClass<unknown>;
4
5
  interface ToEventStreamOptions {
5
6
  /**
6
7
  * If true, a ping comment is sent periodically to keep the connection alive.
7
8
  *
8
9
  * @default true
9
10
  */
10
- eventSourcePingEnabled?: boolean;
11
+ eventIteratorKeepAliveEnabled?: boolean;
11
12
  /**
12
13
  * Interval (in milliseconds) between ping comments sent after the last event.
13
14
  *
14
15
  * @default 5000
15
16
  */
16
- eventSourcePingInterval?: number;
17
+ eventIteratorKeepAliveInterval?: number;
17
18
  /**
18
19
  * The content of the ping comment. Must not include newline characters.
19
20
  *
20
21
  * @default ''
21
22
  */
22
- eventSourcePingContent?: string;
23
+ eventIteratorKeepAliveComment?: string;
23
24
  }
24
25
  declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
25
26
 
@@ -43,10 +44,15 @@ declare function toStandardHeaders(headers: Headers, standardHeaders?: StandardH
43
44
  */
44
45
  declare function toFetchHeaders(headers: StandardHeaders, fetchHeaders?: Headers): Headers;
45
46
 
46
- declare function toStandardRequest(request: Request): StandardRequest;
47
+ declare function toStandardLazyRequest(request: Request): StandardLazyRequest;
48
+ interface ToFetchRequestOptions extends ToFetchBodyOptions {
49
+ }
50
+ declare function toFetchRequest(request: StandardRequest, options?: ToFetchRequestOptions): Request;
47
51
 
48
52
  interface ToFetchResponseOptions extends ToFetchBodyOptions {
49
53
  }
50
54
  declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
55
+ declare function toStandardLazyResponse(response: Response): StandardLazyResponse;
51
56
 
52
- export { type ToEventStreamOptions, type ToFetchBodyOptions, type ToFetchResponseOptions, toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchResponse, toStandardBody, toStandardHeaders, toStandardRequest };
57
+ export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchRequest, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse };
58
+ export type { ToEventStreamOptions, ToFetchBodyOptions, ToFetchRequestOptions, ToFetchResponseOptions };
package/dist/index.d.ts CHANGED
@@ -1,25 +1,26 @@
1
- import { StandardBody, StandardHeaders, StandardRequest, StandardResponse } from '@orpc/standard-server';
1
+ import { StandardBody, StandardHeaders, StandardLazyRequest, StandardRequest, StandardResponse, StandardLazyResponse } from '@orpc/standard-server';
2
+ import { AsyncIteratorClass } from '@orpc/shared';
2
3
 
3
- declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
4
+ declare function toEventIterator(stream: ReadableStream<Uint8Array> | null): AsyncIteratorClass<unknown>;
4
5
  interface ToEventStreamOptions {
5
6
  /**
6
7
  * If true, a ping comment is sent periodically to keep the connection alive.
7
8
  *
8
9
  * @default true
9
10
  */
10
- eventSourcePingEnabled?: boolean;
11
+ eventIteratorKeepAliveEnabled?: boolean;
11
12
  /**
12
13
  * Interval (in milliseconds) between ping comments sent after the last event.
13
14
  *
14
15
  * @default 5000
15
16
  */
16
- eventSourcePingInterval?: number;
17
+ eventIteratorKeepAliveInterval?: number;
17
18
  /**
18
19
  * The content of the ping comment. Must not include newline characters.
19
20
  *
20
21
  * @default ''
21
22
  */
22
- eventSourcePingContent?: string;
23
+ eventIteratorKeepAliveComment?: string;
23
24
  }
24
25
  declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
25
26
 
@@ -43,10 +44,15 @@ declare function toStandardHeaders(headers: Headers, standardHeaders?: StandardH
43
44
  */
44
45
  declare function toFetchHeaders(headers: StandardHeaders, fetchHeaders?: Headers): Headers;
45
46
 
46
- declare function toStandardRequest(request: Request): StandardRequest;
47
+ declare function toStandardLazyRequest(request: Request): StandardLazyRequest;
48
+ interface ToFetchRequestOptions extends ToFetchBodyOptions {
49
+ }
50
+ declare function toFetchRequest(request: StandardRequest, options?: ToFetchRequestOptions): Request;
47
51
 
48
52
  interface ToFetchResponseOptions extends ToFetchBodyOptions {
49
53
  }
50
54
  declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
55
+ declare function toStandardLazyResponse(response: Response): StandardLazyResponse;
51
56
 
52
- export { type ToEventStreamOptions, type ToFetchBodyOptions, type ToFetchResponseOptions, toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchResponse, toStandardBody, toStandardHeaders, toStandardRequest };
57
+ export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchRequest, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse };
58
+ export type { ToEventStreamOptions, ToFetchBodyOptions, ToFetchRequestOptions, ToFetchResponseOptions };
package/dist/index.mjs CHANGED
@@ -1,74 +1,83 @@
1
- import { stringifyJSON, parseEmptyableJSON, isTypescriptObject, isAsyncIteratorObject, once } from '@orpc/shared';
2
- import { EventDecoderStream, encodeEventMessage, getEventMeta, ErrorEvent, withEventMeta, parseContentDisposition, contentDisposition } from '@orpc/standard-server';
1
+ import { AsyncIteratorClass, parseEmptyableJSON, isTypescriptObject, stringifyJSON, isAsyncIteratorObject, once } from '@orpc/shared';
2
+ import { EventDecoderStream, withEventMeta, ErrorEvent, encodeEventMessage, getEventMeta, getFilenameFromContentDisposition, generateContentDisposition } from '@orpc/standard-server';
3
3
 
4
4
  function toEventIterator(stream) {
5
- const eventStream = stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
6
- const reader = eventStream.getReader();
7
- async function* gen() {
8
- try {
9
- while (true) {
10
- const { done, value } = await reader.read();
11
- if (done) {
12
- return;
13
- }
14
- switch (value.event) {
15
- case "message": {
16
- let message = parseEmptyableJSON(value.data);
17
- if (isTypescriptObject(message)) {
18
- message = withEventMeta(message, value);
19
- }
20
- yield message;
21
- break;
22
- }
23
- case "error": {
24
- let error = new ErrorEvent({
25
- data: parseEmptyableJSON(value.data)
26
- });
27
- error = withEventMeta(error, value);
28
- throw error;
5
+ const eventStream = stream?.pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
6
+ const reader = eventStream?.getReader();
7
+ return new AsyncIteratorClass(async () => {
8
+ while (true) {
9
+ if (reader === void 0) {
10
+ return { done: true, value: void 0 };
11
+ }
12
+ const { done, value } = await reader.read();
13
+ if (done) {
14
+ return { done: true, value: void 0 };
15
+ }
16
+ switch (value.event) {
17
+ case "message": {
18
+ let message = parseEmptyableJSON(value.data);
19
+ if (isTypescriptObject(message)) {
20
+ message = withEventMeta(message, value);
29
21
  }
30
- case "done": {
31
- let done2 = parseEmptyableJSON(value.data);
32
- if (isTypescriptObject(done2)) {
33
- done2 = withEventMeta(done2, value);
34
- }
35
- return done2;
22
+ return { done: false, value: message };
23
+ }
24
+ case "error": {
25
+ let error = new ErrorEvent({
26
+ data: parseEmptyableJSON(value.data)
27
+ });
28
+ error = withEventMeta(error, value);
29
+ throw error;
30
+ }
31
+ case "done": {
32
+ let done2 = parseEmptyableJSON(value.data);
33
+ if (isTypescriptObject(done2)) {
34
+ done2 = withEventMeta(done2, value);
36
35
  }
36
+ return { done: true, value: done2 };
37
37
  }
38
38
  }
39
- } finally {
40
- await reader.cancel();
41
39
  }
42
- }
43
- return gen();
40
+ }, async () => {
41
+ await reader?.cancel();
42
+ });
44
43
  }
45
44
  function toEventStream(iterator, options = {}) {
46
- const pingEnabled = options.eventSourcePingEnabled ?? true;
47
- const pingInterval = options.eventSourcePingInterval ?? 5e3;
48
- const pingContent = options.eventSourcePingContent ?? "";
45
+ const keepAliveEnabled = options.eventIteratorKeepAliveEnabled ?? true;
46
+ const keepAliveInterval = options.eventIteratorKeepAliveInterval ?? 5e3;
47
+ const keepAliveComment = options.eventIteratorKeepAliveComment ?? "";
48
+ let cancelled = false;
49
49
  let timeout;
50
50
  const stream = new ReadableStream({
51
51
  async pull(controller) {
52
52
  try {
53
- if (pingEnabled) {
53
+ if (keepAliveEnabled) {
54
54
  timeout = setInterval(() => {
55
55
  controller.enqueue(encodeEventMessage({
56
- comments: [pingContent]
56
+ comments: [keepAliveComment]
57
57
  }));
58
- }, pingInterval);
58
+ }, keepAliveInterval);
59
59
  }
60
60
  const value = await iterator.next();
61
61
  clearInterval(timeout);
62
- controller.enqueue(encodeEventMessage({
63
- ...getEventMeta(value.value),
64
- event: value.done ? "done" : "message",
65
- data: stringifyJSON(value.value)
66
- }));
62
+ if (cancelled) {
63
+ return;
64
+ }
65
+ const meta = getEventMeta(value.value);
66
+ if (!value.done || value.value !== void 0 || meta !== void 0) {
67
+ controller.enqueue(encodeEventMessage({
68
+ ...meta,
69
+ event: value.done ? "done" : "message",
70
+ data: stringifyJSON(value.value)
71
+ }));
72
+ }
67
73
  if (value.done) {
68
74
  controller.close();
69
75
  }
70
76
  } catch (err) {
71
77
  clearInterval(timeout);
78
+ if (cancelled) {
79
+ return;
80
+ }
72
81
  controller.enqueue(encodeEventMessage({
73
82
  ...getEventMeta(err),
74
83
  event: "error",
@@ -77,30 +86,23 @@ function toEventStream(iterator, options = {}) {
77
86
  controller.close();
78
87
  }
79
88
  },
80
- async cancel(reason) {
81
- if (reason) {
82
- await iterator.throw?.(reason);
83
- } else {
84
- await iterator.return?.();
85
- }
89
+ async cancel() {
90
+ cancelled = true;
91
+ clearInterval(timeout);
92
+ await iterator.return?.();
86
93
  }
87
94
  }).pipeThrough(new TextEncoderStream());
88
95
  return stream;
89
96
  }
90
97
 
91
98
  async function toStandardBody(re) {
92
- if (!re.body) {
93
- return void 0;
94
- }
95
- const contentDisposition2 = re.headers.get("content-disposition");
96
- if (contentDisposition2) {
97
- const fileName = parseContentDisposition(contentDisposition2).parameters.filename;
98
- if (typeof fileName === "string") {
99
- const blob2 = await re.blob();
100
- return new File([blob2], fileName, {
101
- type: blob2.type
102
- });
103
- }
99
+ const contentDisposition = re.headers.get("content-disposition");
100
+ if (typeof contentDisposition === "string") {
101
+ const fileName = getFilenameFromContentDisposition(contentDisposition) ?? "blob";
102
+ const blob2 = await re.blob();
103
+ return new File([blob2], fileName, {
104
+ type: blob2.type
105
+ });
104
106
  }
105
107
  const contentType = re.headers.get("content-type");
106
108
  if (!contentType || contentType.startsWith("application/json")) {
@@ -126,6 +128,7 @@ async function toStandardBody(re) {
126
128
  });
127
129
  }
128
130
  function toFetchBody(body, headers, options = {}) {
131
+ const currentContentDisposition = headers.get("content-disposition");
129
132
  headers.delete("content-type");
130
133
  headers.delete("content-disposition");
131
134
  if (body === void 0) {
@@ -136,7 +139,7 @@ function toFetchBody(body, headers, options = {}) {
136
139
  headers.set("content-length", body.size.toString());
137
140
  headers.set(
138
141
  "content-disposition",
139
- contentDisposition(body instanceof File ? body.name : "blob", { type: "inline" })
142
+ currentContentDisposition ?? generateContentDisposition(body instanceof File ? body.name : "blob")
140
143
  );
141
144
  return body;
142
145
  }
@@ -148,8 +151,6 @@ function toFetchBody(body, headers, options = {}) {
148
151
  }
149
152
  if (isAsyncIteratorObject(body)) {
150
153
  headers.set("content-type", "text/event-stream");
151
- headers.set("cache-control", "no-cache");
152
- headers.set("connection", "keep-alive");
153
154
  return toEventStream(body, options);
154
155
  }
155
156
  headers.set("content-type", "application/json");
@@ -181,9 +182,8 @@ function toFetchHeaders(headers, fetchHeaders = new Headers()) {
181
182
  return fetchHeaders;
182
183
  }
183
184
 
184
- function toStandardRequest(request) {
185
+ function toStandardLazyRequest(request) {
185
186
  return {
186
- raw: { request },
187
187
  url: new URL(request.url),
188
188
  signal: request.signal,
189
189
  method: request.method,
@@ -198,11 +198,35 @@ function toStandardRequest(request) {
198
198
  }
199
199
  };
200
200
  }
201
+ function toFetchRequest(request, options = {}) {
202
+ const headers = toFetchHeaders(request.headers);
203
+ const body = toFetchBody(request.body, headers, options);
204
+ return new Request(request.url, {
205
+ signal: request.signal,
206
+ method: request.method,
207
+ headers,
208
+ body
209
+ });
210
+ }
201
211
 
202
212
  function toFetchResponse(response, options = {}) {
203
213
  const headers = toFetchHeaders(response.headers);
204
214
  const body = toFetchBody(response.body, headers, options);
205
215
  return new Response(body, { headers, status: response.status });
206
216
  }
217
+ function toStandardLazyResponse(response) {
218
+ return {
219
+ body: once(() => toStandardBody(response)),
220
+ status: response.status,
221
+ get headers() {
222
+ const headers = toStandardHeaders(response.headers);
223
+ Object.defineProperty(this, "headers", { value: headers, writable: true });
224
+ return headers;
225
+ },
226
+ set headers(value) {
227
+ Object.defineProperty(this, "headers", { value, writable: true });
228
+ }
229
+ };
230
+ }
207
231
 
208
- export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchResponse, toStandardBody, toStandardHeaders, toStandardRequest };
232
+ export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchRequest, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/standard-server-fetch",
3
3
  "type": "module",
4
- "version": "0.0.0-next.e7ee5a9",
4
+ "version": "0.0.0-next.e8524dc",
5
5
  "license": "MIT",
6
6
  "homepage": "https://unnoq.com",
7
7
  "repository": {
@@ -23,11 +23,11 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@orpc/shared": "0.0.0-next.e7ee5a9",
27
- "@orpc/standard-server": "0.0.0-next.e7ee5a9"
26
+ "@orpc/shared": "0.0.0-next.e8524dc",
27
+ "@orpc/standard-server": "0.0.0-next.e8524dc"
28
28
  },
29
29
  "devDependencies": {
30
- "@hono/node-server": "^1.13.8"
30
+ "@hono/node-server": "^1.14.4"
31
31
  },
32
32
  "scripts": {
33
33
  "build": "unbuild",