@orpc/server 0.0.0-next.6b26cdc → 0.0.0-next.6bc474e
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 +145 -35
- package/dist/adapters/aws-lambda/index.d.mts +46 -0
- package/dist/adapters/aws-lambda/index.d.ts +46 -0
- package/dist/adapters/aws-lambda/index.mjs +42 -0
- package/dist/adapters/bun-ws/index.d.mts +36 -0
- package/dist/adapters/bun-ws/index.d.ts +36 -0
- package/dist/adapters/bun-ws/index.mjs +47 -0
- package/dist/adapters/crossws/index.d.mts +33 -0
- package/dist/adapters/crossws/index.d.ts +33 -0
- package/dist/adapters/crossws/index.mjs +45 -0
- package/dist/adapters/fastify/index.d.mts +53 -0
- package/dist/adapters/fastify/index.d.ts +53 -0
- package/dist/adapters/fastify/index.mjs +54 -0
- package/dist/adapters/fetch/index.d.mts +74 -8
- package/dist/adapters/fetch/index.d.ts +74 -8
- package/dist/adapters/fetch/index.mjs +177 -8
- package/dist/adapters/message-port/index.d.mts +57 -0
- package/dist/adapters/message-port/index.d.ts +57 -0
- package/dist/adapters/message-port/index.mjs +55 -0
- package/dist/adapters/node/index.d.mts +51 -9
- package/dist/adapters/node/index.d.ts +51 -9
- package/dist/adapters/node/index.mjs +76 -14
- package/dist/adapters/standard/index.d.mts +8 -13
- package/dist/adapters/standard/index.d.ts +8 -13
- package/dist/adapters/standard/index.mjs +5 -5
- package/dist/adapters/standard-peer/index.d.mts +18 -0
- package/dist/adapters/standard-peer/index.d.ts +18 -0
- package/dist/adapters/standard-peer/index.mjs +7 -0
- package/dist/adapters/websocket/index.d.mts +56 -0
- package/dist/adapters/websocket/index.d.ts +56 -0
- package/dist/adapters/websocket/index.mjs +67 -0
- package/dist/adapters/ws/index.d.mts +31 -0
- package/dist/adapters/ws/index.d.ts +31 -0
- package/dist/adapters/ws/index.mjs +37 -0
- package/dist/helpers/index.d.mts +149 -0
- package/dist/helpers/index.d.ts +149 -0
- package/dist/helpers/index.mjs +198 -0
- package/dist/hibernation/index.d.mts +44 -0
- package/dist/hibernation/index.d.ts +44 -0
- package/dist/hibernation/index.mjs +65 -0
- package/dist/index.d.mts +563 -51
- package/dist/index.d.ts +563 -51
- package/dist/index.mjs +142 -13
- package/dist/plugins/index.d.mts +92 -12
- package/dist/plugins/index.d.ts +92 -12
- package/dist/plugins/index.mjs +142 -66
- package/dist/shared/{server.DFFT_EZo.d.ts → server.7cEtMB30.d.ts} +18 -17
- package/dist/shared/server.B8gYOD5g.d.mts +12 -0
- package/dist/shared/{server.DOYDVeMX.d.mts → server.BqadksTP.d.mts} +18 -17
- package/dist/shared/{server.DFuJLDuo.mjs → server.Bxx6tqNe.mjs} +80 -51
- package/dist/shared/server.C8_sRzQB.d.mts +42 -0
- package/dist/shared/server.ChUyt5-i.d.mts +32 -0
- package/dist/shared/server.ChyoA9XY.d.ts +42 -0
- package/dist/shared/{server.BVwwTHyO.mjs → server.DZ5BIITo.mjs} +1 -1
- package/dist/shared/{server.C37gDhSZ.mjs → server.Ds4HPpvH.mjs} +119 -74
- package/dist/shared/server.EfTOZ2Q7.d.ts +12 -0
- package/dist/shared/{server.BW-nUGgA.mjs → server.TEVCLCFC.mjs} +3 -0
- package/dist/shared/server.UVMTOWrk.mjs +26 -0
- package/dist/shared/{server.DLt5njUb.d.mts → server.qKsRrdxW.d.mts} +60 -10
- package/dist/shared/{server.DLt5njUb.d.ts → server.qKsRrdxW.d.ts} +60 -10
- package/dist/shared/server.yoEB3Fx4.d.ts +32 -0
- package/package.json +82 -23
- package/dist/adapters/hono/index.d.mts +0 -22
- package/dist/adapters/hono/index.d.ts +0 -22
- package/dist/adapters/hono/index.mjs +0 -35
- package/dist/adapters/next/index.d.mts +0 -29
- package/dist/adapters/next/index.d.ts +0 -29
- package/dist/adapters/next/index.mjs +0 -32
- package/dist/shared/server.Bm0UqHzd.mjs +0 -103
- package/dist/shared/server.C8NkqxHo.d.ts +0 -17
- package/dist/shared/server.CGCwEAt_.d.mts +0 -10
- package/dist/shared/server.DCQgF_JR.d.mts +0 -17
- package/dist/shared/server._2UufoXA.d.ts +0 -10
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Value } from '@orpc/shared';
|
|
1
|
+
import { Value, Promisable, ThrowableError } from '@orpc/shared';
|
|
2
2
|
import { StandardRequest, StandardHeaders } from '@orpc/standard-server';
|
|
3
3
|
import { BatchResponseBodyItem } from '@orpc/standard-server/batch';
|
|
4
|
-
import {
|
|
5
|
-
import { C as Context,
|
|
4
|
+
import { d as StandardHandlerInterceptorOptions, g as StandardHandlerPlugin, e as StandardHandlerOptions } from '../shared/server.7cEtMB30.js';
|
|
5
|
+
import { C as Context, d as ProcedureClientInterceptorOptions } from '../shared/server.qKsRrdxW.js';
|
|
6
6
|
import { Meta, ORPCError as ORPCError$1 } from '@orpc/contract';
|
|
7
7
|
import { ORPCError } from '@orpc/client';
|
|
8
8
|
|
|
@@ -12,7 +12,7 @@ interface BatchHandlerOptions<T extends Context> {
|
|
|
12
12
|
*
|
|
13
13
|
* @default 10
|
|
14
14
|
*/
|
|
15
|
-
maxSize?: Value<number
|
|
15
|
+
maxSize?: Value<Promisable<number>, [StandardHandlerInterceptorOptions<T>]>;
|
|
16
16
|
/**
|
|
17
17
|
* Map the request before processing it.
|
|
18
18
|
*
|
|
@@ -24,14 +24,20 @@ interface BatchHandlerOptions<T extends Context> {
|
|
|
24
24
|
*
|
|
25
25
|
* @default 207
|
|
26
26
|
*/
|
|
27
|
-
successStatus?: Value<number
|
|
27
|
+
successStatus?: Value<Promisable<number>, [responses: Promise<BatchResponseBodyItem>[], batchOptions: StandardHandlerInterceptorOptions<T>]>;
|
|
28
28
|
/**
|
|
29
29
|
* success batch response headers.
|
|
30
30
|
*
|
|
31
31
|
* @default {}
|
|
32
32
|
*/
|
|
33
|
-
headers?: Value<StandardHeaders
|
|
33
|
+
headers?: Value<Promisable<StandardHeaders>, [responses: Promise<BatchResponseBodyItem>[], batchOptions: StandardHandlerInterceptorOptions<T>]>;
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* The Batch Requests Plugin allows you to combine multiple requests and responses into a single batch,
|
|
37
|
+
* reducing the overhead of sending each one separately.
|
|
38
|
+
*
|
|
39
|
+
* @see {@link https://orpc.dev/docs/plugins/batch-requests Batch Requests Plugin Docs}
|
|
40
|
+
*/
|
|
35
41
|
declare class BatchHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
36
42
|
private readonly maxSize;
|
|
37
43
|
private readonly mapRequestItem;
|
|
@@ -43,14 +49,19 @@ declare class BatchHandlerPlugin<T extends Context> implements StandardHandlerPl
|
|
|
43
49
|
}
|
|
44
50
|
|
|
45
51
|
interface CORSOptions<T extends Context> {
|
|
46
|
-
origin?: Value<string | readonly string[] | null | undefined
|
|
47
|
-
timingOrigin?: Value<string | readonly string[] | null | undefined
|
|
52
|
+
origin?: Value<Promisable<string | readonly string[] | null | undefined>, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
|
|
53
|
+
timingOrigin?: Value<Promisable<string | readonly string[] | null | undefined>, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
|
|
48
54
|
allowMethods?: readonly string[];
|
|
49
55
|
allowHeaders?: readonly string[];
|
|
50
56
|
maxAge?: number;
|
|
51
57
|
credentials?: boolean;
|
|
52
58
|
exposeHeaders?: readonly string[];
|
|
53
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* CORSPlugin is a plugin for oRPC that allows you to configure CORS for your API.
|
|
62
|
+
*
|
|
63
|
+
* @see {@link https://orpc.dev/docs/plugins/cors CORS Plugin Docs}
|
|
64
|
+
*/
|
|
54
65
|
declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
55
66
|
private readonly options;
|
|
56
67
|
order: number;
|
|
@@ -58,34 +69,85 @@ declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T>
|
|
|
58
69
|
init(options: StandardHandlerOptions<T>): void;
|
|
59
70
|
}
|
|
60
71
|
|
|
72
|
+
interface RequestHeadersPluginContext {
|
|
73
|
+
reqHeaders?: Headers;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The Request Headers Plugin injects a `reqHeaders` instance into the context,
|
|
77
|
+
* allowing access to request headers in oRPC.
|
|
78
|
+
*
|
|
79
|
+
* @see {@link https://orpc.dev/docs/plugins/request-headers Request Headers Plugin Docs}
|
|
80
|
+
*/
|
|
81
|
+
declare class RequestHeadersPlugin<T extends RequestHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
|
82
|
+
init(options: StandardHandlerOptions<T>): void;
|
|
83
|
+
}
|
|
84
|
+
|
|
61
85
|
interface ResponseHeadersPluginContext {
|
|
62
86
|
resHeaders?: Headers;
|
|
63
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* The Response Headers Plugin allows you to set response headers in oRPC.
|
|
90
|
+
* It injects a resHeaders instance into the context, enabling you to modify response headers easily.
|
|
91
|
+
*
|
|
92
|
+
* @see {@link https://orpc.dev/docs/plugins/response-headers Response Headers Plugin Docs}
|
|
93
|
+
*/
|
|
64
94
|
declare class ResponseHeadersPlugin<T extends ResponseHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
|
65
95
|
init(options: StandardHandlerOptions<T>): void;
|
|
66
96
|
}
|
|
67
97
|
|
|
98
|
+
interface experimental_RethrowHandlerPluginOptions<T extends Context> {
|
|
99
|
+
/**
|
|
100
|
+
* Decide which errors should be rethrown.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* const rethrowPlugin = new RethrowHandlerPlugin({
|
|
105
|
+
* filter: (error) => {
|
|
106
|
+
* // Rethrow all non-ORPCError errors
|
|
107
|
+
* return !(error instanceof ORPCError)
|
|
108
|
+
* }
|
|
109
|
+
* })
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
filter: Value<boolean, [error: ThrowableError, options: StandardHandlerInterceptorOptions<T>]>;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The plugin allows you to catch and rethrow specific errors that occur during request handling.
|
|
116
|
+
* This is particularly useful when your framework has its own error handling mechanism
|
|
117
|
+
* (e.g., global exception filters in NestJS, error middleware in Express)
|
|
118
|
+
* and you want certain errors to be processed by that mechanism instead of being handled by the
|
|
119
|
+
* oRPC error handling flow.
|
|
120
|
+
*
|
|
121
|
+
* @see {@link https://orpc.dev/docs/plugins/rethrow-handler Rethrow Handler Plugin Docs}
|
|
122
|
+
*/
|
|
123
|
+
declare class experimental_RethrowHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
124
|
+
private readonly filter;
|
|
125
|
+
CONTEXT_SYMBOL: symbol;
|
|
126
|
+
constructor(options: experimental_RethrowHandlerPluginOptions<T>);
|
|
127
|
+
init(options: StandardHandlerOptions<T>): void;
|
|
128
|
+
}
|
|
129
|
+
|
|
68
130
|
interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
|
|
69
131
|
/**
|
|
70
132
|
* The name of the header to check.
|
|
71
133
|
*
|
|
72
134
|
* @default 'x-csrf-token'
|
|
73
135
|
*/
|
|
74
|
-
headerName?: Value<string
|
|
136
|
+
headerName?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
|
|
75
137
|
/**
|
|
76
138
|
* The value of the header to check.
|
|
77
139
|
*
|
|
78
140
|
* @default 'orpc'
|
|
79
141
|
*
|
|
80
142
|
*/
|
|
81
|
-
headerValue?: Value<string
|
|
143
|
+
headerValue?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
|
|
82
144
|
/**
|
|
83
145
|
* Exclude a procedure from the plugin.
|
|
84
146
|
*
|
|
85
147
|
* @default false
|
|
86
148
|
*
|
|
87
149
|
*/
|
|
88
|
-
exclude?: Value<boolean
|
|
150
|
+
exclude?: Value<Promisable<boolean>, [options: ProcedureClientInterceptorOptions<T, Record<never, never>, Meta>]>;
|
|
89
151
|
/**
|
|
90
152
|
* The error thrown when the CSRF token is invalid.
|
|
91
153
|
*
|
|
@@ -96,6 +158,13 @@ interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
|
|
|
96
158
|
*/
|
|
97
159
|
error?: InstanceType<typeof ORPCError>;
|
|
98
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* This plugin adds basic Cross-Site Request Forgery (CSRF) protection to your oRPC application.
|
|
163
|
+
* It helps ensure that requests to your procedures originate from JavaScript code,
|
|
164
|
+
* not from other sources like standard HTML forms or direct browser navigation.
|
|
165
|
+
*
|
|
166
|
+
* @see {@link https://orpc.dev/docs/plugins/simple-csrf-protection Simple CSRF Protection Plugin Docs}
|
|
167
|
+
*/
|
|
99
168
|
declare class SimpleCsrfProtectionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
100
169
|
private readonly headerName;
|
|
101
170
|
private readonly headerValue;
|
|
@@ -114,11 +183,22 @@ interface StrictGetMethodPluginOptions {
|
|
|
114
183
|
*/
|
|
115
184
|
error?: InstanceType<typeof ORPCError$1>;
|
|
116
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* This plugin enhances security by ensuring only procedures explicitly marked to accept GET requests
|
|
188
|
+
* can be called using the HTTP GET method for RPC Protocol. This helps prevent certain types of
|
|
189
|
+
* Cross-Site Request Forgery (CSRF) attacks.
|
|
190
|
+
*
|
|
191
|
+
* @see {@link https://orpc.dev/docs/plugins/strict-get-method Strict Get Method Plugin Docs}
|
|
192
|
+
*/
|
|
117
193
|
declare class StrictGetMethodPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
118
194
|
private readonly error;
|
|
195
|
+
/**
|
|
196
|
+
* make sure execute before batch plugin to get real method
|
|
197
|
+
*/
|
|
119
198
|
order: number;
|
|
120
199
|
constructor(options?: StrictGetMethodPluginOptions);
|
|
121
200
|
init(options: StandardHandlerOptions<T>): void;
|
|
122
201
|
}
|
|
123
202
|
|
|
124
|
-
export {
|
|
203
|
+
export { BatchHandlerPlugin, CORSPlugin, RequestHeadersPlugin, ResponseHeadersPlugin, SimpleCsrfProtectionHandlerPlugin, StrictGetMethodPlugin, experimental_RethrowHandlerPlugin };
|
|
204
|
+
export type { BatchHandlerOptions, CORSOptions, RequestHeadersPluginContext, ResponseHeadersPluginContext, SimpleCsrfProtectionHandlerPluginOptions, StrictGetMethodPluginOptions, experimental_RethrowHandlerPluginOptions };
|
package/dist/plugins/index.mjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { value, isAsyncIteratorObject } from '@orpc/shared';
|
|
1
|
+
import { runWithSpan, value, setSpanError, isAsyncIteratorObject, AsyncIteratorClass, clone } from '@orpc/shared';
|
|
2
|
+
import { flattenHeader } from '@orpc/standard-server';
|
|
2
3
|
import { parseBatchRequest, toBatchResponse } from '@orpc/standard-server/batch';
|
|
4
|
+
import { toFetchHeaders } from '@orpc/standard-server-fetch';
|
|
3
5
|
import { ORPCError } from '@orpc/client';
|
|
4
|
-
export { S as StrictGetMethodPlugin } from '../shared/server.
|
|
6
|
+
export { S as StrictGetMethodPlugin } from '../shared/server.TEVCLCFC.mjs';
|
|
5
7
|
import '@orpc/contract';
|
|
6
8
|
|
|
7
9
|
class BatchHandlerPlugin {
|
|
@@ -25,69 +27,82 @@ class BatchHandlerPlugin {
|
|
|
25
27
|
init(options) {
|
|
26
28
|
options.rootInterceptors ??= [];
|
|
27
29
|
options.rootInterceptors.unshift(async (options2) => {
|
|
28
|
-
|
|
30
|
+
const xHeader = flattenHeader(options2.request.headers["x-orpc-batch"]);
|
|
31
|
+
if (xHeader === void 0) {
|
|
29
32
|
return options2.next();
|
|
30
33
|
}
|
|
31
34
|
let isParsing = false;
|
|
32
35
|
try {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return options2.next({ ...options2, request: { ...mapped, body: () => Promise.resolve(mapped.body) } }).then(({ response: response2, matched }) => {
|
|
51
|
-
if (matched) {
|
|
52
|
-
if (response2.body instanceof Blob || response2.body instanceof FormData || isAsyncIteratorObject(response2.body)) {
|
|
53
|
-
return {
|
|
54
|
-
index,
|
|
55
|
-
status: 500,
|
|
56
|
-
headers: {},
|
|
57
|
-
body: "Batch responses do not support file/blob, or event-iterator. Please call this procedure separately outside of the batch request."
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
return { ...response2, index };
|
|
36
|
+
return await runWithSpan({ name: "handle_batch_request" }, async (span) => {
|
|
37
|
+
const mode = xHeader === "buffered" ? "buffered" : "streaming";
|
|
38
|
+
isParsing = true;
|
|
39
|
+
const parsed = parseBatchRequest({ ...options2.request, body: await options2.request.body() });
|
|
40
|
+
isParsing = false;
|
|
41
|
+
span?.setAttribute("batch.mode", mode);
|
|
42
|
+
span?.setAttribute("batch.size", parsed.length);
|
|
43
|
+
const maxSize = await value(this.maxSize, options2);
|
|
44
|
+
if (parsed.length > maxSize) {
|
|
45
|
+
const message = "Batch request size exceeds the maximum allowed size";
|
|
46
|
+
setSpanError(span, message);
|
|
47
|
+
return {
|
|
48
|
+
matched: true,
|
|
49
|
+
response: {
|
|
50
|
+
status: 413,
|
|
51
|
+
headers: {},
|
|
52
|
+
body: message
|
|
61
53
|
}
|
|
62
|
-
|
|
63
|
-
}).catch(() => {
|
|
64
|
-
return { index, status: 500, headers: {}, body: "Internal server error" };
|
|
65
|
-
});
|
|
54
|
+
};
|
|
66
55
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
56
|
+
const responses = parsed.map(
|
|
57
|
+
(request, index) => {
|
|
58
|
+
const mapped = this.mapRequestItem(request, options2);
|
|
59
|
+
return options2.next({ ...options2, request: { ...mapped, body: () => Promise.resolve(mapped.body) } }).then(({ response: response2, matched }) => {
|
|
60
|
+
span?.addEvent(`response.${index}.${matched ? "success" : "not_matched"}`);
|
|
61
|
+
if (matched) {
|
|
62
|
+
if (response2.body instanceof Blob || response2.body instanceof FormData || isAsyncIteratorObject(response2.body)) {
|
|
63
|
+
return {
|
|
64
|
+
index,
|
|
65
|
+
status: 500,
|
|
66
|
+
headers: {},
|
|
67
|
+
body: "Batch responses do not support file/blob, or event-iterator. Please call this procedure separately outside of the batch request."
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return { ...response2, index };
|
|
71
|
+
}
|
|
72
|
+
return { index, status: 404, headers: {}, body: "No procedure matched" };
|
|
73
|
+
}).catch((err) => {
|
|
74
|
+
Promise.reject(err);
|
|
75
|
+
return { index, status: 500, headers: {}, body: "Internal server error" };
|
|
76
|
+
});
|
|
84
77
|
}
|
|
85
|
-
|
|
78
|
+
);
|
|
79
|
+
await Promise.race(responses);
|
|
80
|
+
const status = await value(this.successStatus, responses, options2);
|
|
81
|
+
const headers = await value(this.headers, responses, options2);
|
|
82
|
+
const promises = [...responses];
|
|
83
|
+
const response = await toBatchResponse({
|
|
84
|
+
status,
|
|
85
|
+
headers,
|
|
86
|
+
mode,
|
|
87
|
+
body: new AsyncIteratorClass(
|
|
88
|
+
async () => {
|
|
89
|
+
const handling = promises.filter((p) => p !== void 0);
|
|
90
|
+
if (handling.length <= 0) {
|
|
91
|
+
return { done: true, value: void 0 };
|
|
92
|
+
}
|
|
93
|
+
const value2 = await Promise.race(handling);
|
|
94
|
+
promises[value2.index] = void 0;
|
|
95
|
+
return { done: false, value: value2 };
|
|
96
|
+
},
|
|
97
|
+
async () => {
|
|
98
|
+
}
|
|
99
|
+
)
|
|
100
|
+
});
|
|
101
|
+
return {
|
|
102
|
+
matched: true,
|
|
103
|
+
response
|
|
104
|
+
};
|
|
86
105
|
});
|
|
87
|
-
return {
|
|
88
|
-
matched: true,
|
|
89
|
-
response
|
|
90
|
-
};
|
|
91
106
|
} catch (cause) {
|
|
92
107
|
if (isParsing) {
|
|
93
108
|
return {
|
|
@@ -123,13 +138,11 @@ class CORSPlugin {
|
|
|
123
138
|
resHeaders["access-control-max-age"] = this.options.maxAge.toString();
|
|
124
139
|
}
|
|
125
140
|
if (this.options.allowMethods?.length) {
|
|
126
|
-
resHeaders["access-control-allow-methods"] = this.options.allowMethods
|
|
141
|
+
resHeaders["access-control-allow-methods"] = flattenHeader(this.options.allowMethods);
|
|
127
142
|
}
|
|
128
143
|
const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers["access-control-request-headers"];
|
|
129
|
-
if (
|
|
130
|
-
resHeaders["access-control-allow-headers"] = allowHeaders
|
|
131
|
-
} else if (typeof allowHeaders === "string") {
|
|
132
|
-
resHeaders["access-control-allow-headers"] = allowHeaders;
|
|
144
|
+
if (typeof allowHeaders === "string" || allowHeaders?.length) {
|
|
145
|
+
resHeaders["access-control-allow-headers"] = flattenHeader(allowHeaders);
|
|
133
146
|
}
|
|
134
147
|
return {
|
|
135
148
|
matched: true,
|
|
@@ -147,7 +160,7 @@ class CORSPlugin {
|
|
|
147
160
|
if (!result.matched) {
|
|
148
161
|
return result;
|
|
149
162
|
}
|
|
150
|
-
const origin =
|
|
163
|
+
const origin = flattenHeader(interceptorOptions.request.headers.origin) ?? "";
|
|
151
164
|
const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
|
|
152
165
|
const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
|
|
153
166
|
if (allowedOriginArr.includes("*")) {
|
|
@@ -169,13 +182,29 @@ class CORSPlugin {
|
|
|
169
182
|
result.response.headers["access-control-allow-credentials"] = "true";
|
|
170
183
|
}
|
|
171
184
|
if (this.options.exposeHeaders?.length) {
|
|
172
|
-
result.response.headers["access-control-expose-headers"] = this.options.exposeHeaders
|
|
185
|
+
result.response.headers["access-control-expose-headers"] = flattenHeader(this.options.exposeHeaders);
|
|
173
186
|
}
|
|
174
187
|
return result;
|
|
175
188
|
});
|
|
176
189
|
}
|
|
177
190
|
}
|
|
178
191
|
|
|
192
|
+
class RequestHeadersPlugin {
|
|
193
|
+
init(options) {
|
|
194
|
+
options.rootInterceptors ??= [];
|
|
195
|
+
options.rootInterceptors.push((interceptorOptions) => {
|
|
196
|
+
const reqHeaders = interceptorOptions.context.reqHeaders ?? toFetchHeaders(interceptorOptions.request.headers);
|
|
197
|
+
return interceptorOptions.next({
|
|
198
|
+
...interceptorOptions,
|
|
199
|
+
context: {
|
|
200
|
+
...interceptorOptions.context,
|
|
201
|
+
reqHeaders
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
179
208
|
class ResponseHeadersPlugin {
|
|
180
209
|
init(options) {
|
|
181
210
|
options.rootInterceptors ??= [];
|
|
@@ -191,7 +220,7 @@ class ResponseHeadersPlugin {
|
|
|
191
220
|
if (!result.matched) {
|
|
192
221
|
return result;
|
|
193
222
|
}
|
|
194
|
-
const responseHeaders = result.response.headers;
|
|
223
|
+
const responseHeaders = clone(result.response.headers);
|
|
195
224
|
for (const [key, value] of resHeaders) {
|
|
196
225
|
if (Array.isArray(responseHeaders[key])) {
|
|
197
226
|
responseHeaders[key].push(value);
|
|
@@ -201,8 +230,55 @@ class ResponseHeadersPlugin {
|
|
|
201
230
|
responseHeaders[key] = value;
|
|
202
231
|
}
|
|
203
232
|
}
|
|
233
|
+
return {
|
|
234
|
+
...result,
|
|
235
|
+
response: {
|
|
236
|
+
...result.response,
|
|
237
|
+
headers: responseHeaders
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
class experimental_RethrowHandlerPlugin {
|
|
245
|
+
filter;
|
|
246
|
+
CONTEXT_SYMBOL = Symbol("ORPC_RETHROW_HANDLER_PLUGIN_CONTEXT");
|
|
247
|
+
constructor(options) {
|
|
248
|
+
this.filter = options.filter;
|
|
249
|
+
}
|
|
250
|
+
init(options) {
|
|
251
|
+
options.rootInterceptors ??= [];
|
|
252
|
+
options.interceptors ??= [];
|
|
253
|
+
options.rootInterceptors.push(async (options2) => {
|
|
254
|
+
const pluginContext = {};
|
|
255
|
+
const result = await options2.next({
|
|
256
|
+
...options2,
|
|
257
|
+
context: {
|
|
258
|
+
...options2.context,
|
|
259
|
+
[this.CONTEXT_SYMBOL]: pluginContext
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
if (pluginContext.error) {
|
|
263
|
+
throw pluginContext.error.value;
|
|
264
|
+
}
|
|
204
265
|
return result;
|
|
205
266
|
});
|
|
267
|
+
options.interceptors.unshift(async (options2) => {
|
|
268
|
+
const pluginContext = options2.context[this.CONTEXT_SYMBOL];
|
|
269
|
+
if (!pluginContext) {
|
|
270
|
+
throw new TypeError("[RethrowHandlerPlugin] Rethrow handler context has been corrupted or modified by another plugin or interceptor");
|
|
271
|
+
}
|
|
272
|
+
try {
|
|
273
|
+
return await options2.next();
|
|
274
|
+
} catch (error) {
|
|
275
|
+
if (value(this.filter, error, options2)) {
|
|
276
|
+
pluginContext.error = { value: error };
|
|
277
|
+
return { matched: false, response: void 0 };
|
|
278
|
+
}
|
|
279
|
+
throw error;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
206
282
|
}
|
|
207
283
|
}
|
|
208
284
|
|
|
@@ -249,4 +325,4 @@ class SimpleCsrfProtectionHandlerPlugin {
|
|
|
249
325
|
}
|
|
250
326
|
}
|
|
251
327
|
|
|
252
|
-
export { BatchHandlerPlugin, CORSPlugin, ResponseHeadersPlugin, SimpleCsrfProtectionHandlerPlugin };
|
|
328
|
+
export { BatchHandlerPlugin, CORSPlugin, RequestHeadersPlugin, ResponseHeadersPlugin, SimpleCsrfProtectionHandlerPlugin, experimental_RethrowHandlerPlugin };
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import { HTTPPath, ORPCError } from '@orpc/client';
|
|
2
|
-
import { Meta
|
|
3
|
-
import { Interceptor
|
|
2
|
+
import { Meta } from '@orpc/contract';
|
|
3
|
+
import { Interceptor } from '@orpc/shared';
|
|
4
4
|
import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
|
|
5
|
-
import {
|
|
5
|
+
import { C as Context, R as Router, b as AnyRouter, A as AnyProcedure, d as ProcedureClientInterceptorOptions } from './server.qKsRrdxW.js';
|
|
6
|
+
|
|
7
|
+
interface StandardHandlerPlugin<T extends Context> {
|
|
8
|
+
order?: number;
|
|
9
|
+
init?(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
|
10
|
+
}
|
|
11
|
+
declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
|
|
12
|
+
protected readonly plugins: TPlugin[];
|
|
13
|
+
constructor(plugins?: readonly TPlugin[]);
|
|
14
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
|
15
|
+
}
|
|
6
16
|
|
|
7
17
|
type StandardParams = Record<string, string>;
|
|
8
18
|
type StandardMatchResult = {
|
|
@@ -20,16 +30,6 @@ interface StandardCodec {
|
|
|
20
30
|
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
|
|
21
31
|
}
|
|
22
32
|
|
|
23
|
-
interface StandardHandlerPlugin<TContext extends Context> {
|
|
24
|
-
order?: number;
|
|
25
|
-
init?(options: StandardHandlerOptions<TContext>): void;
|
|
26
|
-
}
|
|
27
|
-
declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
|
|
28
|
-
protected readonly plugins: TPlugin[];
|
|
29
|
-
constructor(plugins?: readonly TPlugin[]);
|
|
30
|
-
init(options: StandardHandlerOptions<T>): void;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
33
|
interface StandardHandleOptions<T extends Context> {
|
|
34
34
|
prefix?: HTTPPath;
|
|
35
35
|
context: T;
|
|
@@ -49,16 +49,16 @@ interface StandardHandlerOptions<TContext extends Context> {
|
|
|
49
49
|
/**
|
|
50
50
|
* Interceptors at the request level, helpful when you want catch errors
|
|
51
51
|
*/
|
|
52
|
-
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult
|
|
52
|
+
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, Promise<StandardHandleResult>>[];
|
|
53
53
|
/**
|
|
54
54
|
* Interceptors at the root level, helpful when you want override the request/response
|
|
55
55
|
*/
|
|
56
|
-
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult
|
|
56
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, Promise<StandardHandleResult>>[];
|
|
57
57
|
/**
|
|
58
58
|
*
|
|
59
59
|
* Interceptors for procedure client.
|
|
60
60
|
*/
|
|
61
|
-
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>,
|
|
61
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, Promise<unknown>>[];
|
|
62
62
|
}
|
|
63
63
|
declare class StandardHandler<T extends Context> {
|
|
64
64
|
private readonly matcher;
|
|
@@ -70,4 +70,5 @@ declare class StandardHandler<T extends Context> {
|
|
|
70
70
|
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
export { CompositeStandardHandlerPlugin as C,
|
|
73
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as f };
|
|
74
|
+
export type { StandardCodec as S, StandardParams as a, StandardHandleOptions as b, StandardHandleResult as c, StandardHandlerInterceptorOptions as d, StandardHandlerOptions as e, StandardHandlerPlugin as g, StandardMatchResult as h, StandardMatcher as i };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { C as Context } from './server.qKsRrdxW.mjs';
|
|
2
|
+
import { b as StandardHandleOptions } from './server.BqadksTP.mjs';
|
|
3
|
+
|
|
4
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
|
5
|
+
context?: T;
|
|
6
|
+
} : {
|
|
7
|
+
context: T;
|
|
8
|
+
});
|
|
9
|
+
declare function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T>;
|
|
10
|
+
|
|
11
|
+
export { resolveFriendlyStandardHandleOptions as r };
|
|
12
|
+
export type { FriendlyStandardHandleOptions as F };
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import { HTTPPath, ORPCError } from '@orpc/client';
|
|
2
|
-
import { Meta
|
|
3
|
-
import { Interceptor
|
|
2
|
+
import { Meta } from '@orpc/contract';
|
|
3
|
+
import { Interceptor } from '@orpc/shared';
|
|
4
4
|
import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
|
|
5
|
-
import {
|
|
5
|
+
import { C as Context, R as Router, b as AnyRouter, A as AnyProcedure, d as ProcedureClientInterceptorOptions } from './server.qKsRrdxW.mjs';
|
|
6
|
+
|
|
7
|
+
interface StandardHandlerPlugin<T extends Context> {
|
|
8
|
+
order?: number;
|
|
9
|
+
init?(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
|
10
|
+
}
|
|
11
|
+
declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
|
|
12
|
+
protected readonly plugins: TPlugin[];
|
|
13
|
+
constructor(plugins?: readonly TPlugin[]);
|
|
14
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
|
15
|
+
}
|
|
6
16
|
|
|
7
17
|
type StandardParams = Record<string, string>;
|
|
8
18
|
type StandardMatchResult = {
|
|
@@ -20,16 +30,6 @@ interface StandardCodec {
|
|
|
20
30
|
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
|
|
21
31
|
}
|
|
22
32
|
|
|
23
|
-
interface StandardHandlerPlugin<TContext extends Context> {
|
|
24
|
-
order?: number;
|
|
25
|
-
init?(options: StandardHandlerOptions<TContext>): void;
|
|
26
|
-
}
|
|
27
|
-
declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
|
|
28
|
-
protected readonly plugins: TPlugin[];
|
|
29
|
-
constructor(plugins?: readonly TPlugin[]);
|
|
30
|
-
init(options: StandardHandlerOptions<T>): void;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
33
|
interface StandardHandleOptions<T extends Context> {
|
|
34
34
|
prefix?: HTTPPath;
|
|
35
35
|
context: T;
|
|
@@ -49,16 +49,16 @@ interface StandardHandlerOptions<TContext extends Context> {
|
|
|
49
49
|
/**
|
|
50
50
|
* Interceptors at the request level, helpful when you want catch errors
|
|
51
51
|
*/
|
|
52
|
-
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult
|
|
52
|
+
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, Promise<StandardHandleResult>>[];
|
|
53
53
|
/**
|
|
54
54
|
* Interceptors at the root level, helpful when you want override the request/response
|
|
55
55
|
*/
|
|
56
|
-
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult
|
|
56
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, Promise<StandardHandleResult>>[];
|
|
57
57
|
/**
|
|
58
58
|
*
|
|
59
59
|
* Interceptors for procedure client.
|
|
60
60
|
*/
|
|
61
|
-
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>,
|
|
61
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, Promise<unknown>>[];
|
|
62
62
|
}
|
|
63
63
|
declare class StandardHandler<T extends Context> {
|
|
64
64
|
private readonly matcher;
|
|
@@ -70,4 +70,5 @@ declare class StandardHandler<T extends Context> {
|
|
|
70
70
|
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
export { CompositeStandardHandlerPlugin as C,
|
|
73
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as f };
|
|
74
|
+
export type { StandardCodec as S, StandardParams as a, StandardHandleOptions as b, StandardHandleResult as c, StandardHandlerInterceptorOptions as d, StandardHandlerOptions as e, StandardHandlerPlugin as g, StandardMatchResult as h, StandardMatcher as i };
|