@orpc/server 0.0.0-next.c7f8b1d → 0.0.0-next.c854c60
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 +17 -22
- package/dist/adapters/aws-lambda/index.d.mts +45 -0
- package/dist/adapters/aws-lambda/index.d.ts +45 -0
- package/dist/adapters/aws-lambda/index.mjs +41 -0
- package/dist/adapters/bun-ws/index.d.mts +35 -0
- package/dist/adapters/bun-ws/index.d.ts +35 -0
- package/dist/adapters/bun-ws/index.mjs +46 -0
- package/dist/adapters/crossws/index.d.mts +30 -0
- package/dist/adapters/crossws/index.d.ts +30 -0
- package/dist/adapters/crossws/index.mjs +46 -0
- package/dist/adapters/fetch/index.d.mts +27 -8
- package/dist/adapters/fetch/index.d.ts +27 -8
- package/dist/adapters/fetch/index.mjs +9 -4
- package/dist/adapters/message-port/index.d.mts +28 -0
- package/dist/adapters/message-port/index.d.ts +28 -0
- package/dist/adapters/message-port/index.mjs +41 -0
- package/dist/adapters/node/index.d.mts +27 -8
- package/dist/adapters/node/index.d.ts +27 -8
- package/dist/adapters/node/index.mjs +9 -4
- package/dist/adapters/standard/index.d.mts +4 -4
- package/dist/adapters/standard/index.d.ts +4 -4
- package/dist/adapters/standard/index.mjs +4 -5
- package/dist/adapters/websocket/index.d.mts +27 -0
- package/dist/adapters/websocket/index.d.ts +27 -0
- package/dist/adapters/websocket/index.mjs +39 -0
- package/dist/adapters/ws/index.d.mts +28 -0
- package/dist/adapters/ws/index.d.ts +28 -0
- package/dist/adapters/ws/index.mjs +39 -0
- package/dist/index.d.mts +553 -11
- package/dist/index.d.ts +553 -11
- package/dist/index.mjs +136 -11
- package/dist/plugins/index.d.mts +42 -11
- package/dist/plugins/index.d.ts +42 -11
- package/dist/plugins/index.mjs +10 -9
- package/dist/shared/{server.CaWivVk3.d.ts → server.-ACo36I0.d.ts} +9 -9
- package/dist/shared/{server.D_vpYits.d.ts → server.BPAWobQg.d.ts} +3 -9
- package/dist/shared/{server.BuLPHTX1.d.mts → server.Bd52nNaH.d.mts} +3 -9
- package/dist/shared/{server.Dwnm6cSk.d.mts → server.BliFSTnG.d.mts} +2 -2
- package/dist/shared/{server.DFuJLDuo.mjs → server.CHvOkcM3.mjs} +12 -16
- package/dist/shared/{server.BVHsfJ99.d.mts → server.DD2C4ujN.d.mts} +55 -7
- package/dist/shared/{server.BVHsfJ99.d.ts → server.DD2C4ujN.d.ts} +55 -7
- package/dist/shared/{server.C37gDhSZ.mjs → server.DG7Tamti.mjs} +3 -0
- package/dist/shared/{server.BVwwTHyO.mjs → server.DZ5BIITo.mjs} +1 -1
- package/dist/shared/{server.B1oIHH_j.d.mts → server.Dq8xr7PQ.d.mts} +9 -9
- package/dist/shared/{server.DMhSfHk1.d.ts → server.IG2MjhrD.d.ts} +2 -2
- package/package.json +56 -8
package/dist/index.mjs
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
import { mergeErrorMap, mergeMeta, mergeRoute, mergePrefix, mergeTags, isContractProcedure, getContractRouter } from '@orpc/contract';
|
1
|
+
import { mergeErrorMap, mergeMeta, mergeRoute, mergePrefix, mergeTags, isContractProcedure, getContractRouter, fallbackContractConfig } from '@orpc/contract';
|
2
2
|
export { ValidationError, eventIterator, type } from '@orpc/contract';
|
3
|
-
import { P as Procedure, b as addMiddleware, c as createProcedureClient, e as enhanceRouter, l as lazy, s as setHiddenRouterContract, i as isProcedure, d as isLazy, f as createAssertedLazyProcedure
|
4
|
-
export { L as LAZY_SYMBOL, p as call, r as createAccessibleLazyRouter, a as createContractedProcedure, h as createORPCErrorConstructorMap, q as getHiddenRouterContract, j as getLazyMeta, n as isStartWithMiddlewares, m as mergeCurrentContext, o as mergeMiddlewares, k as middlewareOutputFn, w as resolveContractProcedures, t as traverseContractProcedures,
|
3
|
+
import { P as Procedure, b as addMiddleware, c as createProcedureClient, e as enhanceRouter, l as lazy, s as setHiddenRouterContract, u as unlazy, g as getRouter, i as isProcedure, d as isLazy, f as createAssertedLazyProcedure } from './shared/server.DG7Tamti.mjs';
|
4
|
+
export { L as LAZY_SYMBOL, p as call, r as createAccessibleLazyRouter, a as createContractedProcedure, h as createORPCErrorConstructorMap, q as getHiddenRouterContract, j as getLazyMeta, n as isStartWithMiddlewares, m as mergeCurrentContext, o as mergeMiddlewares, k as middlewareOutputFn, w as resolveContractProcedures, t as traverseContractProcedures, x as unlazyRouter, v as validateORPCError } from './shared/server.DG7Tamti.mjs';
|
5
5
|
import { toORPCError } from '@orpc/client';
|
6
6
|
export { ORPCError, isDefinedError, safe } from '@orpc/client';
|
7
|
-
|
7
|
+
import { resolveMaybeOptionalOptions } from '@orpc/shared';
|
8
|
+
export { EventPublisher, onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
8
9
|
export { getEventMeta, withEventMeta } from '@orpc/standard-server';
|
9
10
|
|
10
11
|
const DEFAULT_CONFIG = {
|
@@ -50,6 +51,9 @@ function createActionableClient(client) {
|
|
50
51
|
try {
|
51
52
|
return [null, await client(input)];
|
52
53
|
} catch (error) {
|
54
|
+
if (error instanceof Error && "digest" in error && typeof error.digest === "string" && error.digest.startsWith("NEXT_")) {
|
55
|
+
throw error;
|
56
|
+
}
|
53
57
|
return [toORPCError(error).toJSON(), void 0];
|
54
58
|
}
|
55
59
|
};
|
@@ -57,18 +61,38 @@ function createActionableClient(client) {
|
|
57
61
|
}
|
58
62
|
|
59
63
|
class DecoratedProcedure extends Procedure {
|
64
|
+
/**
|
65
|
+
* Adds type-safe custom errors.
|
66
|
+
* The provided errors are spared-merged with any existing errors.
|
67
|
+
*
|
68
|
+
* @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
69
|
+
*/
|
60
70
|
errors(errors) {
|
61
71
|
return new DecoratedProcedure({
|
62
72
|
...this["~orpc"],
|
63
73
|
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
64
74
|
});
|
65
75
|
}
|
76
|
+
/**
|
77
|
+
* Sets or updates the metadata.
|
78
|
+
* The provided metadata is spared-merged with any existing metadata.
|
79
|
+
*
|
80
|
+
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
81
|
+
*/
|
66
82
|
meta(meta) {
|
67
83
|
return new DecoratedProcedure({
|
68
84
|
...this["~orpc"],
|
69
85
|
meta: mergeMeta(this["~orpc"].meta, meta)
|
70
86
|
});
|
71
87
|
}
|
88
|
+
/**
|
89
|
+
* Sets or updates the route definition.
|
90
|
+
* The provided route is spared-merged with any existing route.
|
91
|
+
* This option is typically relevant when integrating with OpenAPI.
|
92
|
+
*
|
93
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
94
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
95
|
+
*/
|
72
96
|
route(route) {
|
73
97
|
return new DecoratedProcedure({
|
74
98
|
...this["~orpc"],
|
@@ -84,6 +108,8 @@ class DecoratedProcedure extends Procedure {
|
|
84
108
|
}
|
85
109
|
/**
|
86
110
|
* Make this procedure callable (works like a function while still being a procedure).
|
111
|
+
*
|
112
|
+
* @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
|
87
113
|
*/
|
88
114
|
callable(...rest) {
|
89
115
|
const client = createProcedureClient(this, ...rest);
|
@@ -98,6 +124,8 @@ class DecoratedProcedure extends Procedure {
|
|
98
124
|
}
|
99
125
|
/**
|
100
126
|
* Make this procedure compatible with server action.
|
127
|
+
*
|
128
|
+
* @see {@link https://orpc.unnoq.com/docs/server-action Server Action Docs}
|
101
129
|
*/
|
102
130
|
actionable(...rest) {
|
103
131
|
const action = createActionableClient(createProcedureClient(this, ...rest));
|
@@ -113,12 +141,18 @@ class DecoratedProcedure extends Procedure {
|
|
113
141
|
}
|
114
142
|
|
115
143
|
class Builder {
|
144
|
+
/**
|
145
|
+
* This property holds the defined options.
|
146
|
+
*/
|
116
147
|
"~orpc";
|
117
148
|
constructor(def) {
|
118
149
|
this["~orpc"] = def;
|
119
150
|
}
|
120
151
|
/**
|
121
|
-
*
|
152
|
+
* Sets or overrides the config.
|
153
|
+
*
|
154
|
+
* @see {@link https://orpc.unnoq.com/docs/lifecycle#middlewares-order Middlewares Order Docs}
|
155
|
+
* @see {@link https://orpc.unnoq.com/docs/best-practices/dedupe-middleware#configuration Dedupe Middleware Docs}
|
122
156
|
*/
|
123
157
|
$config(config) {
|
124
158
|
const inputValidationCount = this["~orpc"].inputValidationIndex - fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex);
|
@@ -132,7 +166,9 @@ class Builder {
|
|
132
166
|
});
|
133
167
|
}
|
134
168
|
/**
|
135
|
-
*
|
169
|
+
* Set or override the initial context.
|
170
|
+
*
|
171
|
+
* @see {@link https://orpc.unnoq.com/docs/context Context Docs}
|
136
172
|
*/
|
137
173
|
$context() {
|
138
174
|
return new Builder({
|
@@ -143,7 +179,9 @@ class Builder {
|
|
143
179
|
});
|
144
180
|
}
|
145
181
|
/**
|
146
|
-
*
|
182
|
+
* Sets or overrides the initial meta.
|
183
|
+
*
|
184
|
+
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
147
185
|
*/
|
148
186
|
$meta(initialMeta) {
|
149
187
|
return new Builder({
|
@@ -152,7 +190,11 @@ class Builder {
|
|
152
190
|
});
|
153
191
|
}
|
154
192
|
/**
|
155
|
-
*
|
193
|
+
* Sets or overrides the initial route.
|
194
|
+
* This option is typically relevant when integrating with OpenAPI.
|
195
|
+
*
|
196
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
197
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
156
198
|
*/
|
157
199
|
$route(initialRoute) {
|
158
200
|
return new Builder({
|
@@ -160,15 +202,31 @@ class Builder {
|
|
160
202
|
route: initialRoute
|
161
203
|
});
|
162
204
|
}
|
205
|
+
/**
|
206
|
+
* Sets or overrides the initial input schema.
|
207
|
+
*
|
208
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure#initial-configuration Initial Procedure Configuration Docs}
|
209
|
+
*/
|
163
210
|
$input(initialInputSchema) {
|
164
211
|
return new Builder({
|
165
212
|
...this["~orpc"],
|
166
213
|
inputSchema: initialInputSchema
|
167
214
|
});
|
168
215
|
}
|
216
|
+
/**
|
217
|
+
* Creates a middleware.
|
218
|
+
*
|
219
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
220
|
+
*/
|
169
221
|
middleware(middleware) {
|
170
222
|
return decorateMiddleware(middleware);
|
171
223
|
}
|
224
|
+
/**
|
225
|
+
* Adds type-safe custom errors.
|
226
|
+
* The provided errors are spared-merged with any existing errors.
|
227
|
+
*
|
228
|
+
* @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
229
|
+
*/
|
172
230
|
errors(errors) {
|
173
231
|
return new Builder({
|
174
232
|
...this["~orpc"],
|
@@ -182,18 +240,37 @@ class Builder {
|
|
182
240
|
middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
|
183
241
|
});
|
184
242
|
}
|
243
|
+
/**
|
244
|
+
* Sets or updates the metadata.
|
245
|
+
* The provided metadata is spared-merged with any existing metadata.
|
246
|
+
*
|
247
|
+
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
248
|
+
*/
|
185
249
|
meta(meta) {
|
186
250
|
return new Builder({
|
187
251
|
...this["~orpc"],
|
188
252
|
meta: mergeMeta(this["~orpc"].meta, meta)
|
189
253
|
});
|
190
254
|
}
|
255
|
+
/**
|
256
|
+
* Sets or updates the route definition.
|
257
|
+
* The provided route is spared-merged with any existing route.
|
258
|
+
* This option is typically relevant when integrating with OpenAPI.
|
259
|
+
*
|
260
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
261
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
262
|
+
*/
|
191
263
|
route(route) {
|
192
264
|
return new Builder({
|
193
265
|
...this["~orpc"],
|
194
266
|
route: mergeRoute(this["~orpc"].route, route)
|
195
267
|
});
|
196
268
|
}
|
269
|
+
/**
|
270
|
+
* Defines the input validation schema.
|
271
|
+
*
|
272
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Input Validation Docs}
|
273
|
+
*/
|
197
274
|
input(schema) {
|
198
275
|
return new Builder({
|
199
276
|
...this["~orpc"],
|
@@ -201,6 +278,11 @@ class Builder {
|
|
201
278
|
inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex) + this["~orpc"].middlewares.length
|
202
279
|
});
|
203
280
|
}
|
281
|
+
/**
|
282
|
+
* Defines the output validation schema.
|
283
|
+
*
|
284
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Output Validation Docs}
|
285
|
+
*/
|
204
286
|
output(schema) {
|
205
287
|
return new Builder({
|
206
288
|
...this["~orpc"],
|
@@ -208,27 +290,57 @@ class Builder {
|
|
208
290
|
outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex) + this["~orpc"].middlewares.length
|
209
291
|
});
|
210
292
|
}
|
293
|
+
/**
|
294
|
+
* Defines the handler of the procedure.
|
295
|
+
*
|
296
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
297
|
+
*/
|
211
298
|
handler(handler) {
|
212
299
|
return new DecoratedProcedure({
|
213
300
|
...this["~orpc"],
|
214
301
|
handler
|
215
302
|
});
|
216
303
|
}
|
304
|
+
/**
|
305
|
+
* Prefixes all procedures in the router.
|
306
|
+
* The provided prefix is post-appended to any existing router prefix.
|
307
|
+
*
|
308
|
+
* @note This option does not affect procedures that do not define a path in their route definition.
|
309
|
+
*
|
310
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing#route-prefixes OpenAPI Route Prefixes Docs}
|
311
|
+
*/
|
217
312
|
prefix(prefix) {
|
218
313
|
return new Builder({
|
219
314
|
...this["~orpc"],
|
220
315
|
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
221
316
|
});
|
222
317
|
}
|
318
|
+
/**
|
319
|
+
* Adds tags to all procedures in the router.
|
320
|
+
* This helpful when you want to group procedures together in the OpenAPI specification.
|
321
|
+
*
|
322
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
323
|
+
*/
|
223
324
|
tag(...tags) {
|
224
325
|
return new Builder({
|
225
326
|
...this["~orpc"],
|
226
327
|
tags: mergeTags(this["~orpc"].tags, tags)
|
227
328
|
});
|
228
329
|
}
|
330
|
+
/**
|
331
|
+
* Applies all of the previously defined options to the specified router.
|
332
|
+
*
|
333
|
+
* @see {@link https://orpc.unnoq.com/docs/router#extending-router Extending Router Docs}
|
334
|
+
*/
|
229
335
|
router(router) {
|
230
336
|
return enhanceRouter(router, this["~orpc"]);
|
231
337
|
}
|
338
|
+
/**
|
339
|
+
* Create a lazy router
|
340
|
+
* And applies all of the previously defined options to the specified router.
|
341
|
+
*
|
342
|
+
* @see {@link https://orpc.unnoq.com/docs/router#extending-router Extending Router Docs}
|
343
|
+
*/
|
232
344
|
lazy(loader) {
|
233
345
|
return enhanceRouter(lazy(loader), this["~orpc"]);
|
234
346
|
}
|
@@ -336,12 +448,25 @@ function implement(contract, config = {}) {
|
|
336
448
|
return impl;
|
337
449
|
}
|
338
450
|
|
451
|
+
function inferRPCMethodFromRouter(router) {
|
452
|
+
return async (_, path) => {
|
453
|
+
const { default: procedure } = await unlazy(getRouter(router, path));
|
454
|
+
if (!isProcedure(procedure)) {
|
455
|
+
throw new Error(
|
456
|
+
`[inferRPCMethodFromRouter] No valid procedure found at path "${path.join(".")}". This may happen when the router is not properly configured.`
|
457
|
+
);
|
458
|
+
}
|
459
|
+
const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
|
460
|
+
return method === "HEAD" ? "GET" : method;
|
461
|
+
};
|
462
|
+
}
|
463
|
+
|
339
464
|
function createRouterClient(router, ...rest) {
|
340
465
|
if (isProcedure(router)) {
|
341
|
-
const caller = createProcedureClient(router,
|
466
|
+
const caller = createProcedureClient(router, resolveMaybeOptionalOptions(rest));
|
342
467
|
return caller;
|
343
468
|
}
|
344
|
-
const procedureCaller = isLazy(router) ? createProcedureClient(createAssertedLazyProcedure(router),
|
469
|
+
const procedureCaller = isLazy(router) ? createProcedureClient(createAssertedLazyProcedure(router), resolveMaybeOptionalOptions(rest)) : {};
|
345
470
|
const recursive = new Proxy(procedureCaller, {
|
346
471
|
get(target, key) {
|
347
472
|
if (typeof key !== "string") {
|
@@ -360,4 +485,4 @@ function createRouterClient(router, ...rest) {
|
|
360
485
|
return recursive;
|
361
486
|
}
|
362
487
|
|
363
|
-
export { Builder, DecoratedProcedure, Procedure, addMiddleware, createActionableClient, createAssertedLazyProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, fallbackConfig, getRouter, implement, implementerInternal, isLazy, isProcedure, lazy, os, setHiddenRouterContract };
|
488
|
+
export { Builder, DecoratedProcedure, Procedure, addMiddleware, createActionableClient, createAssertedLazyProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, fallbackConfig, getRouter, implement, implementerInternal, inferRPCMethodFromRouter, isLazy, isProcedure, lazy, os, setHiddenRouterContract, unlazy };
|
package/dist/plugins/index.d.mts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
import { Value } from '@orpc/shared';
|
1
|
+
import { Value, Promisable } from '@orpc/shared';
|
2
2
|
import { StandardRequest, StandardHeaders } from '@orpc/standard-server';
|
3
3
|
import { BatchResponseBodyItem } from '@orpc/standard-server/batch';
|
4
|
-
import { S as StandardHandlerInterceptorOptions, a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.
|
5
|
-
import { C as Context, F as ProcedureClientInterceptorOptions } from '../shared/server.
|
4
|
+
import { S as StandardHandlerInterceptorOptions, a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.Dq8xr7PQ.mjs';
|
5
|
+
import { C as Context, F as ProcedureClientInterceptorOptions } from '../shared/server.DD2C4ujN.mjs';
|
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.unnoq.com/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.unnoq.com/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;
|
@@ -61,6 +72,12 @@ declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T>
|
|
61
72
|
interface ResponseHeadersPluginContext {
|
62
73
|
resHeaders?: Headers;
|
63
74
|
}
|
75
|
+
/**
|
76
|
+
* The Response Headers Plugin allows you to set response headers in oRPC.
|
77
|
+
* It injects a resHeaders instance into the context, enabling you to modify response headers easily.
|
78
|
+
*
|
79
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/response-headers Response Headers Plugin Docs}
|
80
|
+
*/
|
64
81
|
declare class ResponseHeadersPlugin<T extends ResponseHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
65
82
|
init(options: StandardHandlerOptions<T>): void;
|
66
83
|
}
|
@@ -71,21 +88,21 @@ interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
|
|
71
88
|
*
|
72
89
|
* @default 'x-csrf-token'
|
73
90
|
*/
|
74
|
-
headerName?: Value<string
|
91
|
+
headerName?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
|
75
92
|
/**
|
76
93
|
* The value of the header to check.
|
77
94
|
*
|
78
95
|
* @default 'orpc'
|
79
96
|
*
|
80
97
|
*/
|
81
|
-
headerValue?: Value<string
|
98
|
+
headerValue?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
|
82
99
|
/**
|
83
100
|
* Exclude a procedure from the plugin.
|
84
101
|
*
|
85
102
|
* @default false
|
86
103
|
*
|
87
104
|
*/
|
88
|
-
exclude?: Value<boolean
|
105
|
+
exclude?: Value<Promisable<boolean>, [options: ProcedureClientInterceptorOptions<T, Record<never, never>, Meta>]>;
|
89
106
|
/**
|
90
107
|
* The error thrown when the CSRF token is invalid.
|
91
108
|
*
|
@@ -96,6 +113,13 @@ interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
|
|
96
113
|
*/
|
97
114
|
error?: InstanceType<typeof ORPCError>;
|
98
115
|
}
|
116
|
+
/**
|
117
|
+
* This plugin adds basic Cross-Site Request Forgery (CSRF) protection to your oRPC application.
|
118
|
+
* It helps ensure that requests to your procedures originate from JavaScript code,
|
119
|
+
* not from other sources like standard HTML forms or direct browser navigation.
|
120
|
+
*
|
121
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/simple-csrf-protection Simple CSRF Protection Plugin Docs}
|
122
|
+
*/
|
99
123
|
declare class SimpleCsrfProtectionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
100
124
|
private readonly headerName;
|
101
125
|
private readonly headerValue;
|
@@ -114,6 +138,13 @@ interface StrictGetMethodPluginOptions {
|
|
114
138
|
*/
|
115
139
|
error?: InstanceType<typeof ORPCError$1>;
|
116
140
|
}
|
141
|
+
/**
|
142
|
+
* This plugin enhances security by ensuring only procedures explicitly marked to accept GET requests
|
143
|
+
* can be called using the HTTP GET method for RPC Protocol. This helps prevent certain types of
|
144
|
+
* Cross-Site Request Forgery (CSRF) attacks.
|
145
|
+
*
|
146
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/strict-get-method Strict Get Method Plugin Docs}
|
147
|
+
*/
|
117
148
|
declare class StrictGetMethodPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
118
149
|
private readonly error;
|
119
150
|
order: number;
|
package/dist/plugins/index.d.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
import { Value } from '@orpc/shared';
|
1
|
+
import { Value, Promisable } from '@orpc/shared';
|
2
2
|
import { StandardRequest, StandardHeaders } from '@orpc/standard-server';
|
3
3
|
import { BatchResponseBodyItem } from '@orpc/standard-server/batch';
|
4
|
-
import { S as StandardHandlerInterceptorOptions, a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.
|
5
|
-
import { C as Context, F as ProcedureClientInterceptorOptions } from '../shared/server.
|
4
|
+
import { S as StandardHandlerInterceptorOptions, a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.-ACo36I0.js';
|
5
|
+
import { C as Context, F as ProcedureClientInterceptorOptions } from '../shared/server.DD2C4ujN.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.unnoq.com/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.unnoq.com/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;
|
@@ -61,6 +72,12 @@ declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T>
|
|
61
72
|
interface ResponseHeadersPluginContext {
|
62
73
|
resHeaders?: Headers;
|
63
74
|
}
|
75
|
+
/**
|
76
|
+
* The Response Headers Plugin allows you to set response headers in oRPC.
|
77
|
+
* It injects a resHeaders instance into the context, enabling you to modify response headers easily.
|
78
|
+
*
|
79
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/response-headers Response Headers Plugin Docs}
|
80
|
+
*/
|
64
81
|
declare class ResponseHeadersPlugin<T extends ResponseHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
65
82
|
init(options: StandardHandlerOptions<T>): void;
|
66
83
|
}
|
@@ -71,21 +88,21 @@ interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
|
|
71
88
|
*
|
72
89
|
* @default 'x-csrf-token'
|
73
90
|
*/
|
74
|
-
headerName?: Value<string
|
91
|
+
headerName?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
|
75
92
|
/**
|
76
93
|
* The value of the header to check.
|
77
94
|
*
|
78
95
|
* @default 'orpc'
|
79
96
|
*
|
80
97
|
*/
|
81
|
-
headerValue?: Value<string
|
98
|
+
headerValue?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
|
82
99
|
/**
|
83
100
|
* Exclude a procedure from the plugin.
|
84
101
|
*
|
85
102
|
* @default false
|
86
103
|
*
|
87
104
|
*/
|
88
|
-
exclude?: Value<boolean
|
105
|
+
exclude?: Value<Promisable<boolean>, [options: ProcedureClientInterceptorOptions<T, Record<never, never>, Meta>]>;
|
89
106
|
/**
|
90
107
|
* The error thrown when the CSRF token is invalid.
|
91
108
|
*
|
@@ -96,6 +113,13 @@ interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
|
|
96
113
|
*/
|
97
114
|
error?: InstanceType<typeof ORPCError>;
|
98
115
|
}
|
116
|
+
/**
|
117
|
+
* This plugin adds basic Cross-Site Request Forgery (CSRF) protection to your oRPC application.
|
118
|
+
* It helps ensure that requests to your procedures originate from JavaScript code,
|
119
|
+
* not from other sources like standard HTML forms or direct browser navigation.
|
120
|
+
*
|
121
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/simple-csrf-protection Simple CSRF Protection Plugin Docs}
|
122
|
+
*/
|
99
123
|
declare class SimpleCsrfProtectionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
100
124
|
private readonly headerName;
|
101
125
|
private readonly headerValue;
|
@@ -114,6 +138,13 @@ interface StrictGetMethodPluginOptions {
|
|
114
138
|
*/
|
115
139
|
error?: InstanceType<typeof ORPCError$1>;
|
116
140
|
}
|
141
|
+
/**
|
142
|
+
* This plugin enhances security by ensuring only procedures explicitly marked to accept GET requests
|
143
|
+
* can be called using the HTTP GET method for RPC Protocol. This helps prevent certain types of
|
144
|
+
* Cross-Site Request Forgery (CSRF) attacks.
|
145
|
+
*
|
146
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/strict-get-method Strict Get Method Plugin Docs}
|
147
|
+
*/
|
117
148
|
declare class StrictGetMethodPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
118
149
|
private readonly error;
|
119
150
|
order: number;
|
package/dist/plugins/index.mjs
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import { value, isAsyncIteratorObject } from '@orpc/shared';
|
2
|
+
import { flattenHeader } from '@orpc/standard-server';
|
2
3
|
import { parseBatchRequest, toBatchResponse } from '@orpc/standard-server/batch';
|
3
4
|
import { ORPCError } from '@orpc/client';
|
4
5
|
export { S as StrictGetMethodPlugin } from '../shared/server.BW-nUGgA.mjs';
|
@@ -25,7 +26,8 @@ class BatchHandlerPlugin {
|
|
25
26
|
init(options) {
|
26
27
|
options.rootInterceptors ??= [];
|
27
28
|
options.rootInterceptors.unshift(async (options2) => {
|
28
|
-
|
29
|
+
const xHeader = flattenHeader(options2.request.headers["x-orpc-batch"]);
|
30
|
+
if (xHeader === void 0) {
|
29
31
|
return options2.next();
|
30
32
|
}
|
31
33
|
let isParsing = false;
|
@@ -68,9 +70,10 @@ class BatchHandlerPlugin {
|
|
68
70
|
await Promise.race(responses);
|
69
71
|
const status = await value(this.successStatus, responses, options2);
|
70
72
|
const headers = await value(this.headers, responses, options2);
|
71
|
-
const response = toBatchResponse({
|
73
|
+
const response = await toBatchResponse({
|
72
74
|
status,
|
73
75
|
headers,
|
76
|
+
mode: xHeader === "buffered" ? "buffered" : "streaming",
|
74
77
|
body: async function* () {
|
75
78
|
const promises = [...responses];
|
76
79
|
while (true) {
|
@@ -123,13 +126,11 @@ class CORSPlugin {
|
|
123
126
|
resHeaders["access-control-max-age"] = this.options.maxAge.toString();
|
124
127
|
}
|
125
128
|
if (this.options.allowMethods?.length) {
|
126
|
-
resHeaders["access-control-allow-methods"] = this.options.allowMethods
|
129
|
+
resHeaders["access-control-allow-methods"] = flattenHeader(this.options.allowMethods);
|
127
130
|
}
|
128
131
|
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;
|
132
|
+
if (typeof allowHeaders === "string" || allowHeaders?.length) {
|
133
|
+
resHeaders["access-control-allow-headers"] = flattenHeader(allowHeaders);
|
133
134
|
}
|
134
135
|
return {
|
135
136
|
matched: true,
|
@@ -147,7 +148,7 @@ class CORSPlugin {
|
|
147
148
|
if (!result.matched) {
|
148
149
|
return result;
|
149
150
|
}
|
150
|
-
const origin =
|
151
|
+
const origin = flattenHeader(interceptorOptions.request.headers.origin) ?? "";
|
151
152
|
const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
|
152
153
|
const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
|
153
154
|
if (allowedOriginArr.includes("*")) {
|
@@ -169,7 +170,7 @@ class CORSPlugin {
|
|
169
170
|
result.response.headers["access-control-allow-credentials"] = "true";
|
170
171
|
}
|
171
172
|
if (this.options.exposeHeaders?.length) {
|
172
|
-
result.response.headers["access-control-expose-headers"] = this.options.exposeHeaders
|
173
|
+
result.response.headers["access-control-expose-headers"] = flattenHeader(this.options.exposeHeaders);
|
173
174
|
}
|
174
175
|
return result;
|
175
176
|
});
|