@orpc/standard-server-fetch 0.43.0 → 0.44.0

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 ADDED
@@ -0,0 +1,68 @@
1
+ <div align="center">
2
+ <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
3
+ </div>
4
+
5
+ <h1></h1>
6
+
7
+ <div align="center">
8
+ <a href="https://codecov.io/gh/unnoq/orpc">
9
+ <img alt="codecov" src="https://codecov.io/gh/unnoq/orpc/branch/main/graph/badge.svg">
10
+ </a>
11
+ <a href="https://www.npmjs.com/package/@orpc/standard-server-fetch">
12
+ <img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fstandard-server-fetch?logo=npm" />
13
+ </a>
14
+ <a href="https://github.com/unnoq/orpc/blob/main/LICENSE">
15
+ <img alt="MIT License" src="https://img.shields.io/github/license/unnoq/orpc?logo=open-source-initiative" />
16
+ </a>
17
+ <a href="https://discord.gg/TXEbwRBvQn">
18
+ <img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
19
+ </a>
20
+ </div>
21
+
22
+ <h3 align="center">Typesafe APIs Made Simple 🪄</h3>
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.
25
+
26
+ ---
27
+
28
+ ## Highlights
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.
46
+
47
+ ## Documentation
48
+
49
+ You can find the full documentation [here](https://orpc.unnoq.com).
50
+
51
+ ## Packages
52
+
53
+ - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
54
+ - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
55
+ - [@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
+ - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
60
+ - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
61
+
62
+ ## `@orpc/standard-server-fetch`
63
+
64
+ [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) server adapter for oRPC.
65
+
66
+ ## License
67
+
68
+ Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
@@ -0,0 +1,52 @@
1
+ import { StandardBody, StandardHeaders, StandardRequest, StandardResponse } from '@orpc/standard-server';
2
+
3
+ declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
4
+ interface ToEventStreamOptions {
5
+ /**
6
+ * If true, a ping comment is sent periodically to keep the connection alive.
7
+ *
8
+ * @default true
9
+ */
10
+ eventSourcePingEnabled?: boolean;
11
+ /**
12
+ * Interval (in milliseconds) between ping comments sent after the last event.
13
+ *
14
+ * @default 5000
15
+ */
16
+ eventSourcePingInterval?: number;
17
+ /**
18
+ * The content of the ping comment. Must not include newline characters.
19
+ *
20
+ * @default ''
21
+ */
22
+ eventSourcePingContent?: string;
23
+ }
24
+ declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
25
+
26
+ declare function toStandardBody(re: Request | Response): Promise<StandardBody>;
27
+ interface ToFetchBodyOptions extends ToEventStreamOptions {
28
+ }
29
+ /**
30
+ * @param body
31
+ * @param headers - The headers can be changed by the function and effects on the original headers.
32
+ */
33
+ declare function toFetchBody(body: StandardBody, headers: Headers, options?: ToFetchBodyOptions): string | Blob | FormData | URLSearchParams | undefined | ReadableStream<Uint8Array>;
34
+
35
+ /**
36
+ * @param headers
37
+ * @param standardHeaders - The base headers can be changed by the function and effects on the original headers.
38
+ */
39
+ declare function toStandardHeaders(headers: Headers, standardHeaders?: StandardHeaders): StandardHeaders;
40
+ /**
41
+ * @param headers
42
+ * @param fetchHeaders - The base headers can be changed by the function and effects on the original headers.
43
+ */
44
+ declare function toFetchHeaders(headers: StandardHeaders, fetchHeaders?: Headers): Headers;
45
+
46
+ declare function toStandardRequest(request: Request): StandardRequest;
47
+
48
+ interface ToFetchResponseOptions extends ToFetchBodyOptions {
49
+ }
50
+ declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
51
+
52
+ export { type ToEventStreamOptions, type ToFetchBodyOptions, type ToFetchResponseOptions, toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchResponse, toStandardBody, toStandardHeaders, toStandardRequest };
@@ -0,0 +1,52 @@
1
+ import { StandardBody, StandardHeaders, StandardRequest, StandardResponse } from '@orpc/standard-server';
2
+
3
+ declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
4
+ interface ToEventStreamOptions {
5
+ /**
6
+ * If true, a ping comment is sent periodically to keep the connection alive.
7
+ *
8
+ * @default true
9
+ */
10
+ eventSourcePingEnabled?: boolean;
11
+ /**
12
+ * Interval (in milliseconds) between ping comments sent after the last event.
13
+ *
14
+ * @default 5000
15
+ */
16
+ eventSourcePingInterval?: number;
17
+ /**
18
+ * The content of the ping comment. Must not include newline characters.
19
+ *
20
+ * @default ''
21
+ */
22
+ eventSourcePingContent?: string;
23
+ }
24
+ declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
25
+
26
+ declare function toStandardBody(re: Request | Response): Promise<StandardBody>;
27
+ interface ToFetchBodyOptions extends ToEventStreamOptions {
28
+ }
29
+ /**
30
+ * @param body
31
+ * @param headers - The headers can be changed by the function and effects on the original headers.
32
+ */
33
+ declare function toFetchBody(body: StandardBody, headers: Headers, options?: ToFetchBodyOptions): string | Blob | FormData | URLSearchParams | undefined | ReadableStream<Uint8Array>;
34
+
35
+ /**
36
+ * @param headers
37
+ * @param standardHeaders - The base headers can be changed by the function and effects on the original headers.
38
+ */
39
+ declare function toStandardHeaders(headers: Headers, standardHeaders?: StandardHeaders): StandardHeaders;
40
+ /**
41
+ * @param headers
42
+ * @param fetchHeaders - The base headers can be changed by the function and effects on the original headers.
43
+ */
44
+ declare function toFetchHeaders(headers: StandardHeaders, fetchHeaders?: Headers): Headers;
45
+
46
+ declare function toStandardRequest(request: Request): StandardRequest;
47
+
48
+ interface ToFetchResponseOptions extends ToFetchBodyOptions {
49
+ }
50
+ declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
51
+
52
+ export { type ToEventStreamOptions, type ToFetchBodyOptions, type ToFetchResponseOptions, toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchResponse, toStandardBody, toStandardHeaders, toStandardRequest };
@@ -1,17 +1,6 @@
1
- // src/body.ts
2
- import { contentDisposition, isAsyncIteratorObject, parseContentDisposition, parseEmptyableJSON as parseEmptyableJSON2 } from "@orpc/standard-server";
1
+ import { stringifyJSON, parseEmptyableJSON, isTypescriptObject, isAsyncIteratorObject, once } from '@orpc/shared';
2
+ import { EventDecoderStream, encodeEventMessage, getEventMeta, ErrorEvent, withEventMeta, parseContentDisposition, contentDisposition } from '@orpc/standard-server';
3
3
 
4
- // src/event-source.ts
5
- import {
6
- encodeEventMessage,
7
- ErrorEvent,
8
- EventDecoderStream,
9
- getEventMeta,
10
- isEventMetaContainer,
11
- parseEmptyableJSON,
12
- UnknownEvent,
13
- withEventMeta
14
- } from "@orpc/standard-server";
15
4
  function toEventIterator(stream) {
16
5
  const eventStream = stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
17
6
  const reader = eventStream.getReader();
@@ -25,7 +14,7 @@ function toEventIterator(stream) {
25
14
  switch (value.event) {
26
15
  case "message": {
27
16
  let message = parseEmptyableJSON(value.data);
28
- if (isEventMetaContainer(message)) {
17
+ if (isTypescriptObject(message)) {
29
18
  message = withEventMeta(message, value);
30
19
  }
31
20
  yield message;
@@ -40,19 +29,11 @@ function toEventIterator(stream) {
40
29
  }
41
30
  case "done": {
42
31
  let done2 = parseEmptyableJSON(value.data);
43
- if (isEventMetaContainer(done2)) {
32
+ if (isTypescriptObject(done2)) {
44
33
  done2 = withEventMeta(done2, value);
45
34
  }
46
35
  return done2;
47
36
  }
48
- default: {
49
- let error = new UnknownEvent({
50
- message: `Unknown event: ${value.event}`,
51
- data: parseEmptyableJSON(value.data)
52
- });
53
- error = withEventMeta(error, value);
54
- throw error;
55
- }
56
37
  }
57
38
  }
58
39
  } finally {
@@ -61,24 +42,37 @@ function toEventIterator(stream) {
61
42
  }
62
43
  return gen();
63
44
  }
64
- function toEventStream(iterator) {
45
+ function toEventStream(iterator, options = {}) {
46
+ const pingEnabled = options.eventSourcePingEnabled ?? true;
47
+ const pingInterval = options.eventSourcePingInterval ?? 5e3;
48
+ const pingContent = options.eventSourcePingContent ?? "";
49
+ let timeout;
65
50
  const stream = new ReadableStream({
66
51
  async pull(controller) {
67
52
  try {
53
+ if (pingEnabled) {
54
+ timeout = setInterval(() => {
55
+ controller.enqueue(encodeEventMessage({
56
+ comments: [pingContent]
57
+ }));
58
+ }, pingInterval);
59
+ }
68
60
  const value = await iterator.next();
61
+ clearInterval(timeout);
69
62
  controller.enqueue(encodeEventMessage({
70
63
  ...getEventMeta(value.value),
71
64
  event: value.done ? "done" : "message",
72
- data: JSON.stringify(value.value)
65
+ data: stringifyJSON(value.value)
73
66
  }));
74
67
  if (value.done) {
75
68
  controller.close();
76
69
  }
77
70
  } catch (err) {
71
+ clearInterval(timeout);
78
72
  controller.enqueue(encodeEventMessage({
79
73
  ...getEventMeta(err),
80
74
  event: "error",
81
- data: err instanceof ErrorEvent ? JSON.stringify(err.data) : void 0
75
+ data: err instanceof ErrorEvent ? stringifyJSON(err.data) : void 0
82
76
  }));
83
77
  controller.close();
84
78
  }
@@ -94,7 +88,6 @@ function toEventStream(iterator) {
94
88
  return stream;
95
89
  }
96
90
 
97
- // src/body.ts
98
91
  async function toStandardBody(re) {
99
92
  if (!re.body) {
100
93
  return void 0;
@@ -112,7 +105,7 @@ async function toStandardBody(re) {
112
105
  const contentType = re.headers.get("content-type");
113
106
  if (!contentType || contentType.startsWith("application/json")) {
114
107
  const text = await re.text();
115
- return parseEmptyableJSON2(text);
108
+ return parseEmptyableJSON(text);
116
109
  }
117
110
  if (contentType.startsWith("multipart/form-data")) {
118
111
  return await re.formData();
@@ -124,7 +117,7 @@ async function toStandardBody(re) {
124
117
  if (contentType.startsWith("text/event-stream")) {
125
118
  return toEventIterator(re.body);
126
119
  }
127
- if (contentType.startsWith("text/")) {
120
+ if (contentType.startsWith("text/plain")) {
128
121
  return await re.text();
129
122
  }
130
123
  const blob = await re.blob();
@@ -132,7 +125,7 @@ async function toStandardBody(re) {
132
125
  type: blob.type
133
126
  });
134
127
  }
135
- function toFetchBody(body, headers) {
128
+ function toFetchBody(body, headers, options = {}) {
136
129
  headers.delete("content-type");
137
130
  headers.delete("content-disposition");
138
131
  if (body === void 0) {
@@ -157,13 +150,12 @@ function toFetchBody(body, headers) {
157
150
  headers.set("content-type", "text/event-stream");
158
151
  headers.set("cache-control", "no-cache");
159
152
  headers.set("connection", "keep-alive");
160
- return toEventStream(body);
153
+ return toEventStream(body, options);
161
154
  }
162
155
  headers.set("content-type", "application/json");
163
- return JSON.stringify(body);
156
+ return stringifyJSON(body);
164
157
  }
165
158
 
166
- // src/headers.ts
167
159
  function toStandardHeaders(headers, standardHeaders = {}) {
168
160
  for (const [key, value] of headers) {
169
161
  if (Array.isArray(standardHeaders[key])) {
@@ -189,8 +181,6 @@ function toFetchHeaders(headers, fetchHeaders = new Headers()) {
189
181
  return fetchHeaders;
190
182
  }
191
183
 
192
- // src/request.ts
193
- import { once } from "@orpc/standard-server";
194
184
  function toStandardRequest(request) {
195
185
  return {
196
186
  raw: { request },
@@ -209,20 +199,10 @@ function toStandardRequest(request) {
209
199
  };
210
200
  }
211
201
 
212
- // src/response.ts
213
- function toFetchResponse(response) {
202
+ function toFetchResponse(response, options = {}) {
214
203
  const headers = toFetchHeaders(response.headers);
215
- const body = toFetchBody(response.body, headers);
204
+ const body = toFetchBody(response.body, headers, options);
216
205
  return new Response(body, { headers, status: response.status });
217
206
  }
218
- export {
219
- toEventIterator,
220
- toEventStream,
221
- toFetchBody,
222
- toFetchHeaders,
223
- toFetchResponse,
224
- toStandardBody,
225
- toStandardHeaders,
226
- toStandardRequest
227
- };
228
- //# sourceMappingURL=index.js.map
207
+
208
+ export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchResponse, toStandardBody, toStandardHeaders, toStandardRequest };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/standard-server-fetch",
3
3
  "type": "module",
4
- "version": "0.43.0",
4
+ "version": "0.44.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://unnoq.com",
7
7
  "repository": {
@@ -14,27 +14,23 @@
14
14
  ],
15
15
  "exports": {
16
16
  ".": {
17
- "types": "./dist/src/index.d.ts",
18
- "import": "./dist/index.js",
19
- "default": "./dist/index.js"
20
- },
21
- "./🔒/*": {
22
- "types": "./dist/src/*.d.ts"
17
+ "types": "./dist/index.d.mts",
18
+ "import": "./dist/index.mjs",
19
+ "default": "./dist/index.mjs"
23
20
  }
24
21
  },
25
22
  "files": [
26
- "!**/*.map",
27
- "!**/*.tsbuildinfo",
28
23
  "dist"
29
24
  ],
30
25
  "dependencies": {
31
- "@orpc/standard-server": "0.43.0"
26
+ "@orpc/shared": "0.44.0",
27
+ "@orpc/standard-server": "0.44.0"
32
28
  },
33
29
  "devDependencies": {
34
30
  "@hono/node-server": "^1.13.8"
35
31
  },
36
32
  "scripts": {
37
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
33
+ "build": "unbuild",
38
34
  "build:watch": "pnpm run build --watch",
39
35
  "type:check": "tsc -b"
40
36
  }
@@ -1,8 +0,0 @@
1
- import type { StandardBody } from '@orpc/standard-server';
2
- export declare function toStandardBody(re: Request | Response): Promise<StandardBody>;
3
- /**
4
- * @param body
5
- * @param headers - The headers can be changed by the function and effects on the original headers.
6
- */
7
- export declare function toFetchBody(body: StandardBody, headers: Headers): string | Blob | FormData | URLSearchParams | undefined | ReadableStream<Uint8Array>;
8
- //# sourceMappingURL=body.d.ts.map
@@ -1,4 +0,0 @@
1
- import type { JsonValue } from '@orpc/standard-server';
2
- export declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<JsonValue | void, JsonValue | void, void>;
3
- export declare function toEventStream(iterator: AsyncIterator<JsonValue | void, JsonValue | void, void>): ReadableStream<Uint8Array>;
4
- //# sourceMappingURL=event-source.d.ts.map
@@ -1,12 +0,0 @@
1
- import type { StandardHeaders } from '@orpc/standard-server';
2
- /**
3
- * @param headers
4
- * @param standardHeaders - The base headers can be changed by the function and effects on the original headers.
5
- */
6
- export declare function toStandardHeaders(headers: Headers, standardHeaders?: StandardHeaders): StandardHeaders;
7
- /**
8
- * @param headers
9
- * @param fetchHeaders - The base headers can be changed by the function and effects on the original headers.
10
- */
11
- export declare function toFetchHeaders(headers: StandardHeaders, fetchHeaders?: Headers): Headers;
12
- //# sourceMappingURL=headers.d.ts.map
@@ -1,6 +0,0 @@
1
- export * from './body';
2
- export * from './event-source';
3
- export * from './headers';
4
- export * from './request';
5
- export * from './response';
6
- //# sourceMappingURL=index.d.ts.map
@@ -1,3 +0,0 @@
1
- import type { StandardRequest } from '@orpc/standard-server';
2
- export declare function toStandardRequest(request: Request): StandardRequest;
3
- //# sourceMappingURL=request.d.ts.map
@@ -1,3 +0,0 @@
1
- import type { StandardResponse } from '@orpc/standard-server';
2
- export declare function toFetchResponse(response: StandardResponse): Response;
3
- //# sourceMappingURL=response.d.ts.map