@orpc/server 0.0.0-next.f4d410a → 0.0.0-next.f538070
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 +11 -0
- package/dist/adapters/fetch/index.d.mts +41 -11
- package/dist/adapters/fetch/index.d.ts +41 -11
- package/dist/adapters/fetch/index.mjs +8 -6
- package/dist/adapters/hono/index.d.mts +6 -5
- package/dist/adapters/hono/index.d.ts +6 -5
- package/dist/adapters/hono/index.mjs +12 -9
- package/dist/adapters/next/index.d.mts +6 -5
- package/dist/adapters/next/index.d.ts +6 -5
- package/dist/adapters/next/index.mjs +12 -9
- package/dist/adapters/node/index.d.mts +43 -22
- package/dist/adapters/node/index.d.ts +43 -22
- package/dist/adapters/node/index.mjs +82 -24
- package/dist/adapters/standard/index.d.mts +6 -6
- package/dist/adapters/standard/index.d.ts +6 -6
- package/dist/adapters/standard/index.mjs +6 -4
- package/dist/index.d.mts +56 -43
- package/dist/index.d.ts +56 -43
- package/dist/index.mjs +32 -11
- package/dist/plugins/index.d.mts +108 -15
- package/dist/plugins/index.d.ts +108 -15
- package/dist/plugins/index.mjs +147 -6
- package/dist/shared/server.BVwwTHyO.mjs +9 -0
- package/dist/shared/server.BW-nUGgA.mjs +36 -0
- package/dist/shared/server.Bm0UqHzd.mjs +103 -0
- package/dist/shared/{server.BtxZnWJ9.mjs → server.C37gDhSZ.mjs} +19 -29
- package/dist/shared/server.C8NkqxHo.d.ts +17 -0
- package/dist/shared/server.CGCwEAt_.d.mts +10 -0
- package/dist/shared/server.DCQgF_JR.d.mts +17 -0
- package/dist/shared/{server.B3Tm0IXY.d.ts → server.DFFT_EZo.d.ts} +27 -29
- package/dist/shared/{server.jG7ZuX3S.mjs → server.DFuJLDuo.mjs} +60 -28
- package/dist/shared/{server.BYTulgUc.d.mts → server.DLt5njUb.d.mts} +9 -10
- package/dist/shared/{server.BYTulgUc.d.ts → server.DLt5njUb.d.ts} +9 -10
- package/dist/shared/{server.BeJithK4.d.mts → server.DOYDVeMX.d.mts} +27 -29
- package/dist/shared/server._2UufoXA.d.ts +10 -0
- package/package.json +8 -8
- package/dist/shared/server.Bz_xNBjz.d.mts +0 -8
- package/dist/shared/server.DoP20NVH.mjs +0 -29
- package/dist/shared/server.EhgR_5_I.d.ts +0 -8
- package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
package/dist/plugins/index.d.ts
CHANGED
@@ -1,31 +1,124 @@
|
|
1
|
-
import { b as StandardHandlerInterceptorOptions, H as HandlerPlugin, a as StandardHandlerOptions } from '../shared/server.B3Tm0IXY.js';
|
2
|
-
export { C as CompositePlugin } from '../shared/server.B3Tm0IXY.js';
|
3
1
|
import { Value } from '@orpc/shared';
|
4
|
-
import {
|
5
|
-
import '@orpc/
|
6
|
-
import '
|
7
|
-
import '
|
2
|
+
import { StandardRequest, StandardHeaders } from '@orpc/standard-server';
|
3
|
+
import { BatchResponseBodyItem } from '@orpc/standard-server/batch';
|
4
|
+
import { h as StandardHandlerInterceptorOptions, i as StandardHandlerPlugin, a as StandardHandlerOptions } from '../shared/server.DFFT_EZo.js';
|
5
|
+
import { C as Context, P as ProcedureClientInterceptorOptions } from '../shared/server.DLt5njUb.js';
|
6
|
+
import { Meta, ORPCError as ORPCError$1 } from '@orpc/contract';
|
7
|
+
import { ORPCError } from '@orpc/client';
|
8
8
|
|
9
|
-
interface
|
10
|
-
|
11
|
-
|
9
|
+
interface BatchHandlerOptions<T extends Context> {
|
10
|
+
/**
|
11
|
+
* The max size of the batch allowed.
|
12
|
+
*
|
13
|
+
* @default 10
|
14
|
+
*/
|
15
|
+
maxSize?: Value<number, [StandardHandlerInterceptorOptions<T>]>;
|
16
|
+
/**
|
17
|
+
* Map the request before processing it.
|
18
|
+
*
|
19
|
+
* @default merged back batch request headers into the request
|
20
|
+
*/
|
21
|
+
mapRequestItem?(request: StandardRequest, batchOptions: StandardHandlerInterceptorOptions<T>): StandardRequest;
|
22
|
+
/**
|
23
|
+
* Success batch response status code.
|
24
|
+
*
|
25
|
+
* @default 207
|
26
|
+
*/
|
27
|
+
successStatus?: Value<number, [responses: Promise<BatchResponseBodyItem>[], batchOptions: StandardHandlerInterceptorOptions<T>]>;
|
28
|
+
/**
|
29
|
+
* success batch response headers.
|
30
|
+
*
|
31
|
+
* @default {}
|
32
|
+
*/
|
33
|
+
headers?: Value<StandardHeaders, [responses: Promise<BatchResponseBodyItem>[], batchOptions: StandardHandlerInterceptorOptions<T>]>;
|
34
|
+
}
|
35
|
+
declare class BatchHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
36
|
+
private readonly maxSize;
|
37
|
+
private readonly mapRequestItem;
|
38
|
+
private readonly successStatus;
|
39
|
+
private readonly headers;
|
40
|
+
order: number;
|
41
|
+
constructor(options?: BatchHandlerOptions<T>);
|
42
|
+
init(options: StandardHandlerOptions<T>): void;
|
43
|
+
}
|
44
|
+
|
45
|
+
interface CORSOptions<T extends Context> {
|
46
|
+
origin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
|
47
|
+
timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
|
12
48
|
allowMethods?: readonly string[];
|
13
49
|
allowHeaders?: readonly string[];
|
14
50
|
maxAge?: number;
|
15
51
|
credentials?: boolean;
|
16
52
|
exposeHeaders?: readonly string[];
|
17
53
|
}
|
18
|
-
declare class CORSPlugin<
|
54
|
+
declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
19
55
|
private readonly options;
|
20
|
-
|
21
|
-
|
56
|
+
order: number;
|
57
|
+
constructor(options?: CORSOptions<T>);
|
58
|
+
init(options: StandardHandlerOptions<T>): void;
|
22
59
|
}
|
23
60
|
|
24
61
|
interface ResponseHeadersPluginContext {
|
25
62
|
resHeaders?: Headers;
|
26
63
|
}
|
27
|
-
declare class ResponseHeadersPlugin<
|
28
|
-
init(options: StandardHandlerOptions<
|
64
|
+
declare class ResponseHeadersPlugin<T extends ResponseHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
65
|
+
init(options: StandardHandlerOptions<T>): void;
|
66
|
+
}
|
67
|
+
|
68
|
+
interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
|
69
|
+
/**
|
70
|
+
* The name of the header to check.
|
71
|
+
*
|
72
|
+
* @default 'x-csrf-token'
|
73
|
+
*/
|
74
|
+
headerName?: Value<string, [options: StandardHandlerInterceptorOptions<T>]>;
|
75
|
+
/**
|
76
|
+
* The value of the header to check.
|
77
|
+
*
|
78
|
+
* @default 'orpc'
|
79
|
+
*
|
80
|
+
*/
|
81
|
+
headerValue?: Value<string, [options: StandardHandlerInterceptorOptions<T>]>;
|
82
|
+
/**
|
83
|
+
* Exclude a procedure from the plugin.
|
84
|
+
*
|
85
|
+
* @default false
|
86
|
+
*
|
87
|
+
*/
|
88
|
+
exclude?: Value<boolean, [options: ProcedureClientInterceptorOptions<T, Record<never, never>, Meta>]>;
|
89
|
+
/**
|
90
|
+
* The error thrown when the CSRF token is invalid.
|
91
|
+
*
|
92
|
+
* @default new ORPCError('CSRF_TOKEN_MISMATCH', {
|
93
|
+
* status: 403,
|
94
|
+
* message: 'Invalid CSRF token',
|
95
|
+
* })
|
96
|
+
*/
|
97
|
+
error?: InstanceType<typeof ORPCError>;
|
98
|
+
}
|
99
|
+
declare class SimpleCsrfProtectionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
100
|
+
private readonly headerName;
|
101
|
+
private readonly headerValue;
|
102
|
+
private readonly exclude;
|
103
|
+
private readonly error;
|
104
|
+
constructor(options?: SimpleCsrfProtectionHandlerPluginOptions<T>);
|
105
|
+
order: number;
|
106
|
+
init(options: StandardHandlerOptions<T>): void;
|
107
|
+
}
|
108
|
+
|
109
|
+
interface StrictGetMethodPluginOptions {
|
110
|
+
/**
|
111
|
+
* The error thrown when a GET request is made to a procedure that doesn't allow GET.
|
112
|
+
*
|
113
|
+
* @default new ORPCError('METHOD_NOT_SUPPORTED')
|
114
|
+
*/
|
115
|
+
error?: InstanceType<typeof ORPCError$1>;
|
116
|
+
}
|
117
|
+
declare class StrictGetMethodPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
118
|
+
private readonly error;
|
119
|
+
order: number;
|
120
|
+
constructor(options?: StrictGetMethodPluginOptions);
|
121
|
+
init(options: StandardHandlerOptions<T>): void;
|
29
122
|
}
|
30
123
|
|
31
|
-
export { type CORSOptions, CORSPlugin,
|
124
|
+
export { type BatchHandlerOptions, BatchHandlerPlugin, type CORSOptions, CORSPlugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext, SimpleCsrfProtectionHandlerPlugin, type SimpleCsrfProtectionHandlerPluginOptions, StrictGetMethodPlugin, type StrictGetMethodPluginOptions };
|
package/dist/plugins/index.mjs
CHANGED
@@ -1,8 +1,101 @@
|
|
1
|
-
export { C as CompositePlugin } from '../shared/server.Q6ZmnTgO.mjs';
|
2
1
|
import { value } from '@orpc/shared';
|
2
|
+
import { parseBatchRequest, toBatchResponse } from '@orpc/standard-server/batch';
|
3
|
+
import { ORPCError } from '@orpc/client';
|
4
|
+
export { S as StrictGetMethodPlugin } from '../shared/server.BW-nUGgA.mjs';
|
5
|
+
import '@orpc/contract';
|
6
|
+
|
7
|
+
class BatchHandlerPlugin {
|
8
|
+
maxSize;
|
9
|
+
mapRequestItem;
|
10
|
+
successStatus;
|
11
|
+
headers;
|
12
|
+
order = 5e6;
|
13
|
+
constructor(options = {}) {
|
14
|
+
this.maxSize = options.maxSize ?? 10;
|
15
|
+
this.mapRequestItem = options.mapRequestItem ?? ((request, { request: batchRequest }) => ({
|
16
|
+
...request,
|
17
|
+
headers: {
|
18
|
+
...batchRequest.headers,
|
19
|
+
...request.headers
|
20
|
+
}
|
21
|
+
}));
|
22
|
+
this.successStatus = options.successStatus ?? 207;
|
23
|
+
this.headers = options.headers ?? {};
|
24
|
+
}
|
25
|
+
init(options) {
|
26
|
+
options.rootInterceptors ??= [];
|
27
|
+
options.rootInterceptors.unshift(async (options2) => {
|
28
|
+
if (options2.request.headers["x-orpc-batch"] !== "1") {
|
29
|
+
return options2.next();
|
30
|
+
}
|
31
|
+
let isParsing = false;
|
32
|
+
try {
|
33
|
+
isParsing = true;
|
34
|
+
const parsed = parseBatchRequest({ ...options2.request, body: await options2.request.body() });
|
35
|
+
isParsing = false;
|
36
|
+
const maxSize = await value(this.maxSize, options2);
|
37
|
+
if (parsed.length > maxSize) {
|
38
|
+
return {
|
39
|
+
matched: true,
|
40
|
+
response: {
|
41
|
+
status: 413,
|
42
|
+
headers: {},
|
43
|
+
body: "Batch request size exceeds the maximum allowed size"
|
44
|
+
}
|
45
|
+
};
|
46
|
+
}
|
47
|
+
const responses = parsed.map(
|
48
|
+
(request, index) => {
|
49
|
+
const mapped = this.mapRequestItem(request, options2);
|
50
|
+
return options2.next({ ...options2, request: { ...mapped, body: () => Promise.resolve(mapped.body) } }).then(({ response: response2, matched }) => {
|
51
|
+
if (matched) {
|
52
|
+
return { ...response2, index };
|
53
|
+
}
|
54
|
+
return { index, status: 404, headers: {}, body: "No procedure matched" };
|
55
|
+
}).catch(() => {
|
56
|
+
return { index, status: 500, headers: {}, body: "Internal server error" };
|
57
|
+
});
|
58
|
+
}
|
59
|
+
);
|
60
|
+
await Promise.race(responses);
|
61
|
+
const status = await value(this.successStatus, responses, options2);
|
62
|
+
const headers = await value(this.headers, responses, options2);
|
63
|
+
const response = toBatchResponse({
|
64
|
+
status,
|
65
|
+
headers,
|
66
|
+
body: async function* () {
|
67
|
+
const promises = [...responses];
|
68
|
+
while (true) {
|
69
|
+
const handling = promises.filter((p) => p !== void 0);
|
70
|
+
if (handling.length === 0) {
|
71
|
+
return;
|
72
|
+
}
|
73
|
+
const result = await Promise.race(handling);
|
74
|
+
promises[result.index] = void 0;
|
75
|
+
yield result;
|
76
|
+
}
|
77
|
+
}()
|
78
|
+
});
|
79
|
+
return {
|
80
|
+
matched: true,
|
81
|
+
response
|
82
|
+
};
|
83
|
+
} catch (cause) {
|
84
|
+
if (isParsing) {
|
85
|
+
return {
|
86
|
+
matched: true,
|
87
|
+
response: { status: 400, headers: {}, body: "Invalid batch request, this could be caused by a malformed request body or a missing header" }
|
88
|
+
};
|
89
|
+
}
|
90
|
+
throw cause;
|
91
|
+
}
|
92
|
+
});
|
93
|
+
}
|
94
|
+
}
|
3
95
|
|
4
96
|
class CORSPlugin {
|
5
97
|
options;
|
98
|
+
order = 9e6;
|
6
99
|
constructor(options = {}) {
|
7
100
|
const defaults = {
|
8
101
|
origin: (origin) => origin,
|
@@ -79,14 +172,19 @@ class ResponseHeadersPlugin {
|
|
79
172
|
init(options) {
|
80
173
|
options.rootInterceptors ??= [];
|
81
174
|
options.rootInterceptors.push(async (interceptorOptions) => {
|
82
|
-
const
|
83
|
-
|
84
|
-
|
175
|
+
const resHeaders = interceptorOptions.context.resHeaders ?? new Headers();
|
176
|
+
const result = await interceptorOptions.next({
|
177
|
+
...interceptorOptions,
|
178
|
+
context: {
|
179
|
+
...interceptorOptions.context,
|
180
|
+
resHeaders
|
181
|
+
}
|
182
|
+
});
|
85
183
|
if (!result.matched) {
|
86
184
|
return result;
|
87
185
|
}
|
88
186
|
const responseHeaders = result.response.headers;
|
89
|
-
for (const [key, value] of
|
187
|
+
for (const [key, value] of resHeaders) {
|
90
188
|
if (Array.isArray(responseHeaders[key])) {
|
91
189
|
responseHeaders[key].push(value);
|
92
190
|
} else if (responseHeaders[key] !== void 0) {
|
@@ -100,4 +198,47 @@ class ResponseHeadersPlugin {
|
|
100
198
|
}
|
101
199
|
}
|
102
200
|
|
103
|
-
|
201
|
+
const SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL = Symbol("SIMPLE_CSRF_PROTECTION_CONTEXT");
|
202
|
+
class SimpleCsrfProtectionHandlerPlugin {
|
203
|
+
headerName;
|
204
|
+
headerValue;
|
205
|
+
exclude;
|
206
|
+
error;
|
207
|
+
constructor(options = {}) {
|
208
|
+
this.headerName = options.headerName ?? "x-csrf-token";
|
209
|
+
this.headerValue = options.headerValue ?? "orpc";
|
210
|
+
this.exclude = options.exclude ?? false;
|
211
|
+
this.error = options.error ?? new ORPCError("CSRF_TOKEN_MISMATCH", {
|
212
|
+
status: 403,
|
213
|
+
message: "Invalid CSRF token"
|
214
|
+
});
|
215
|
+
}
|
216
|
+
order = 8e6;
|
217
|
+
init(options) {
|
218
|
+
options.rootInterceptors ??= [];
|
219
|
+
options.clientInterceptors ??= [];
|
220
|
+
options.rootInterceptors.unshift(async (options2) => {
|
221
|
+
const headerName = await value(this.headerName, options2);
|
222
|
+
const headerValue = await value(this.headerValue, options2);
|
223
|
+
return options2.next({
|
224
|
+
...options2,
|
225
|
+
context: {
|
226
|
+
...options2.context,
|
227
|
+
[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]: options2.request.headers[headerName] === headerValue
|
228
|
+
}
|
229
|
+
});
|
230
|
+
});
|
231
|
+
options.clientInterceptors.unshift(async (options2) => {
|
232
|
+
if (typeof options2.context[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL] !== "boolean") {
|
233
|
+
throw new TypeError("[SimpleCsrfProtectionHandlerPlugin] CSRF protection context has been corrupted or modified by another plugin or interceptor");
|
234
|
+
}
|
235
|
+
const excluded = await value(this.exclude, options2);
|
236
|
+
if (!excluded && !options2.context[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]) {
|
237
|
+
throw this.error;
|
238
|
+
}
|
239
|
+
return options2.next();
|
240
|
+
});
|
241
|
+
}
|
242
|
+
}
|
243
|
+
|
244
|
+
export { BatchHandlerPlugin, CORSPlugin, ResponseHeadersPlugin, SimpleCsrfProtectionHandlerPlugin };
|
@@ -0,0 +1,36 @@
|
|
1
|
+
import { ORPCError, fallbackContractConfig } from '@orpc/contract';
|
2
|
+
|
3
|
+
const STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL = Symbol("STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT");
|
4
|
+
class StrictGetMethodPlugin {
|
5
|
+
error;
|
6
|
+
order = 7e6;
|
7
|
+
constructor(options = {}) {
|
8
|
+
this.error = options.error ?? new ORPCError("METHOD_NOT_SUPPORTED");
|
9
|
+
}
|
10
|
+
init(options) {
|
11
|
+
options.rootInterceptors ??= [];
|
12
|
+
options.clientInterceptors ??= [];
|
13
|
+
options.rootInterceptors.unshift((options2) => {
|
14
|
+
const isGetMethod = options2.request.method === "GET";
|
15
|
+
return options2.next({
|
16
|
+
...options2,
|
17
|
+
context: {
|
18
|
+
...options2.context,
|
19
|
+
[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL]: isGetMethod
|
20
|
+
}
|
21
|
+
});
|
22
|
+
});
|
23
|
+
options.clientInterceptors.unshift((options2) => {
|
24
|
+
if (typeof options2.context[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL] !== "boolean") {
|
25
|
+
throw new TypeError("[StrictGetMethodPlugin] strict GET method context has been corrupted or modified by another plugin or interceptor");
|
26
|
+
}
|
27
|
+
const procedureMethod = fallbackContractConfig("defaultMethod", options2.procedure["~orpc"].route.method);
|
28
|
+
if (options2.context[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL] && procedureMethod !== "GET") {
|
29
|
+
throw this.error;
|
30
|
+
}
|
31
|
+
return options2.next();
|
32
|
+
});
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
export { StrictGetMethodPlugin as S };
|
@@ -0,0 +1,103 @@
|
|
1
|
+
import { ORPCError } from '@orpc/client';
|
2
|
+
import { toArray, intercept, resolveMaybeOptionalOptions } from '@orpc/shared';
|
3
|
+
import '@orpc/contract';
|
4
|
+
import { C as CompositeStandardHandlerPlugin, b as StandardRPCHandler } from './server.DFuJLDuo.mjs';
|
5
|
+
import '@orpc/client/standard';
|
6
|
+
import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
|
7
|
+
import { r as resolveFriendlyStandardHandleOptions } from './server.BVwwTHyO.mjs';
|
8
|
+
import '@orpc/standard-server/batch';
|
9
|
+
|
10
|
+
class BodyLimitPlugin {
|
11
|
+
maxBodySize;
|
12
|
+
constructor(options) {
|
13
|
+
this.maxBodySize = options.maxBodySize;
|
14
|
+
}
|
15
|
+
initRuntimeAdapter(options) {
|
16
|
+
options.adapterInterceptors ??= [];
|
17
|
+
options.adapterInterceptors.push(async (options2) => {
|
18
|
+
if (!options2.request.body) {
|
19
|
+
return options2.next();
|
20
|
+
}
|
21
|
+
let currentBodySize = 0;
|
22
|
+
const rawReader = options2.request.body.getReader();
|
23
|
+
const reader = new ReadableStream({
|
24
|
+
start: async (controller) => {
|
25
|
+
try {
|
26
|
+
if (Number(options2.request.headers.get("content-length")) > this.maxBodySize) {
|
27
|
+
controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
|
28
|
+
return;
|
29
|
+
}
|
30
|
+
while (true) {
|
31
|
+
const { done, value } = await rawReader.read();
|
32
|
+
if (done) {
|
33
|
+
break;
|
34
|
+
}
|
35
|
+
currentBodySize += value.length;
|
36
|
+
if (currentBodySize > this.maxBodySize) {
|
37
|
+
controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
|
38
|
+
break;
|
39
|
+
}
|
40
|
+
controller.enqueue(value);
|
41
|
+
}
|
42
|
+
} finally {
|
43
|
+
controller.close();
|
44
|
+
}
|
45
|
+
}
|
46
|
+
});
|
47
|
+
const requestInit = { body: reader, duplex: "half" };
|
48
|
+
return options2.next({
|
49
|
+
...options2,
|
50
|
+
request: new Request(options2.request, requestInit)
|
51
|
+
});
|
52
|
+
});
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
class CompositeFetchHandlerPlugin extends CompositeStandardHandlerPlugin {
|
57
|
+
initRuntimeAdapter(options) {
|
58
|
+
for (const plugin of this.plugins) {
|
59
|
+
plugin.initRuntimeAdapter?.(options);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
class FetchHandler {
|
65
|
+
constructor(standardHandler, options = {}) {
|
66
|
+
this.standardHandler = standardHandler;
|
67
|
+
const plugin = new CompositeFetchHandlerPlugin(options.plugins);
|
68
|
+
plugin.initRuntimeAdapter(options);
|
69
|
+
this.adapterInterceptors = toArray(options.adapterInterceptors);
|
70
|
+
this.toFetchResponseOptions = options;
|
71
|
+
}
|
72
|
+
toFetchResponseOptions;
|
73
|
+
adapterInterceptors;
|
74
|
+
async handle(request, ...rest) {
|
75
|
+
return intercept(
|
76
|
+
this.adapterInterceptors,
|
77
|
+
{
|
78
|
+
...resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest)),
|
79
|
+
request,
|
80
|
+
toFetchResponseOptions: this.toFetchResponseOptions
|
81
|
+
},
|
82
|
+
async ({ request: request2, toFetchResponseOptions, ...options }) => {
|
83
|
+
const standardRequest = toStandardLazyRequest(request2);
|
84
|
+
const result = await this.standardHandler.handle(standardRequest, options);
|
85
|
+
if (!result.matched) {
|
86
|
+
return result;
|
87
|
+
}
|
88
|
+
return {
|
89
|
+
matched: true,
|
90
|
+
response: toFetchResponse(result.response, toFetchResponseOptions)
|
91
|
+
};
|
92
|
+
}
|
93
|
+
);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
class RPCHandler extends FetchHandler {
|
98
|
+
constructor(router, options = {}) {
|
99
|
+
super(new StandardRPCHandler(router, options), options);
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
export { BodyLimitPlugin as B, CompositeFetchHandlerPlugin as C, FetchHandler as F, RPCHandler as R };
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, enhanceRoute } from '@orpc/contract';
|
2
2
|
import { fallbackORPCErrorStatus, ORPCError } from '@orpc/client';
|
3
|
-
import { value, intercept
|
3
|
+
import { value, intercept } from '@orpc/shared';
|
4
4
|
|
5
5
|
const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
|
6
6
|
function lazy(loader, meta = {}) {
|
@@ -127,7 +127,7 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
|
|
127
127
|
);
|
128
128
|
} catch (e) {
|
129
129
|
if (!(e instanceof ORPCError)) {
|
130
|
-
throw
|
130
|
+
throw e;
|
131
131
|
}
|
132
132
|
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
133
133
|
throw validated;
|
@@ -169,35 +169,29 @@ async function executeProcedureInternal(procedure, options) {
|
|
169
169
|
const middlewares = procedure["~orpc"].middlewares;
|
170
170
|
const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
|
171
171
|
const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
|
172
|
-
|
173
|
-
|
174
|
-
let currentInput = options.input;
|
175
|
-
const next = async (...[nextOptions]) => {
|
176
|
-
const index = currentIndex;
|
177
|
-
const midContext = nextOptions?.context ?? {};
|
178
|
-
currentIndex += 1;
|
179
|
-
currentContext = mergeCurrentContext(currentContext, midContext);
|
172
|
+
const next = async (index, context, input) => {
|
173
|
+
let currentInput = input;
|
180
174
|
if (index === inputValidationIndex) {
|
181
175
|
currentInput = await validateInput(procedure, currentInput);
|
182
176
|
}
|
183
177
|
const mid = middlewares[index];
|
184
|
-
const
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
178
|
+
const output = mid ? (await mid({
|
179
|
+
...options,
|
180
|
+
context,
|
181
|
+
next: async (...[nextOptions]) => {
|
182
|
+
const nextContext = nextOptions?.context ?? {};
|
183
|
+
return {
|
184
|
+
output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
|
185
|
+
context: nextContext
|
186
|
+
};
|
187
|
+
}
|
188
|
+
}, currentInput, middlewareOutputFn)).output : await procedure["~orpc"].handler({ ...options, context, input: currentInput });
|
191
189
|
if (index === outputValidationIndex) {
|
192
|
-
|
193
|
-
return {
|
194
|
-
context: result.context,
|
195
|
-
output: validatedOutput
|
196
|
-
};
|
190
|
+
return await validateOutput(procedure, output);
|
197
191
|
}
|
198
|
-
return
|
192
|
+
return output;
|
199
193
|
};
|
200
|
-
return (
|
194
|
+
return next(0, options.context, options.input);
|
201
195
|
}
|
202
196
|
|
203
197
|
const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
|
@@ -367,8 +361,4 @@ function call(procedure, input, ...rest) {
|
|
367
361
|
return createProcedureClient(procedure, ...rest)(input);
|
368
362
|
}
|
369
363
|
|
370
|
-
|
371
|
-
return `/${path.map(encodeURIComponent).join("/")}`;
|
372
|
-
}
|
373
|
-
|
374
|
-
export { LAZY_SYMBOL as L, Procedure as P, toHttpPath as a, createContractedProcedure as b, createProcedureClient as c, addMiddleware as d, enhanceRouter as e, isLazy as f, getRouter as g, createAssertedLazyProcedure as h, isProcedure as i, createORPCErrorConstructorMap as j, getLazyMeta as k, lazy as l, mergeCurrentContext as m, middlewareOutputFn as n, isStartWithMiddlewares as o, mergeMiddlewares as p, call as q, getHiddenRouterContract as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, createAccessibleLazyRouter as w, resolveContractProcedures as x, unlazyRouter as y };
|
364
|
+
export { LAZY_SYMBOL as L, Procedure as P, createContractedProcedure as a, addMiddleware as b, createProcedureClient as c, isLazy as d, enhanceRouter as e, createAssertedLazyProcedure as f, getRouter as g, createORPCErrorConstructorMap as h, isProcedure as i, getLazyMeta as j, middlewareOutputFn as k, lazy as l, mergeCurrentContext as m, isStartWithMiddlewares as n, mergeMiddlewares as o, call as p, getHiddenRouterContract as q, createAccessibleLazyRouter as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, resolveContractProcedures as w, unlazyRouter as x };
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { C as Context, R as Router } from './server.DLt5njUb.js';
|
2
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
3
|
+
import { a as StandardHandlerOptions, b as StandardHandler } from './server.DFFT_EZo.js';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
/**
|
7
|
+
* Enables or disables the StrictGetMethodPlugin.
|
8
|
+
*
|
9
|
+
* @default true
|
10
|
+
*/
|
11
|
+
strictGetMethodPluginEnabled?: boolean;
|
12
|
+
}
|
13
|
+
declare class StandardRPCHandler<T extends Context> extends StandardHandler<T> {
|
14
|
+
constructor(router: Router<any, T>, options: StandardRPCHandlerOptions<T>);
|
15
|
+
}
|
16
|
+
|
17
|
+
export { type StandardRPCHandlerOptions as S, StandardRPCHandler as a };
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { C as Context } from './server.DLt5njUb.mjs';
|
2
|
+
import { S as StandardHandleOptions } from './server.DOYDVeMX.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
|
+
|
10
|
+
export type { FriendlyStandardHandleOptions as F };
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { C as Context, R as Router } from './server.DLt5njUb.mjs';
|
2
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
3
|
+
import { a as StandardHandlerOptions, b as StandardHandler } from './server.DOYDVeMX.mjs';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
/**
|
7
|
+
* Enables or disables the StrictGetMethodPlugin.
|
8
|
+
*
|
9
|
+
* @default true
|
10
|
+
*/
|
11
|
+
strictGetMethodPluginEnabled?: boolean;
|
12
|
+
}
|
13
|
+
declare class StandardRPCHandler<T extends Context> extends StandardHandler<T> {
|
14
|
+
constructor(router: Router<any, T>, options: StandardRPCHandlerOptions<T>);
|
15
|
+
}
|
16
|
+
|
17
|
+
export { type StandardRPCHandlerOptions as S, StandardRPCHandler as a };
|