@orpc/standard-server-fetch 0.0.0-next.cc4cb21 → 0.0.0-next.d0e429d
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 +68 -0
- package/dist/index.js +23 -19
- package/dist/src/body.d.ts +4 -1
- package/dist/src/event-source.d.ts +21 -1
- package/dist/src/response.d.ts +4 -1
- package/package.json +3 -3
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 />
|
|
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.
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
// src/body.ts
|
|
2
|
-
import { isAsyncIteratorObject, parseEmptyableJSON as parseEmptyableJSON2 } from "@orpc/shared";
|
|
2
|
+
import { isAsyncIteratorObject, parseEmptyableJSON as parseEmptyableJSON2, stringifyJSON as stringifyJSON2 } from "@orpc/shared";
|
|
3
3
|
import { contentDisposition, parseContentDisposition } from "@orpc/standard-server";
|
|
4
4
|
|
|
5
5
|
// src/event-source.ts
|
|
6
|
-
import { isTypescriptObject, parseEmptyableJSON } from "@orpc/shared";
|
|
6
|
+
import { isTypescriptObject, parseEmptyableJSON, stringifyJSON } from "@orpc/shared";
|
|
7
7
|
import {
|
|
8
8
|
encodeEventMessage,
|
|
9
9
|
ErrorEvent,
|
|
10
10
|
EventDecoderStream,
|
|
11
11
|
getEventMeta,
|
|
12
|
-
UnknownEvent,
|
|
13
12
|
withEventMeta
|
|
14
13
|
} from "@orpc/standard-server";
|
|
15
14
|
function toEventIterator(stream) {
|
|
@@ -45,14 +44,6 @@ function toEventIterator(stream) {
|
|
|
45
44
|
}
|
|
46
45
|
return done2;
|
|
47
46
|
}
|
|
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
47
|
}
|
|
57
48
|
}
|
|
58
49
|
} finally {
|
|
@@ -61,24 +52,37 @@ function toEventIterator(stream) {
|
|
|
61
52
|
}
|
|
62
53
|
return gen();
|
|
63
54
|
}
|
|
64
|
-
function toEventStream(iterator) {
|
|
55
|
+
function toEventStream(iterator, options = {}) {
|
|
56
|
+
const pingEnabled = options.eventSourcePingEnabled ?? true;
|
|
57
|
+
const pingInterval = options.eventSourcePingInterval ?? 5e3;
|
|
58
|
+
const pingContent = options.eventSourcePingContent ?? "";
|
|
59
|
+
let timeout;
|
|
65
60
|
const stream = new ReadableStream({
|
|
66
61
|
async pull(controller) {
|
|
67
62
|
try {
|
|
63
|
+
if (pingEnabled) {
|
|
64
|
+
timeout = setInterval(() => {
|
|
65
|
+
controller.enqueue(encodeEventMessage({
|
|
66
|
+
comments: [pingContent]
|
|
67
|
+
}));
|
|
68
|
+
}, pingInterval);
|
|
69
|
+
}
|
|
68
70
|
const value = await iterator.next();
|
|
71
|
+
clearInterval(timeout);
|
|
69
72
|
controller.enqueue(encodeEventMessage({
|
|
70
73
|
...getEventMeta(value.value),
|
|
71
74
|
event: value.done ? "done" : "message",
|
|
72
|
-
data:
|
|
75
|
+
data: stringifyJSON(value.value)
|
|
73
76
|
}));
|
|
74
77
|
if (value.done) {
|
|
75
78
|
controller.close();
|
|
76
79
|
}
|
|
77
80
|
} catch (err) {
|
|
81
|
+
clearInterval(timeout);
|
|
78
82
|
controller.enqueue(encodeEventMessage({
|
|
79
83
|
...getEventMeta(err),
|
|
80
84
|
event: "error",
|
|
81
|
-
data: err instanceof ErrorEvent ?
|
|
85
|
+
data: err instanceof ErrorEvent ? stringifyJSON(err.data) : void 0
|
|
82
86
|
}));
|
|
83
87
|
controller.close();
|
|
84
88
|
}
|
|
@@ -132,7 +136,7 @@ async function toStandardBody(re) {
|
|
|
132
136
|
type: blob.type
|
|
133
137
|
});
|
|
134
138
|
}
|
|
135
|
-
function toFetchBody(body, headers) {
|
|
139
|
+
function toFetchBody(body, headers, options = {}) {
|
|
136
140
|
headers.delete("content-type");
|
|
137
141
|
headers.delete("content-disposition");
|
|
138
142
|
if (body === void 0) {
|
|
@@ -157,10 +161,10 @@ function toFetchBody(body, headers) {
|
|
|
157
161
|
headers.set("content-type", "text/event-stream");
|
|
158
162
|
headers.set("cache-control", "no-cache");
|
|
159
163
|
headers.set("connection", "keep-alive");
|
|
160
|
-
return toEventStream(body);
|
|
164
|
+
return toEventStream(body, options);
|
|
161
165
|
}
|
|
162
166
|
headers.set("content-type", "application/json");
|
|
163
|
-
return
|
|
167
|
+
return stringifyJSON2(body);
|
|
164
168
|
}
|
|
165
169
|
|
|
166
170
|
// src/headers.ts
|
|
@@ -210,9 +214,9 @@ function toStandardRequest(request) {
|
|
|
210
214
|
}
|
|
211
215
|
|
|
212
216
|
// src/response.ts
|
|
213
|
-
function toFetchResponse(response) {
|
|
217
|
+
function toFetchResponse(response, options = {}) {
|
|
214
218
|
const headers = toFetchHeaders(response.headers);
|
|
215
|
-
const body = toFetchBody(response.body, headers);
|
|
219
|
+
const body = toFetchBody(response.body, headers, options);
|
|
216
220
|
return new Response(body, { headers, status: response.status });
|
|
217
221
|
}
|
|
218
222
|
export {
|
package/dist/src/body.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { StandardBody } from '@orpc/standard-server';
|
|
2
|
+
import type { ToEventStreamOptions } from './event-source';
|
|
2
3
|
export declare function toStandardBody(re: Request | Response): Promise<StandardBody>;
|
|
4
|
+
export interface ToFetchBodyOptions extends ToEventStreamOptions {
|
|
5
|
+
}
|
|
3
6
|
/**
|
|
4
7
|
* @param body
|
|
5
8
|
* @param headers - The headers can be changed by the function and effects on the original headers.
|
|
6
9
|
*/
|
|
7
|
-
export declare function toFetchBody(body: StandardBody, headers: Headers): string | Blob | FormData | URLSearchParams | undefined | ReadableStream<Uint8Array>;
|
|
10
|
+
export declare function toFetchBody(body: StandardBody, headers: Headers, options?: ToFetchBodyOptions): string | Blob | FormData | URLSearchParams | undefined | ReadableStream<Uint8Array>;
|
|
8
11
|
//# sourceMappingURL=body.d.ts.map
|
|
@@ -1,3 +1,23 @@
|
|
|
1
1
|
export declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
|
|
2
|
-
export
|
|
2
|
+
export interface ToEventStreamOptions {
|
|
3
|
+
/**
|
|
4
|
+
* If true, a ping comment is sent periodically to keep the connection alive.
|
|
5
|
+
*
|
|
6
|
+
* @default true
|
|
7
|
+
*/
|
|
8
|
+
eventSourcePingEnabled?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Interval (in milliseconds) between ping comments sent after the last event.
|
|
11
|
+
*
|
|
12
|
+
* @default 5000
|
|
13
|
+
*/
|
|
14
|
+
eventSourcePingInterval?: number;
|
|
15
|
+
/**
|
|
16
|
+
* The content of the ping comment. Must not include newline characters.
|
|
17
|
+
*
|
|
18
|
+
* @default ''
|
|
19
|
+
*/
|
|
20
|
+
eventSourcePingContent?: string;
|
|
21
|
+
}
|
|
22
|
+
export declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
|
|
3
23
|
//# sourceMappingURL=event-source.d.ts.map
|
package/dist/src/response.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import type { StandardResponse } from '@orpc/standard-server';
|
|
2
|
-
|
|
2
|
+
import type { ToFetchBodyOptions } from './body';
|
|
3
|
+
export interface ToFetchResponseOptions extends ToFetchBodyOptions {
|
|
4
|
+
}
|
|
5
|
+
export declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
|
|
3
6
|
//# sourceMappingURL=response.d.ts.map
|
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.
|
|
4
|
+
"version": "0.0.0-next.d0e429d",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"dist"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@orpc/shared": "0.0.0-next.
|
|
32
|
-
"@orpc/standard-server": "0.0.0-next.
|
|
31
|
+
"@orpc/shared": "0.0.0-next.d0e429d",
|
|
32
|
+
"@orpc/standard-server": "0.0.0-next.d0e429d"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@hono/node-server": "^1.13.8"
|