@orpc/server 0.0.0-next.93e7a4c → 0.0.0-next.93fa264
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 +126 -0
- 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 +78 -0
- package/dist/adapters/fetch/index.d.ts +78 -0
- package/dist/adapters/fetch/index.mjs +110 -0
- 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 +77 -0
- package/dist/adapters/node/index.d.ts +77 -0
- package/dist/adapters/node/index.mjs +95 -0
- package/dist/adapters/standard/index.d.mts +26 -0
- package/dist/adapters/standard/index.d.ts +26 -0
- package/dist/adapters/standard/index.mjs +7 -0
- package/dist/adapters/websocket/index.d.mts +51 -0
- package/dist/adapters/websocket/index.d.ts +51 -0
- package/dist/adapters/websocket/index.mjs +69 -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/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 +834 -0
- package/dist/index.d.ts +834 -0
- package/dist/index.mjs +488 -0
- package/dist/plugins/index.d.mts +156 -0
- package/dist/plugins/index.d.ts +156 -0
- package/dist/plugins/index.mjs +253 -0
- package/dist/shared/server.-ACo36I0.d.ts +74 -0
- package/dist/shared/server.BPAWobQg.d.ts +12 -0
- package/dist/shared/server.BW-nUGgA.mjs +36 -0
- package/dist/shared/server.Bd52nNaH.d.mts +12 -0
- package/dist/shared/server.BliFSTnG.d.mts +10 -0
- package/dist/shared/server.CHvOkcM3.mjs +186 -0
- package/dist/shared/server.DD2C4ujN.d.mts +192 -0
- package/dist/shared/server.DD2C4ujN.d.ts +192 -0
- package/dist/shared/server.DG7Tamti.mjs +367 -0
- package/dist/shared/server.DZ5BIITo.mjs +9 -0
- package/dist/shared/server.Dq8xr7PQ.d.mts +74 -0
- package/dist/shared/server.IG2MjhrD.d.ts +10 -0
- package/package.json +82 -15
- package/dist/chunk-FN62GL22.js +0 -182
- package/dist/fetch.js +0 -286
- package/dist/index.js +0 -518
- package/dist/src/builder.d.ts +0 -35
- package/dist/src/fetch/composite-handler.d.ts +0 -8
- package/dist/src/fetch/index.d.ts +0 -6
- package/dist/src/fetch/orpc-handler.d.ts +0 -20
- package/dist/src/fetch/orpc-payload-codec.d.ts +0 -9
- package/dist/src/fetch/orpc-procedure-matcher.d.ts +0 -12
- package/dist/src/fetch/super-json.d.ts +0 -12
- package/dist/src/fetch/types.d.ts +0 -16
- package/dist/src/hidden.d.ts +0 -6
- package/dist/src/implementer-chainable.d.ts +0 -10
- package/dist/src/index.d.ts +0 -23
- package/dist/src/lazy-decorated.d.ts +0 -10
- package/dist/src/lazy-utils.d.ts +0 -4
- package/dist/src/lazy.d.ts +0 -18
- package/dist/src/middleware-decorated.d.ts +0 -8
- package/dist/src/middleware.d.ts +0 -23
- package/dist/src/procedure-builder.d.ts +0 -22
- package/dist/src/procedure-client.d.ts +0 -29
- package/dist/src/procedure-decorated.d.ts +0 -14
- package/dist/src/procedure-implementer.d.ts +0 -18
- package/dist/src/procedure.d.ts +0 -23
- package/dist/src/router-builder.d.ts +0 -29
- package/dist/src/router-client.d.ts +0 -25
- package/dist/src/router-implementer.d.ts +0 -21
- package/dist/src/router.d.ts +0 -16
- package/dist/src/types.d.ts +0 -12
- package/dist/src/utils.d.ts +0 -3
package/dist/index.mjs
ADDED
@@ -0,0 +1,488 @@
|
|
1
|
+
import { mergeErrorMap, mergeMeta, mergeRoute, mergePrefix, mergeTags, isContractProcedure, getContractRouter, fallbackContractConfig } from '@orpc/contract';
|
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, 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
|
+
import { toORPCError } from '@orpc/client';
|
6
|
+
export { ORPCError, isDefinedError, safe } from '@orpc/client';
|
7
|
+
import { resolveMaybeOptionalOptions } from '@orpc/shared';
|
8
|
+
export { EventPublisher, onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
9
|
+
export { getEventMeta, withEventMeta } from '@orpc/standard-server';
|
10
|
+
|
11
|
+
const DEFAULT_CONFIG = {
|
12
|
+
initialInputValidationIndex: 0,
|
13
|
+
initialOutputValidationIndex: 0,
|
14
|
+
dedupeLeadingMiddlewares: true
|
15
|
+
};
|
16
|
+
function fallbackConfig(key, value) {
|
17
|
+
if (value === void 0) {
|
18
|
+
return DEFAULT_CONFIG[key];
|
19
|
+
}
|
20
|
+
return value;
|
21
|
+
}
|
22
|
+
|
23
|
+
function decorateMiddleware(middleware) {
|
24
|
+
const decorated = (...args) => middleware(...args);
|
25
|
+
decorated.mapInput = (mapInput) => {
|
26
|
+
const mapped = decorateMiddleware(
|
27
|
+
(options, input, ...rest) => middleware(options, mapInput(input), ...rest)
|
28
|
+
);
|
29
|
+
return mapped;
|
30
|
+
};
|
31
|
+
decorated.concat = (concatMiddleware, mapInput) => {
|
32
|
+
const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
|
33
|
+
const concatted = decorateMiddleware((options, input, output, ...rest) => {
|
34
|
+
const merged = middleware({
|
35
|
+
...options,
|
36
|
+
next: (...[nextOptions1]) => mapped({
|
37
|
+
...options,
|
38
|
+
context: { ...options.context, ...nextOptions1?.context },
|
39
|
+
next: (...[nextOptions2]) => options.next({ context: { ...nextOptions1?.context, ...nextOptions2?.context } })
|
40
|
+
}, input, output, ...rest)
|
41
|
+
}, input, output, ...rest);
|
42
|
+
return merged;
|
43
|
+
});
|
44
|
+
return concatted;
|
45
|
+
};
|
46
|
+
return decorated;
|
47
|
+
}
|
48
|
+
|
49
|
+
function createActionableClient(client) {
|
50
|
+
const action = async (input) => {
|
51
|
+
try {
|
52
|
+
return [null, await client(input)];
|
53
|
+
} catch (error) {
|
54
|
+
if (error instanceof Error && "digest" in error && typeof error.digest === "string" && error.digest.startsWith("NEXT_")) {
|
55
|
+
throw error;
|
56
|
+
}
|
57
|
+
return [toORPCError(error).toJSON(), void 0];
|
58
|
+
}
|
59
|
+
};
|
60
|
+
return action;
|
61
|
+
}
|
62
|
+
|
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
|
+
*/
|
70
|
+
errors(errors) {
|
71
|
+
return new DecoratedProcedure({
|
72
|
+
...this["~orpc"],
|
73
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
74
|
+
});
|
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
|
+
*/
|
82
|
+
meta(meta) {
|
83
|
+
return new DecoratedProcedure({
|
84
|
+
...this["~orpc"],
|
85
|
+
meta: mergeMeta(this["~orpc"].meta, meta)
|
86
|
+
});
|
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
|
+
*/
|
96
|
+
route(route) {
|
97
|
+
return new DecoratedProcedure({
|
98
|
+
...this["~orpc"],
|
99
|
+
route: mergeRoute(this["~orpc"].route, route)
|
100
|
+
});
|
101
|
+
}
|
102
|
+
use(middleware, mapInput) {
|
103
|
+
const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
104
|
+
return new DecoratedProcedure({
|
105
|
+
...this["~orpc"],
|
106
|
+
middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
|
107
|
+
});
|
108
|
+
}
|
109
|
+
/**
|
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}
|
113
|
+
*/
|
114
|
+
callable(...rest) {
|
115
|
+
const client = createProcedureClient(this, ...rest);
|
116
|
+
return new Proxy(client, {
|
117
|
+
get: (target, key) => {
|
118
|
+
return Reflect.has(this, key) ? Reflect.get(this, key) : Reflect.get(target, key);
|
119
|
+
},
|
120
|
+
has: (target, key) => {
|
121
|
+
return Reflect.has(this, key) || Reflect.has(target, key);
|
122
|
+
}
|
123
|
+
});
|
124
|
+
}
|
125
|
+
/**
|
126
|
+
* Make this procedure compatible with server action.
|
127
|
+
*
|
128
|
+
* @see {@link https://orpc.unnoq.com/docs/server-action Server Action Docs}
|
129
|
+
*/
|
130
|
+
actionable(...rest) {
|
131
|
+
const action = createActionableClient(createProcedureClient(this, ...rest));
|
132
|
+
return new Proxy(action, {
|
133
|
+
get: (target, key) => {
|
134
|
+
return Reflect.has(this, key) ? Reflect.get(this, key) : Reflect.get(target, key);
|
135
|
+
},
|
136
|
+
has: (target, key) => {
|
137
|
+
return Reflect.has(this, key) || Reflect.has(target, key);
|
138
|
+
}
|
139
|
+
});
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
class Builder {
|
144
|
+
/**
|
145
|
+
* This property holds the defined options.
|
146
|
+
*/
|
147
|
+
"~orpc";
|
148
|
+
constructor(def) {
|
149
|
+
this["~orpc"] = def;
|
150
|
+
}
|
151
|
+
/**
|
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}
|
156
|
+
*/
|
157
|
+
$config(config) {
|
158
|
+
const inputValidationCount = this["~orpc"].inputValidationIndex - fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex);
|
159
|
+
const outputValidationCount = this["~orpc"].outputValidationIndex - fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex);
|
160
|
+
return new Builder({
|
161
|
+
...this["~orpc"],
|
162
|
+
config,
|
163
|
+
dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares),
|
164
|
+
inputValidationIndex: fallbackConfig("initialInputValidationIndex", config.initialInputValidationIndex) + inputValidationCount,
|
165
|
+
outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config.initialOutputValidationIndex) + outputValidationCount
|
166
|
+
});
|
167
|
+
}
|
168
|
+
/**
|
169
|
+
* Set or override the initial context.
|
170
|
+
*
|
171
|
+
* @see {@link https://orpc.unnoq.com/docs/context Context Docs}
|
172
|
+
*/
|
173
|
+
$context() {
|
174
|
+
return new Builder({
|
175
|
+
...this["~orpc"],
|
176
|
+
middlewares: [],
|
177
|
+
inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
|
178
|
+
outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex)
|
179
|
+
});
|
180
|
+
}
|
181
|
+
/**
|
182
|
+
* Sets or overrides the initial meta.
|
183
|
+
*
|
184
|
+
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
185
|
+
*/
|
186
|
+
$meta(initialMeta) {
|
187
|
+
return new Builder({
|
188
|
+
...this["~orpc"],
|
189
|
+
meta: initialMeta
|
190
|
+
});
|
191
|
+
}
|
192
|
+
/**
|
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}
|
198
|
+
*/
|
199
|
+
$route(initialRoute) {
|
200
|
+
return new Builder({
|
201
|
+
...this["~orpc"],
|
202
|
+
route: initialRoute
|
203
|
+
});
|
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
|
+
*/
|
210
|
+
$input(initialInputSchema) {
|
211
|
+
return new Builder({
|
212
|
+
...this["~orpc"],
|
213
|
+
inputSchema: initialInputSchema
|
214
|
+
});
|
215
|
+
}
|
216
|
+
/**
|
217
|
+
* Creates a middleware.
|
218
|
+
*
|
219
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
220
|
+
*/
|
221
|
+
middleware(middleware) {
|
222
|
+
return decorateMiddleware(middleware);
|
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
|
+
*/
|
230
|
+
errors(errors) {
|
231
|
+
return new Builder({
|
232
|
+
...this["~orpc"],
|
233
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
234
|
+
});
|
235
|
+
}
|
236
|
+
use(middleware, mapInput) {
|
237
|
+
const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
238
|
+
return new Builder({
|
239
|
+
...this["~orpc"],
|
240
|
+
middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
|
241
|
+
});
|
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
|
+
*/
|
249
|
+
meta(meta) {
|
250
|
+
return new Builder({
|
251
|
+
...this["~orpc"],
|
252
|
+
meta: mergeMeta(this["~orpc"].meta, meta)
|
253
|
+
});
|
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
|
+
*/
|
263
|
+
route(route) {
|
264
|
+
return new Builder({
|
265
|
+
...this["~orpc"],
|
266
|
+
route: mergeRoute(this["~orpc"].route, route)
|
267
|
+
});
|
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
|
+
*/
|
274
|
+
input(schema) {
|
275
|
+
return new Builder({
|
276
|
+
...this["~orpc"],
|
277
|
+
inputSchema: schema,
|
278
|
+
inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex) + this["~orpc"].middlewares.length
|
279
|
+
});
|
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
|
+
*/
|
286
|
+
output(schema) {
|
287
|
+
return new Builder({
|
288
|
+
...this["~orpc"],
|
289
|
+
outputSchema: schema,
|
290
|
+
outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex) + this["~orpc"].middlewares.length
|
291
|
+
});
|
292
|
+
}
|
293
|
+
/**
|
294
|
+
* Defines the handler of the procedure.
|
295
|
+
*
|
296
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
297
|
+
*/
|
298
|
+
handler(handler) {
|
299
|
+
return new DecoratedProcedure({
|
300
|
+
...this["~orpc"],
|
301
|
+
handler
|
302
|
+
});
|
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
|
+
*/
|
312
|
+
prefix(prefix) {
|
313
|
+
return new Builder({
|
314
|
+
...this["~orpc"],
|
315
|
+
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
316
|
+
});
|
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
|
+
*/
|
324
|
+
tag(...tags) {
|
325
|
+
return new Builder({
|
326
|
+
...this["~orpc"],
|
327
|
+
tags: mergeTags(this["~orpc"].tags, tags)
|
328
|
+
});
|
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
|
+
*/
|
335
|
+
router(router) {
|
336
|
+
return enhanceRouter(router, this["~orpc"]);
|
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
|
+
*/
|
344
|
+
lazy(loader) {
|
345
|
+
return enhanceRouter(lazy(loader), this["~orpc"]);
|
346
|
+
}
|
347
|
+
}
|
348
|
+
const os = new Builder({
|
349
|
+
config: {},
|
350
|
+
route: {},
|
351
|
+
meta: {},
|
352
|
+
errorMap: {},
|
353
|
+
inputValidationIndex: fallbackConfig("initialInputValidationIndex"),
|
354
|
+
outputValidationIndex: fallbackConfig("initialOutputValidationIndex"),
|
355
|
+
middlewares: [],
|
356
|
+
dedupeLeadingMiddlewares: true
|
357
|
+
});
|
358
|
+
|
359
|
+
function implementerInternal(contract, config, middlewares) {
|
360
|
+
if (isContractProcedure(contract)) {
|
361
|
+
const impl2 = new Builder({
|
362
|
+
...contract["~orpc"],
|
363
|
+
config,
|
364
|
+
middlewares,
|
365
|
+
inputValidationIndex: fallbackConfig("initialInputValidationIndex", config?.initialInputValidationIndex) + middlewares.length,
|
366
|
+
outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config?.initialOutputValidationIndex) + middlewares.length,
|
367
|
+
dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
|
368
|
+
});
|
369
|
+
return impl2;
|
370
|
+
}
|
371
|
+
const impl = new Proxy(contract, {
|
372
|
+
get: (target, key) => {
|
373
|
+
if (typeof key !== "string") {
|
374
|
+
return Reflect.get(target, key);
|
375
|
+
}
|
376
|
+
let method;
|
377
|
+
if (key === "middleware") {
|
378
|
+
method = (mid) => decorateMiddleware(mid);
|
379
|
+
} else if (key === "use") {
|
380
|
+
method = (mid) => {
|
381
|
+
return implementerInternal(
|
382
|
+
contract,
|
383
|
+
config,
|
384
|
+
addMiddleware(middlewares, mid)
|
385
|
+
);
|
386
|
+
};
|
387
|
+
} else if (key === "router") {
|
388
|
+
method = (router) => {
|
389
|
+
const adapted = enhanceRouter(router, {
|
390
|
+
middlewares,
|
391
|
+
errorMap: {},
|
392
|
+
prefix: void 0,
|
393
|
+
tags: void 0,
|
394
|
+
dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
|
395
|
+
});
|
396
|
+
return setHiddenRouterContract(adapted, contract);
|
397
|
+
};
|
398
|
+
} else if (key === "lazy") {
|
399
|
+
method = (loader) => {
|
400
|
+
const adapted = enhanceRouter(lazy(loader), {
|
401
|
+
middlewares,
|
402
|
+
errorMap: {},
|
403
|
+
prefix: void 0,
|
404
|
+
tags: void 0,
|
405
|
+
dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
|
406
|
+
});
|
407
|
+
return setHiddenRouterContract(adapted, contract);
|
408
|
+
};
|
409
|
+
}
|
410
|
+
const next = getContractRouter(target, [key]);
|
411
|
+
if (!next) {
|
412
|
+
return method ?? next;
|
413
|
+
}
|
414
|
+
const nextImpl = implementerInternal(next, config, middlewares);
|
415
|
+
if (method) {
|
416
|
+
return new Proxy(method, {
|
417
|
+
get(_, key2) {
|
418
|
+
return Reflect.get(nextImpl, key2);
|
419
|
+
}
|
420
|
+
});
|
421
|
+
}
|
422
|
+
return nextImpl;
|
423
|
+
}
|
424
|
+
});
|
425
|
+
return impl;
|
426
|
+
}
|
427
|
+
function implement(contract, config = {}) {
|
428
|
+
const implInternal = implementerInternal(contract, config, []);
|
429
|
+
const impl = new Proxy(implInternal, {
|
430
|
+
get: (target, key) => {
|
431
|
+
let method;
|
432
|
+
if (key === "$context") {
|
433
|
+
method = () => impl;
|
434
|
+
} else if (key === "$config") {
|
435
|
+
method = (config2) => implement(contract, config2);
|
436
|
+
}
|
437
|
+
const next = Reflect.get(target, key);
|
438
|
+
if (!method || !next || typeof next !== "function" && typeof next !== "object") {
|
439
|
+
return method || next;
|
440
|
+
}
|
441
|
+
return new Proxy(method, {
|
442
|
+
get(_, key2) {
|
443
|
+
return Reflect.get(next, key2);
|
444
|
+
}
|
445
|
+
});
|
446
|
+
}
|
447
|
+
});
|
448
|
+
return impl;
|
449
|
+
}
|
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
|
+
|
464
|
+
function createRouterClient(router, ...rest) {
|
465
|
+
if (isProcedure(router)) {
|
466
|
+
const caller = createProcedureClient(router, resolveMaybeOptionalOptions(rest));
|
467
|
+
return caller;
|
468
|
+
}
|
469
|
+
const procedureCaller = isLazy(router) ? createProcedureClient(createAssertedLazyProcedure(router), resolveMaybeOptionalOptions(rest)) : {};
|
470
|
+
const recursive = new Proxy(procedureCaller, {
|
471
|
+
get(target, key) {
|
472
|
+
if (typeof key !== "string") {
|
473
|
+
return Reflect.get(target, key);
|
474
|
+
}
|
475
|
+
const next = getRouter(router, [key]);
|
476
|
+
if (!next) {
|
477
|
+
return Reflect.get(target, key);
|
478
|
+
}
|
479
|
+
return createRouterClient(next, {
|
480
|
+
...rest[0],
|
481
|
+
path: [...rest[0]?.path ?? [], key]
|
482
|
+
});
|
483
|
+
}
|
484
|
+
});
|
485
|
+
return recursive;
|
486
|
+
}
|
487
|
+
|
488
|
+
export { Builder, DecoratedProcedure, Procedure, addMiddleware, createActionableClient, createAssertedLazyProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, fallbackConfig, getRouter, implement, implementerInternal, inferRPCMethodFromRouter, isLazy, isProcedure, lazy, os, setHiddenRouterContract, unlazy };
|
@@ -0,0 +1,156 @@
|
|
1
|
+
import { Value, Promisable } from '@orpc/shared';
|
2
|
+
import { StandardRequest, StandardHeaders } from '@orpc/standard-server';
|
3
|
+
import { BatchResponseBodyItem } from '@orpc/standard-server/batch';
|
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
|
+
import { Meta, ORPCError as ORPCError$1 } from '@orpc/contract';
|
7
|
+
import { ORPCError } from '@orpc/client';
|
8
|
+
|
9
|
+
interface BatchHandlerOptions<T extends Context> {
|
10
|
+
/**
|
11
|
+
* The max size of the batch allowed.
|
12
|
+
*
|
13
|
+
* @default 10
|
14
|
+
*/
|
15
|
+
maxSize?: Value<Promisable<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<Promisable<number>, [responses: Promise<BatchResponseBodyItem>[], batchOptions: StandardHandlerInterceptorOptions<T>]>;
|
28
|
+
/**
|
29
|
+
* success batch response headers.
|
30
|
+
*
|
31
|
+
* @default {}
|
32
|
+
*/
|
33
|
+
headers?: Value<Promisable<StandardHeaders>, [responses: Promise<BatchResponseBodyItem>[], batchOptions: StandardHandlerInterceptorOptions<T>]>;
|
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
|
+
*/
|
41
|
+
declare class BatchHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
42
|
+
private readonly maxSize;
|
43
|
+
private readonly mapRequestItem;
|
44
|
+
private readonly successStatus;
|
45
|
+
private readonly headers;
|
46
|
+
order: number;
|
47
|
+
constructor(options?: BatchHandlerOptions<T>);
|
48
|
+
init(options: StandardHandlerOptions<T>): void;
|
49
|
+
}
|
50
|
+
|
51
|
+
interface CORSOptions<T extends Context> {
|
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>]>;
|
54
|
+
allowMethods?: readonly string[];
|
55
|
+
allowHeaders?: readonly string[];
|
56
|
+
maxAge?: number;
|
57
|
+
credentials?: boolean;
|
58
|
+
exposeHeaders?: readonly string[];
|
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
|
+
*/
|
65
|
+
declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
66
|
+
private readonly options;
|
67
|
+
order: number;
|
68
|
+
constructor(options?: CORSOptions<T>);
|
69
|
+
init(options: StandardHandlerOptions<T>): void;
|
70
|
+
}
|
71
|
+
|
72
|
+
interface ResponseHeadersPluginContext {
|
73
|
+
resHeaders?: Headers;
|
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
|
+
*/
|
81
|
+
declare class ResponseHeadersPlugin<T extends ResponseHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
82
|
+
init(options: StandardHandlerOptions<T>): void;
|
83
|
+
}
|
84
|
+
|
85
|
+
interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
|
86
|
+
/**
|
87
|
+
* The name of the header to check.
|
88
|
+
*
|
89
|
+
* @default 'x-csrf-token'
|
90
|
+
*/
|
91
|
+
headerName?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
|
92
|
+
/**
|
93
|
+
* The value of the header to check.
|
94
|
+
*
|
95
|
+
* @default 'orpc'
|
96
|
+
*
|
97
|
+
*/
|
98
|
+
headerValue?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
|
99
|
+
/**
|
100
|
+
* Exclude a procedure from the plugin.
|
101
|
+
*
|
102
|
+
* @default false
|
103
|
+
*
|
104
|
+
*/
|
105
|
+
exclude?: Value<Promisable<boolean>, [options: ProcedureClientInterceptorOptions<T, Record<never, never>, Meta>]>;
|
106
|
+
/**
|
107
|
+
* The error thrown when the CSRF token is invalid.
|
108
|
+
*
|
109
|
+
* @default new ORPCError('CSRF_TOKEN_MISMATCH', {
|
110
|
+
* status: 403,
|
111
|
+
* message: 'Invalid CSRF token',
|
112
|
+
* })
|
113
|
+
*/
|
114
|
+
error?: InstanceType<typeof ORPCError>;
|
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
|
+
*/
|
123
|
+
declare class SimpleCsrfProtectionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
124
|
+
private readonly headerName;
|
125
|
+
private readonly headerValue;
|
126
|
+
private readonly exclude;
|
127
|
+
private readonly error;
|
128
|
+
constructor(options?: SimpleCsrfProtectionHandlerPluginOptions<T>);
|
129
|
+
order: number;
|
130
|
+
init(options: StandardHandlerOptions<T>): void;
|
131
|
+
}
|
132
|
+
|
133
|
+
interface StrictGetMethodPluginOptions {
|
134
|
+
/**
|
135
|
+
* The error thrown when a GET request is made to a procedure that doesn't allow GET.
|
136
|
+
*
|
137
|
+
* @default new ORPCError('METHOD_NOT_SUPPORTED')
|
138
|
+
*/
|
139
|
+
error?: InstanceType<typeof ORPCError$1>;
|
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
|
+
*/
|
148
|
+
declare class StrictGetMethodPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
149
|
+
private readonly error;
|
150
|
+
order: number;
|
151
|
+
constructor(options?: StrictGetMethodPluginOptions);
|
152
|
+
init(options: StandardHandlerOptions<T>): void;
|
153
|
+
}
|
154
|
+
|
155
|
+
export { BatchHandlerPlugin, CORSPlugin, ResponseHeadersPlugin, SimpleCsrfProtectionHandlerPlugin, StrictGetMethodPlugin };
|
156
|
+
export type { BatchHandlerOptions, CORSOptions, ResponseHeadersPluginContext, SimpleCsrfProtectionHandlerPluginOptions, StrictGetMethodPluginOptions };
|