@orpc/standard-server 0.0.0-next.ff68fdb → 0.0.0-next.ff7ad2e
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 +30 -22
- package/dist/batch/index.d.mts +30 -0
- package/dist/batch/index.d.ts +30 -0
- package/dist/batch/index.mjs +140 -0
- package/dist/index.d.mts +18 -42
- package/dist/index.d.ts +18 -42
- package/dist/index.mjs +86 -3
- package/dist/shared/standard-server.BD8Sg1A5.d.mts +38 -0
- package/dist/shared/standard-server.BD8Sg1A5.d.ts +38 -0
- package/package.json +7 -3
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
|
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
|
-
-
|
31
|
-
-
|
32
|
-
-
|
33
|
-
-
|
34
|
-
-
|
35
|
-
-
|
36
|
-
-
|
37
|
-
-
|
38
|
-
-
|
39
|
-
-
|
40
|
-
-
|
41
|
-
-
|
42
|
-
- **Reusability 🔄**: Write once and reuse your code across multiple purposes effortlessly.
|
43
|
-
- **Extendability 🔌**: Easily enhance oRPC with plugins, middleware, and interceptors.
|
44
|
-
- **Reliability 🛡️**: Well-tested, fully TypeScript, production-ready, and MIT licensed for peace of mind.
|
45
|
-
- **Simplicity 💡**: Enjoy straightforward, clean code with no hidden magic.
|
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
|
+
- **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
|
34
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
|
35
|
+
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
36
|
+
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
37
|
+
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
38
|
+
- **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
|
39
|
+
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
40
|
+
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
41
|
+
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
46
42
|
|
47
43
|
## Documentation
|
48
44
|
|
@@ -53,18 +49,30 @@ 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/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
|
59
|
-
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
|
60
|
-
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
61
52
|
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
53
|
+
- [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
|
54
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
55
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
56
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
57
|
+
- [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
|
58
|
+
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
59
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
62
60
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
61
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
62
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
63
63
|
|
64
64
|
## `@orpc/standard-server`
|
65
65
|
|
66
66
|
This package is designed to be used as a base for other server implementations.
|
67
67
|
|
68
|
+
## Sponsors
|
69
|
+
|
70
|
+
<p align="center">
|
71
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
72
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
73
|
+
</a>
|
74
|
+
</p>
|
75
|
+
|
68
76
|
## License
|
69
77
|
|
70
78
|
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import { S as StandardHeaders, c as StandardRequest, e as StandardResponse } from '../shared/standard-server.BD8Sg1A5.mjs';
|
2
|
+
import { Promisable } from '@orpc/shared';
|
3
|
+
|
4
|
+
interface ToBatchRequestOptions {
|
5
|
+
url: URL;
|
6
|
+
method: 'GET' | 'POST';
|
7
|
+
headers: StandardHeaders;
|
8
|
+
requests: readonly StandardRequest[];
|
9
|
+
}
|
10
|
+
declare function toBatchRequest(options: ToBatchRequestOptions): StandardRequest;
|
11
|
+
declare function parseBatchRequest(request: StandardRequest): StandardRequest[];
|
12
|
+
|
13
|
+
type BatchResponseMode = 'streaming' | 'buffered';
|
14
|
+
interface BatchResponseBodyItem extends StandardResponse {
|
15
|
+
index: number;
|
16
|
+
}
|
17
|
+
interface ToBatchResponseOptions extends StandardResponse {
|
18
|
+
body: AsyncIteratorObject<BatchResponseBodyItem>;
|
19
|
+
/**
|
20
|
+
* @default 'streaming'
|
21
|
+
*/
|
22
|
+
mode?: BatchResponseMode;
|
23
|
+
}
|
24
|
+
declare function toBatchResponse(options: ToBatchResponseOptions): Promisable<StandardResponse>;
|
25
|
+
declare function parseBatchResponse(response: StandardResponse): AsyncGenerator<BatchResponseBodyItem>;
|
26
|
+
|
27
|
+
declare function toBatchAbortSignal(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined;
|
28
|
+
|
29
|
+
export { parseBatchRequest, parseBatchResponse, toBatchAbortSignal, toBatchRequest, toBatchResponse };
|
30
|
+
export type { BatchResponseBodyItem, BatchResponseMode, ToBatchRequestOptions, ToBatchResponseOptions };
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import { S as StandardHeaders, c as StandardRequest, e as StandardResponse } from '../shared/standard-server.BD8Sg1A5.js';
|
2
|
+
import { Promisable } from '@orpc/shared';
|
3
|
+
|
4
|
+
interface ToBatchRequestOptions {
|
5
|
+
url: URL;
|
6
|
+
method: 'GET' | 'POST';
|
7
|
+
headers: StandardHeaders;
|
8
|
+
requests: readonly StandardRequest[];
|
9
|
+
}
|
10
|
+
declare function toBatchRequest(options: ToBatchRequestOptions): StandardRequest;
|
11
|
+
declare function parseBatchRequest(request: StandardRequest): StandardRequest[];
|
12
|
+
|
13
|
+
type BatchResponseMode = 'streaming' | 'buffered';
|
14
|
+
interface BatchResponseBodyItem extends StandardResponse {
|
15
|
+
index: number;
|
16
|
+
}
|
17
|
+
interface ToBatchResponseOptions extends StandardResponse {
|
18
|
+
body: AsyncIteratorObject<BatchResponseBodyItem>;
|
19
|
+
/**
|
20
|
+
* @default 'streaming'
|
21
|
+
*/
|
22
|
+
mode?: BatchResponseMode;
|
23
|
+
}
|
24
|
+
declare function toBatchResponse(options: ToBatchResponseOptions): Promisable<StandardResponse>;
|
25
|
+
declare function parseBatchResponse(response: StandardResponse): AsyncGenerator<BatchResponseBodyItem>;
|
26
|
+
|
27
|
+
declare function toBatchAbortSignal(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined;
|
28
|
+
|
29
|
+
export { parseBatchRequest, parseBatchResponse, toBatchAbortSignal, toBatchRequest, toBatchResponse };
|
30
|
+
export type { BatchResponseBodyItem, BatchResponseMode, ToBatchRequestOptions, ToBatchResponseOptions };
|
@@ -0,0 +1,140 @@
|
|
1
|
+
import { stringifyJSON, parseEmptyableJSON, isAsyncIteratorObject, isObject } from '@orpc/shared';
|
2
|
+
|
3
|
+
function toBatchAbortSignal(signals) {
|
4
|
+
const realSignals = signals.filter((signal) => signal !== void 0);
|
5
|
+
if (realSignals.length === 0 || realSignals.length !== signals.length) {
|
6
|
+
return void 0;
|
7
|
+
}
|
8
|
+
const controller = new AbortController();
|
9
|
+
const abortIfAllInputsAborted = () => {
|
10
|
+
if (realSignals.every((signal) => signal.aborted)) {
|
11
|
+
controller.abort();
|
12
|
+
}
|
13
|
+
};
|
14
|
+
abortIfAllInputsAborted();
|
15
|
+
for (const signal of realSignals) {
|
16
|
+
signal.addEventListener("abort", () => {
|
17
|
+
abortIfAllInputsAborted();
|
18
|
+
}, {
|
19
|
+
once: true,
|
20
|
+
signal: controller.signal
|
21
|
+
});
|
22
|
+
}
|
23
|
+
return controller.signal;
|
24
|
+
}
|
25
|
+
|
26
|
+
function toBatchRequest(options) {
|
27
|
+
const url = new URL(options.url);
|
28
|
+
let body;
|
29
|
+
const batchRequestItems = options.requests.map((request) => ({
|
30
|
+
body: request.body,
|
31
|
+
headers: Object.keys(request.headers).length ? request.headers : void 0,
|
32
|
+
method: request.method === options.method ? void 0 : request.method,
|
33
|
+
url: request.url
|
34
|
+
}));
|
35
|
+
if (options.method === "GET") {
|
36
|
+
url.searchParams.append("batch", stringifyJSON(batchRequestItems));
|
37
|
+
} else if (options.method === "POST") {
|
38
|
+
body = batchRequestItems;
|
39
|
+
}
|
40
|
+
return {
|
41
|
+
method: options.method,
|
42
|
+
url,
|
43
|
+
headers: options.headers,
|
44
|
+
body,
|
45
|
+
signal: toBatchAbortSignal(options.requests.map((request) => request.signal))
|
46
|
+
};
|
47
|
+
}
|
48
|
+
function parseBatchRequest(request) {
|
49
|
+
const items = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("batch").at(-1)) : request.body;
|
50
|
+
if (!Array.isArray(items)) {
|
51
|
+
throw new TypeError("Invalid batch request");
|
52
|
+
}
|
53
|
+
return items.map((item) => {
|
54
|
+
return {
|
55
|
+
method: item.method ?? request.method,
|
56
|
+
url: new URL(item.url),
|
57
|
+
headers: item.headers ?? {},
|
58
|
+
body: item.body,
|
59
|
+
signal: request.signal
|
60
|
+
};
|
61
|
+
});
|
62
|
+
}
|
63
|
+
|
64
|
+
function toBatchResponse(options) {
|
65
|
+
const mode = options.mode ?? "streaming";
|
66
|
+
const minifyResponseItem = (item) => {
|
67
|
+
return {
|
68
|
+
index: item.index,
|
69
|
+
status: item.status === options.status ? void 0 : item.status,
|
70
|
+
headers: Object.keys(item.headers).length ? item.headers : void 0,
|
71
|
+
body: item.body
|
72
|
+
};
|
73
|
+
};
|
74
|
+
if (mode === "buffered") {
|
75
|
+
return (async () => {
|
76
|
+
try {
|
77
|
+
const body = [];
|
78
|
+
for await (const item of options.body) {
|
79
|
+
body.push(minifyResponseItem(item));
|
80
|
+
}
|
81
|
+
return {
|
82
|
+
headers: options.headers,
|
83
|
+
status: options.status,
|
84
|
+
body
|
85
|
+
};
|
86
|
+
} finally {
|
87
|
+
await options.body.return?.();
|
88
|
+
}
|
89
|
+
})();
|
90
|
+
}
|
91
|
+
return {
|
92
|
+
headers: options.headers,
|
93
|
+
status: options.status,
|
94
|
+
body: (async function* () {
|
95
|
+
try {
|
96
|
+
for await (const item of options.body) {
|
97
|
+
yield {
|
98
|
+
index: item.index,
|
99
|
+
status: item.status === options.status ? void 0 : item.status,
|
100
|
+
headers: Object.keys(item.headers).length ? item.headers : void 0,
|
101
|
+
body: item.body
|
102
|
+
};
|
103
|
+
}
|
104
|
+
} finally {
|
105
|
+
await options.body.return?.();
|
106
|
+
}
|
107
|
+
})()
|
108
|
+
};
|
109
|
+
}
|
110
|
+
function parseBatchResponse(response) {
|
111
|
+
const body = response.body;
|
112
|
+
if (isAsyncIteratorObject(body) || Array.isArray(body)) {
|
113
|
+
return (async function* () {
|
114
|
+
try {
|
115
|
+
for await (const item of body) {
|
116
|
+
if (!isObject(item) || !("index" in item) || typeof item.index !== "number") {
|
117
|
+
throw new TypeError("Invalid batch response", {
|
118
|
+
cause: item
|
119
|
+
});
|
120
|
+
}
|
121
|
+
yield {
|
122
|
+
index: item.index,
|
123
|
+
status: item.status ?? response.status,
|
124
|
+
headers: item.headers ?? {},
|
125
|
+
body: item.body
|
126
|
+
};
|
127
|
+
}
|
128
|
+
} finally {
|
129
|
+
if (isAsyncIteratorObject(body)) {
|
130
|
+
await body.return?.();
|
131
|
+
}
|
132
|
+
}
|
133
|
+
})();
|
134
|
+
}
|
135
|
+
throw new TypeError("Invalid batch response", {
|
136
|
+
cause: response
|
137
|
+
});
|
138
|
+
}
|
139
|
+
|
140
|
+
export { parseBatchRequest, parseBatchResponse, toBatchAbortSignal, toBatchRequest, toBatchResponse };
|
package/dist/index.d.mts
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
import { AsyncIteratorClass } from '@orpc/shared';
|
2
|
+
import { S as StandardHeaders, a as StandardLazyResponse } from './shared/standard-server.BD8Sg1A5.mjs';
|
3
|
+
export { b as StandardBody, d as StandardLazyRequest, c as StandardRequest, e as StandardResponse } from './shared/standard-server.BD8Sg1A5.mjs';
|
2
4
|
|
3
5
|
interface EventMessage {
|
4
6
|
event: string | undefined;
|
@@ -51,49 +53,23 @@ type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id' | 'comments'>>;
|
|
51
53
|
declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
52
54
|
declare function getEventMeta(container: unknown): EventMeta | undefined;
|
53
55
|
|
54
|
-
interface
|
55
|
-
|
56
|
+
interface HibernationEventIteratorCallback {
|
57
|
+
(id: string): void;
|
56
58
|
}
|
57
|
-
|
58
|
-
interface StandardRequest {
|
59
|
-
method: string;
|
60
|
-
url: URL;
|
61
|
-
headers: StandardHeaders;
|
59
|
+
declare class HibernationEventIterator<T, TReturn = unknown, TNext = unknown> extends AsyncIteratorClass<T, TReturn, TNext> {
|
62
60
|
/**
|
63
|
-
*
|
61
|
+
* this property is not transferred to the client, so it should be optional for type safety
|
64
62
|
*/
|
65
|
-
|
66
|
-
|
67
|
-
}
|
68
|
-
interface StandardLazyRequest extends Omit<StandardRequest, 'body'> {
|
69
|
-
/**
|
70
|
-
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
71
|
-
*/
|
72
|
-
raw: Record<string, unknown>;
|
73
|
-
/**
|
74
|
-
* The body has been parsed based on the content-type header.
|
75
|
-
* This method can safely call multiple times (cached).
|
76
|
-
*/
|
77
|
-
body: () => Promise<StandardBody>;
|
78
|
-
}
|
79
|
-
interface StandardResponse {
|
80
|
-
status: number;
|
81
|
-
headers: StandardHeaders;
|
82
|
-
/**
|
83
|
-
* The body has been parsed based on the content-type header.
|
84
|
-
*/
|
85
|
-
body: StandardBody;
|
86
|
-
}
|
87
|
-
interface StandardLazyResponse extends Omit<StandardResponse, 'body'> {
|
88
|
-
/**
|
89
|
-
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
90
|
-
*/
|
91
|
-
raw: Record<string, unknown>;
|
92
|
-
/**
|
93
|
-
* The body has been parsed based on the content-type header.
|
94
|
-
* This method can safely call multiple times (cached).
|
95
|
-
*/
|
96
|
-
body: () => Promise<StandardBody>;
|
63
|
+
readonly hibernationCallback?: HibernationEventIteratorCallback;
|
64
|
+
constructor(hibernationCallback: HibernationEventIteratorCallback);
|
97
65
|
}
|
98
66
|
|
99
|
-
|
67
|
+
declare function generateContentDisposition(filename: string): string;
|
68
|
+
declare function getFilenameFromContentDisposition(contentDisposition: string): string | undefined;
|
69
|
+
declare function mergeStandardHeaders(a: StandardHeaders, b: StandardHeaders): StandardHeaders;
|
70
|
+
declare function flattenHeader(header: string | readonly string[] | undefined): string | undefined;
|
71
|
+
declare function replicateStandardLazyResponse(response: StandardLazyResponse, count: number): StandardLazyResponse[];
|
72
|
+
declare function isEventIteratorHeaders(headers: StandardHeaders): boolean;
|
73
|
+
|
74
|
+
export { ErrorEvent, EventDecoder, EventDecoderError, EventDecoderStream, EventEncoderError, HibernationEventIterator, StandardHeaders, StandardLazyResponse, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, flattenHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isEventIteratorHeaders, mergeStandardHeaders, replicateStandardLazyResponse, withEventMeta };
|
75
|
+
export type { ErrorEventOptions, EventDecoderOptions, EventMessage, EventMeta, HibernationEventIteratorCallback };
|
package/dist/index.d.ts
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
import { AsyncIteratorClass } from '@orpc/shared';
|
2
|
+
import { S as StandardHeaders, a as StandardLazyResponse } from './shared/standard-server.BD8Sg1A5.js';
|
3
|
+
export { b as StandardBody, d as StandardLazyRequest, c as StandardRequest, e as StandardResponse } from './shared/standard-server.BD8Sg1A5.js';
|
2
4
|
|
3
5
|
interface EventMessage {
|
4
6
|
event: string | undefined;
|
@@ -51,49 +53,23 @@ type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id' | 'comments'>>;
|
|
51
53
|
declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
52
54
|
declare function getEventMeta(container: unknown): EventMeta | undefined;
|
53
55
|
|
54
|
-
interface
|
55
|
-
|
56
|
+
interface HibernationEventIteratorCallback {
|
57
|
+
(id: string): void;
|
56
58
|
}
|
57
|
-
|
58
|
-
interface StandardRequest {
|
59
|
-
method: string;
|
60
|
-
url: URL;
|
61
|
-
headers: StandardHeaders;
|
59
|
+
declare class HibernationEventIterator<T, TReturn = unknown, TNext = unknown> extends AsyncIteratorClass<T, TReturn, TNext> {
|
62
60
|
/**
|
63
|
-
*
|
61
|
+
* this property is not transferred to the client, so it should be optional for type safety
|
64
62
|
*/
|
65
|
-
|
66
|
-
|
67
|
-
}
|
68
|
-
interface StandardLazyRequest extends Omit<StandardRequest, 'body'> {
|
69
|
-
/**
|
70
|
-
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
71
|
-
*/
|
72
|
-
raw: Record<string, unknown>;
|
73
|
-
/**
|
74
|
-
* The body has been parsed based on the content-type header.
|
75
|
-
* This method can safely call multiple times (cached).
|
76
|
-
*/
|
77
|
-
body: () => Promise<StandardBody>;
|
78
|
-
}
|
79
|
-
interface StandardResponse {
|
80
|
-
status: number;
|
81
|
-
headers: StandardHeaders;
|
82
|
-
/**
|
83
|
-
* The body has been parsed based on the content-type header.
|
84
|
-
*/
|
85
|
-
body: StandardBody;
|
86
|
-
}
|
87
|
-
interface StandardLazyResponse extends Omit<StandardResponse, 'body'> {
|
88
|
-
/**
|
89
|
-
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
90
|
-
*/
|
91
|
-
raw: Record<string, unknown>;
|
92
|
-
/**
|
93
|
-
* The body has been parsed based on the content-type header.
|
94
|
-
* This method can safely call multiple times (cached).
|
95
|
-
*/
|
96
|
-
body: () => Promise<StandardBody>;
|
63
|
+
readonly hibernationCallback?: HibernationEventIteratorCallback;
|
64
|
+
constructor(hibernationCallback: HibernationEventIteratorCallback);
|
97
65
|
}
|
98
66
|
|
99
|
-
|
67
|
+
declare function generateContentDisposition(filename: string): string;
|
68
|
+
declare function getFilenameFromContentDisposition(contentDisposition: string): string | undefined;
|
69
|
+
declare function mergeStandardHeaders(a: StandardHeaders, b: StandardHeaders): StandardHeaders;
|
70
|
+
declare function flattenHeader(header: string | readonly string[] | undefined): string | undefined;
|
71
|
+
declare function replicateStandardLazyResponse(response: StandardLazyResponse, count: number): StandardLazyResponse[];
|
72
|
+
declare function isEventIteratorHeaders(headers: StandardHeaders): boolean;
|
73
|
+
|
74
|
+
export { ErrorEvent, EventDecoder, EventDecoderError, EventDecoderStream, EventEncoderError, HibernationEventIterator, StandardHeaders, StandardLazyResponse, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, flattenHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isEventIteratorHeaders, mergeStandardHeaders, replicateStandardLazyResponse, withEventMeta };
|
75
|
+
export type { ErrorEventOptions, EventDecoderOptions, EventMessage, EventMeta, HibernationEventIteratorCallback };
|
package/dist/index.mjs
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
import { isTypescriptObject } from '@orpc/shared';
|
2
|
-
export { contentDisposition, parse as parseContentDisposition } from '@tinyhttp/content-disposition';
|
1
|
+
import { isTypescriptObject, AsyncIteratorClass, tryDecodeURIComponent, toArray, once, isAsyncIteratorObject, replicateAsyncIterator } from '@orpc/shared';
|
3
2
|
|
4
3
|
class EventEncoderError extends TypeError {
|
5
4
|
}
|
@@ -183,4 +182,88 @@ function getEventMeta(container) {
|
|
183
182
|
return isTypescriptObject(container) ? Reflect.get(container, EVENT_SOURCE_META_SYMBOL) : void 0;
|
184
183
|
}
|
185
184
|
|
186
|
-
|
185
|
+
class HibernationEventIterator extends AsyncIteratorClass {
|
186
|
+
/**
|
187
|
+
* this property is not transferred to the client, so it should be optional for type safety
|
188
|
+
*/
|
189
|
+
hibernationCallback;
|
190
|
+
constructor(hibernationCallback) {
|
191
|
+
super(async () => {
|
192
|
+
throw new Error("Cannot iterate over hibernating iterator directly");
|
193
|
+
}, async (reason) => {
|
194
|
+
if (reason !== "next") {
|
195
|
+
throw new Error("Cannot cleanup hibernating iterator directly");
|
196
|
+
}
|
197
|
+
});
|
198
|
+
this.hibernationCallback = hibernationCallback;
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
function generateContentDisposition(filename) {
|
203
|
+
const escapedFileName = filename.replace(/"/g, '\\"');
|
204
|
+
const encodedFilenameStar = encodeURIComponent(filename).replace(/['()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`).replace(/%(7C|60|5E)/g, (str, hex) => String.fromCharCode(Number.parseInt(hex, 16)));
|
205
|
+
return `inline; filename="${escapedFileName}"; filename*=utf-8''${encodedFilenameStar}`;
|
206
|
+
}
|
207
|
+
function getFilenameFromContentDisposition(contentDisposition) {
|
208
|
+
const encodedFilenameStarMatch = contentDisposition.match(/filename\*=(UTF-8'')?([^;]*)/i);
|
209
|
+
if (encodedFilenameStarMatch && typeof encodedFilenameStarMatch[2] === "string") {
|
210
|
+
return tryDecodeURIComponent(encodedFilenameStarMatch[2]);
|
211
|
+
}
|
212
|
+
const encodedFilenameMatch = contentDisposition.match(/filename="((?:\\"|[^"])*)"/i);
|
213
|
+
if (encodedFilenameMatch && typeof encodedFilenameMatch[1] === "string") {
|
214
|
+
return encodedFilenameMatch[1].replace(/\\"/g, '"');
|
215
|
+
}
|
216
|
+
}
|
217
|
+
function mergeStandardHeaders(a, b) {
|
218
|
+
const merged = { ...a };
|
219
|
+
for (const key in b) {
|
220
|
+
if (Array.isArray(b[key])) {
|
221
|
+
merged[key] = [...toArray(merged[key]), ...b[key]];
|
222
|
+
} else if (b[key] !== void 0) {
|
223
|
+
if (Array.isArray(merged[key])) {
|
224
|
+
merged[key] = [...merged[key], b[key]];
|
225
|
+
} else if (merged[key] !== void 0) {
|
226
|
+
merged[key] = [merged[key], b[key]];
|
227
|
+
} else {
|
228
|
+
merged[key] = b[key];
|
229
|
+
}
|
230
|
+
}
|
231
|
+
}
|
232
|
+
return merged;
|
233
|
+
}
|
234
|
+
function flattenHeader(header) {
|
235
|
+
if (typeof header === "string" || header === void 0) {
|
236
|
+
return header;
|
237
|
+
}
|
238
|
+
if (header.length === 0) {
|
239
|
+
return void 0;
|
240
|
+
}
|
241
|
+
return header.join(", ");
|
242
|
+
}
|
243
|
+
function replicateStandardLazyResponse(response, count) {
|
244
|
+
const replicated = [];
|
245
|
+
let bodyPromise;
|
246
|
+
let replicatedAsyncIteratorObjects;
|
247
|
+
for (let i = 0; i < count; i++) {
|
248
|
+
replicated.push({
|
249
|
+
...response,
|
250
|
+
body: once(async () => {
|
251
|
+
if (replicatedAsyncIteratorObjects) {
|
252
|
+
return replicatedAsyncIteratorObjects.shift();
|
253
|
+
}
|
254
|
+
const body = await (bodyPromise ??= response.body());
|
255
|
+
if (!isAsyncIteratorObject(body)) {
|
256
|
+
return body;
|
257
|
+
}
|
258
|
+
replicatedAsyncIteratorObjects = replicateAsyncIterator(body, count);
|
259
|
+
return replicatedAsyncIteratorObjects.shift();
|
260
|
+
})
|
261
|
+
});
|
262
|
+
}
|
263
|
+
return replicated;
|
264
|
+
}
|
265
|
+
function isEventIteratorHeaders(headers) {
|
266
|
+
return Boolean(flattenHeader(headers["content-type"])?.startsWith("text/event-stream") && flattenHeader(headers["content-disposition"]) === void 0);
|
267
|
+
}
|
268
|
+
|
269
|
+
export { ErrorEvent, EventDecoder, EventDecoderError, EventDecoderStream, EventEncoderError, HibernationEventIterator, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, flattenHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isEventIteratorHeaders, mergeStandardHeaders, replicateStandardLazyResponse, withEventMeta };
|
@@ -0,0 +1,38 @@
|
|
1
|
+
interface StandardHeaders {
|
2
|
+
[key: string]: string | string[] | undefined;
|
3
|
+
}
|
4
|
+
type StandardBody = undefined | unknown | Blob | URLSearchParams | FormData | AsyncIterator<unknown | void, unknown | void, undefined>;
|
5
|
+
interface StandardRequest {
|
6
|
+
method: string;
|
7
|
+
url: URL;
|
8
|
+
headers: StandardHeaders;
|
9
|
+
/**
|
10
|
+
* The body has been parsed based on the content-type header.
|
11
|
+
*/
|
12
|
+
body: StandardBody;
|
13
|
+
signal: AbortSignal | undefined;
|
14
|
+
}
|
15
|
+
interface StandardLazyRequest extends Omit<StandardRequest, 'body'> {
|
16
|
+
/**
|
17
|
+
* The body has been parsed based on the content-type header.
|
18
|
+
* This method can safely call multiple times (cached).
|
19
|
+
*/
|
20
|
+
body: () => Promise<StandardBody>;
|
21
|
+
}
|
22
|
+
interface StandardResponse {
|
23
|
+
status: number;
|
24
|
+
headers: StandardHeaders;
|
25
|
+
/**
|
26
|
+
* The body has been parsed based on the content-type header.
|
27
|
+
*/
|
28
|
+
body: StandardBody;
|
29
|
+
}
|
30
|
+
interface StandardLazyResponse extends Omit<StandardResponse, 'body'> {
|
31
|
+
/**
|
32
|
+
* The body has been parsed based on the content-type header.
|
33
|
+
* This method can safely call multiple times (cached).
|
34
|
+
*/
|
35
|
+
body: () => Promise<StandardBody>;
|
36
|
+
}
|
37
|
+
|
38
|
+
export type { StandardHeaders as S, StandardLazyResponse as a, StandardBody as b, StandardRequest as c, StandardLazyRequest as d, StandardResponse as e };
|
@@ -0,0 +1,38 @@
|
|
1
|
+
interface StandardHeaders {
|
2
|
+
[key: string]: string | string[] | undefined;
|
3
|
+
}
|
4
|
+
type StandardBody = undefined | unknown | Blob | URLSearchParams | FormData | AsyncIterator<unknown | void, unknown | void, undefined>;
|
5
|
+
interface StandardRequest {
|
6
|
+
method: string;
|
7
|
+
url: URL;
|
8
|
+
headers: StandardHeaders;
|
9
|
+
/**
|
10
|
+
* The body has been parsed based on the content-type header.
|
11
|
+
*/
|
12
|
+
body: StandardBody;
|
13
|
+
signal: AbortSignal | undefined;
|
14
|
+
}
|
15
|
+
interface StandardLazyRequest extends Omit<StandardRequest, 'body'> {
|
16
|
+
/**
|
17
|
+
* The body has been parsed based on the content-type header.
|
18
|
+
* This method can safely call multiple times (cached).
|
19
|
+
*/
|
20
|
+
body: () => Promise<StandardBody>;
|
21
|
+
}
|
22
|
+
interface StandardResponse {
|
23
|
+
status: number;
|
24
|
+
headers: StandardHeaders;
|
25
|
+
/**
|
26
|
+
* The body has been parsed based on the content-type header.
|
27
|
+
*/
|
28
|
+
body: StandardBody;
|
29
|
+
}
|
30
|
+
interface StandardLazyResponse extends Omit<StandardResponse, 'body'> {
|
31
|
+
/**
|
32
|
+
* The body has been parsed based on the content-type header.
|
33
|
+
* This method can safely call multiple times (cached).
|
34
|
+
*/
|
35
|
+
body: () => Promise<StandardBody>;
|
36
|
+
}
|
37
|
+
|
38
|
+
export type { StandardHeaders as S, StandardLazyResponse as a, StandardBody as b, StandardRequest as c, StandardLazyRequest as d, StandardResponse as e };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/standard-server",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.0.0-next.
|
4
|
+
"version": "0.0.0-next.ff7ad2e",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://unnoq.com",
|
7
7
|
"repository": {
|
@@ -17,14 +17,18 @@
|
|
17
17
|
"types": "./dist/index.d.mts",
|
18
18
|
"import": "./dist/index.mjs",
|
19
19
|
"default": "./dist/index.mjs"
|
20
|
+
},
|
21
|
+
"./batch": {
|
22
|
+
"types": "./dist/batch/index.d.mts",
|
23
|
+
"import": "./dist/batch/index.mjs",
|
24
|
+
"default": "./dist/batch/index.mjs"
|
20
25
|
}
|
21
26
|
},
|
22
27
|
"files": [
|
23
28
|
"dist"
|
24
29
|
],
|
25
30
|
"dependencies": {
|
26
|
-
"@
|
27
|
-
"@orpc/shared": "0.0.0-next.ff68fdb"
|
31
|
+
"@orpc/shared": "0.0.0-next.ff7ad2e"
|
28
32
|
},
|
29
33
|
"scripts": {
|
30
34
|
"build": "unbuild",
|