@temporary-name/server 1.9.3-alpha.592ae1e02e18c5274906b132313b083116248636 → 1.9.3-alpha.5c041722542bb790586629fc98d4c72d9f6ee65e
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/dist/adapters/aws-lambda/index.d.mts +3 -3
- package/dist/adapters/aws-lambda/index.d.ts +3 -3
- package/dist/adapters/aws-lambda/index.mjs +4 -6
- package/dist/adapters/fetch/index.d.mts +3 -3
- package/dist/adapters/fetch/index.d.ts +3 -3
- package/dist/adapters/fetch/index.mjs +4 -6
- package/dist/adapters/node/index.d.mts +3 -3
- package/dist/adapters/node/index.d.ts +3 -3
- package/dist/adapters/node/index.mjs +4 -6
- package/dist/adapters/standard/index.d.mts +14 -30
- package/dist/adapters/standard/index.d.ts +14 -30
- package/dist/adapters/standard/index.mjs +4 -6
- package/dist/index.d.mts +60 -334
- package/dist/index.d.ts +60 -334
- package/dist/index.mjs +111 -354
- package/dist/openapi/index.mjs +30 -64
- package/dist/plugins/index.d.mts +2 -2
- package/dist/plugins/index.d.ts +2 -2
- package/dist/shared/{server.BKSOrA6h.d.mts → server.B5czSQQh.d.mts} +114 -71
- package/dist/shared/{server.BKSOrA6h.d.ts → server.B5czSQQh.d.ts} +114 -71
- package/dist/shared/server.BdGbIyq8.mjs +396 -0
- package/dist/shared/{server.BeuTpcmO.d.mts → server.Bk8Hmx7Z.d.mts} +2 -2
- package/dist/shared/{server.CQyYNJ1H.d.ts → server.C4l2ZA3m.d.ts} +1 -2
- package/dist/shared/{server.SLLuK6_v.d.ts → server.D7UjcuIv.d.ts} +2 -2
- package/dist/shared/{server.BEHw7Eyx.mjs → server.Kxw442A9.mjs} +1 -1
- package/dist/shared/{server.DLsti1Pv.mjs → server.SmOM3Fae.mjs} +58 -89
- package/dist/shared/{server.C1fnTLq0.d.mts → server.TgmlNh8r.d.mts} +1 -2
- package/package.json +10 -9
- package/dist/shared/server.BKh8I1Ny.mjs +0 -239
package/dist/index.mjs
CHANGED
|
@@ -1,136 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { ValidationError, eventIterator,
|
|
1
|
+
import { mergePrefix, mergeTags, ContractProcedure, mergeErrorMap, mergeMeta, mergeRoute, prefixRoute, initialSchemas, isContractProcedure, getContractRouter } from '@temporary-name/contract';
|
|
2
|
+
export { ValidationError, eventIterator, validateORPCError } from '@temporary-name/contract';
|
|
3
3
|
import { onError, resolveMaybeOptionalOptions } from '@temporary-name/shared';
|
|
4
4
|
export { AsyncIteratorClass, EventPublisher, ORPCError, asyncIteratorToStream as eventIteratorToStream, isDefinedError, onError, onFinish, onStart, onSuccess, safe, streamToAsyncIteratorClass as streamToEventIterator } from '@temporary-name/shared';
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import { SchemaClass, gatingContext } from '@temporary-name/zod';
|
|
6
|
+
import { P as Procedure, c as createProcedureClient, e as enhanceRouter, a as addMiddleware, s as setHiddenRouterContract, l as lazyInternal, g as getLazyMeta, u as unlazy, i as isProcedure, b as isLazy, d as getRouter } from './shared/server.BdGbIyq8.mjs';
|
|
7
|
+
export { L as LAZY_SYMBOL, p as createAccessibleLazyRouter, f as createORPCErrorConstructorMap, o as getHiddenRouterContract, k as isStartWithMiddlewares, h as lazy, m as mergeCurrentContext, n as mergeMiddlewares, j as middlewareOutputFn, r as resolveContractProcedures, t as traverseContractProcedures, q as unlazyRouter } from './shared/server.BdGbIyq8.mjs';
|
|
7
8
|
export { getEventMeta, withEventMeta } from '@temporary-name/standard-server';
|
|
8
|
-
import 'node:async_hooks';
|
|
9
|
-
import 'zod';
|
|
10
|
-
import 'zod/v4/core';
|
|
11
|
-
|
|
12
|
-
function decorateMiddleware(middleware) {
|
|
13
|
-
const decorated = ((...args) => middleware(...args));
|
|
14
|
-
decorated.mapInput = (mapInput) => {
|
|
15
|
-
const mapped = decorateMiddleware(
|
|
16
|
-
(options, input, ...rest) => middleware(options, mapInput(input), ...rest)
|
|
17
|
-
);
|
|
18
|
-
return mapped;
|
|
19
|
-
};
|
|
20
|
-
decorated.concat = (concatMiddleware, mapInput) => {
|
|
21
|
-
const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
|
|
22
|
-
const concatted = decorateMiddleware((options, input, output, ...rest) => {
|
|
23
|
-
const merged = middleware(
|
|
24
|
-
{
|
|
25
|
-
...options,
|
|
26
|
-
next: (...[nextOptions1]) => mapped(
|
|
27
|
-
{
|
|
28
|
-
...options,
|
|
29
|
-
context: { ...options.context, ...nextOptions1?.context },
|
|
30
|
-
next: (...[nextOptions2]) => options.next({ context: { ...nextOptions1?.context, ...nextOptions2?.context } })
|
|
31
|
-
},
|
|
32
|
-
input,
|
|
33
|
-
output,
|
|
34
|
-
...rest
|
|
35
|
-
)
|
|
36
|
-
},
|
|
37
|
-
input,
|
|
38
|
-
output,
|
|
39
|
-
...rest
|
|
40
|
-
);
|
|
41
|
-
return merged;
|
|
42
|
-
});
|
|
43
|
-
return concatted;
|
|
44
|
-
};
|
|
45
|
-
return decorated;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function isStartWithMiddlewares(middlewares, compare) {
|
|
49
|
-
if (compare.length > middlewares.length) {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
for (let i = 0; i < middlewares.length; i++) {
|
|
53
|
-
if (compare[i] === void 0) {
|
|
54
|
-
return true;
|
|
55
|
-
}
|
|
56
|
-
if (middlewares[i] !== compare[i]) {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
function mergeMiddlewares(first, second, options) {
|
|
63
|
-
if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
|
|
64
|
-
return second;
|
|
65
|
-
}
|
|
66
|
-
return [...first, ...second];
|
|
67
|
-
}
|
|
68
|
-
function addMiddleware(middlewares, addition) {
|
|
69
|
-
return [...middlewares, addition];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
class Procedure {
|
|
73
|
-
/**
|
|
74
|
-
* This property holds the defined options.
|
|
75
|
-
*/
|
|
76
|
-
"~orpc";
|
|
77
|
-
constructor(def) {
|
|
78
|
-
this["~orpc"] = def;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
function isProcedure(item) {
|
|
82
|
-
if (item instanceof Procedure) {
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
|
86
|
-
}
|
|
87
9
|
|
|
88
10
|
class DecoratedProcedure extends Procedure {
|
|
89
|
-
/**
|
|
90
|
-
* Adds type-safe custom errors.
|
|
91
|
-
* The provided errors are spared-merged with any existing errors.
|
|
92
|
-
*
|
|
93
|
-
* @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
94
|
-
*/
|
|
95
|
-
errors(errors) {
|
|
96
|
-
return new DecoratedProcedure({
|
|
97
|
-
...this["~orpc"],
|
|
98
|
-
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Sets or updates the metadata.
|
|
103
|
-
* The provided metadata is spared-merged with any existing metadata.
|
|
104
|
-
*
|
|
105
|
-
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
|
106
|
-
*/
|
|
107
|
-
meta(meta) {
|
|
108
|
-
return new DecoratedProcedure({
|
|
109
|
-
...this["~orpc"],
|
|
110
|
-
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Sets or updates the route definition.
|
|
115
|
-
* The provided route is spared-merged with any existing route.
|
|
116
|
-
* This option is typically relevant when integrating with OpenAPI.
|
|
117
|
-
*
|
|
118
|
-
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
119
|
-
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
120
|
-
*/
|
|
121
|
-
route(route) {
|
|
122
|
-
return new DecoratedProcedure({
|
|
123
|
-
...this["~orpc"],
|
|
124
|
-
route: mergeRoute(this["~orpc"].route, route)
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
use(middleware, mapInput) {
|
|
128
|
-
const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
|
129
|
-
return new DecoratedProcedure({
|
|
130
|
-
...this["~orpc"],
|
|
131
|
-
middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
11
|
/**
|
|
135
12
|
* Make this procedure callable (works like a function while still being a procedure).
|
|
136
13
|
*
|
|
@@ -149,233 +26,49 @@ class DecoratedProcedure extends Procedure {
|
|
|
149
26
|
}
|
|
150
27
|
}
|
|
151
28
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
return new Proxy(router, {
|
|
155
|
-
get(target, key) {
|
|
156
|
-
if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
|
|
157
|
-
return contract;
|
|
158
|
-
}
|
|
159
|
-
return Reflect.get(target, key);
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
function getHiddenRouterContract(router) {
|
|
164
|
-
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function getRouter(router, path) {
|
|
168
|
-
let current = router;
|
|
169
|
-
for (let i = 0; i < path.length; i++) {
|
|
170
|
-
const segment = path[i];
|
|
171
|
-
if (!current) {
|
|
172
|
-
return void 0;
|
|
173
|
-
}
|
|
174
|
-
if (isProcedure(current)) {
|
|
175
|
-
return void 0;
|
|
176
|
-
}
|
|
177
|
-
if (!isLazy(current)) {
|
|
178
|
-
current = current[segment];
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
const lazied = current;
|
|
182
|
-
const rest = path.slice(i);
|
|
183
|
-
return lazy(async () => {
|
|
184
|
-
const unwrapped = await unlazy(lazied);
|
|
185
|
-
const next = getRouter(unwrapped.default, rest);
|
|
186
|
-
return unlazy(next);
|
|
187
|
-
}, getLazyMeta(lazied));
|
|
188
|
-
}
|
|
189
|
-
return current;
|
|
190
|
-
}
|
|
191
|
-
function createAccessibleLazyRouter(lazied) {
|
|
192
|
-
const recursive = new Proxy(lazied, {
|
|
193
|
-
get(target, key) {
|
|
194
|
-
if (typeof key !== "string") {
|
|
195
|
-
return Reflect.get(target, key);
|
|
196
|
-
}
|
|
197
|
-
const next = getRouter(lazied, [key]);
|
|
198
|
-
return createAccessibleLazyRouter(next);
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
return recursive;
|
|
202
|
-
}
|
|
203
|
-
function enhanceRouter(router, options) {
|
|
204
|
-
if (isLazy(router)) {
|
|
205
|
-
const laziedMeta = getLazyMeta(router);
|
|
206
|
-
const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
|
|
207
|
-
const enhanced2 = lazy(
|
|
208
|
-
async () => {
|
|
209
|
-
const { default: unlaziedRouter } = await unlazy(router);
|
|
210
|
-
const enhanced3 = enhanceRouter(unlaziedRouter, options);
|
|
211
|
-
return unlazy(enhanced3);
|
|
212
|
-
},
|
|
213
|
-
{
|
|
214
|
-
...laziedMeta,
|
|
215
|
-
prefix: enhancedPrefix
|
|
216
|
-
}
|
|
217
|
-
);
|
|
218
|
-
const accessible = createAccessibleLazyRouter(enhanced2);
|
|
219
|
-
return accessible;
|
|
220
|
-
}
|
|
221
|
-
if (isProcedure(router)) {
|
|
222
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, {
|
|
223
|
-
dedupeLeading: options.dedupeLeadingMiddlewares
|
|
224
|
-
});
|
|
225
|
-
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
|
226
|
-
const enhanced2 = new Procedure({
|
|
227
|
-
...router["~orpc"],
|
|
228
|
-
route: enhanceRoute(router["~orpc"].route, options),
|
|
229
|
-
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
|
230
|
-
middlewares: newMiddlewares,
|
|
231
|
-
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
|
232
|
-
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
|
233
|
-
});
|
|
234
|
-
return enhanced2;
|
|
235
|
-
}
|
|
236
|
-
const enhanced = {};
|
|
237
|
-
for (const key in router) {
|
|
238
|
-
enhanced[key] = enhanceRouter(router[key], options);
|
|
239
|
-
}
|
|
240
|
-
return enhanced;
|
|
241
|
-
}
|
|
242
|
-
function traverseContractProcedures(options, callback, lazyOptions = []) {
|
|
243
|
-
let currentRouter = options.router;
|
|
244
|
-
const hiddenContract = getHiddenRouterContract(options.router);
|
|
245
|
-
if (hiddenContract !== void 0) {
|
|
246
|
-
currentRouter = hiddenContract;
|
|
247
|
-
}
|
|
248
|
-
if (isLazy(currentRouter)) {
|
|
249
|
-
lazyOptions.push({
|
|
250
|
-
router: currentRouter,
|
|
251
|
-
path: options.path
|
|
252
|
-
});
|
|
253
|
-
} else if (isContractProcedure(currentRouter)) {
|
|
254
|
-
callback({
|
|
255
|
-
contract: currentRouter,
|
|
256
|
-
path: options.path
|
|
257
|
-
});
|
|
258
|
-
} else {
|
|
259
|
-
for (const key in currentRouter) {
|
|
260
|
-
traverseContractProcedures(
|
|
261
|
-
{
|
|
262
|
-
router: currentRouter[key],
|
|
263
|
-
path: [...options.path, key]
|
|
264
|
-
},
|
|
265
|
-
callback,
|
|
266
|
-
lazyOptions
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
return lazyOptions;
|
|
271
|
-
}
|
|
272
|
-
async function resolveContractProcedures(options, callback) {
|
|
273
|
-
const pending = [options];
|
|
274
|
-
for (const options2 of pending) {
|
|
275
|
-
const lazyOptions = traverseContractProcedures(options2, callback);
|
|
276
|
-
for (const options3 of lazyOptions) {
|
|
277
|
-
const { default: router } = await unlazy(options3.router);
|
|
278
|
-
pending.push({
|
|
279
|
-
router,
|
|
280
|
-
path: options3.path
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
async function unlazyRouter(router) {
|
|
286
|
-
if (isProcedure(router)) {
|
|
287
|
-
return router;
|
|
288
|
-
}
|
|
289
|
-
const unlazied = {};
|
|
290
|
-
for (const key in router) {
|
|
291
|
-
const item = router[key];
|
|
292
|
-
const { default: unlaziedRouter } = await unlazy(item);
|
|
293
|
-
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
|
294
|
-
}
|
|
295
|
-
return unlazied;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
class Builder {
|
|
299
|
-
/**
|
|
300
|
-
* This property holds the defined options.
|
|
301
|
-
*/
|
|
302
|
-
"~orpc";
|
|
29
|
+
class ProcedureBuilder extends ContractProcedure {
|
|
30
|
+
z;
|
|
303
31
|
constructor(def) {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* Set or override the initial context.
|
|
308
|
-
*
|
|
309
|
-
* @see {@link https://orpc.unnoq.com/docs/context Context Docs}
|
|
310
|
-
*/
|
|
311
|
-
$context() {
|
|
312
|
-
return this;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Creates a middleware.
|
|
316
|
-
*
|
|
317
|
-
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
318
|
-
*/
|
|
319
|
-
middleware(middleware) {
|
|
320
|
-
return decorateMiddleware(middleware);
|
|
32
|
+
super(def);
|
|
33
|
+
this.z = new SchemaClass();
|
|
321
34
|
}
|
|
322
35
|
/**
|
|
323
|
-
* Adds type-safe custom errors.
|
|
324
|
-
* The provided errors are spared-merged with any existing errors.
|
|
36
|
+
* Adds type-safe custom errors to the contract.
|
|
37
|
+
* The provided errors are spared-merged with any existing errors in the contract.
|
|
325
38
|
*
|
|
326
39
|
* @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
327
40
|
*/
|
|
328
41
|
errors(errors) {
|
|
329
|
-
return new
|
|
42
|
+
return new ProcedureBuilder({
|
|
330
43
|
...this["~orpc"],
|
|
331
44
|
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
332
45
|
});
|
|
333
46
|
}
|
|
334
47
|
/**
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
* @info Supports both normal middleware and inline middleware implementations.
|
|
338
|
-
* @note The current context must be satisfy middleware dependent-context
|
|
339
|
-
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
340
|
-
*/
|
|
341
|
-
use(middleware) {
|
|
342
|
-
return new Builder({
|
|
343
|
-
...this["~orpc"],
|
|
344
|
-
middlewares: addMiddleware(this["~orpc"].middlewares, middleware)
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
useGating(gates, isGateEnabled) {
|
|
348
|
-
return this.use(({ next, context }) => {
|
|
349
|
-
return gatingContext.run(
|
|
350
|
-
(gate) => isGateEnabled(gate, context),
|
|
351
|
-
() => next({ context: { isGateEnabled } })
|
|
352
|
-
);
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* Sets or updates the metadata.
|
|
357
|
-
* The provided metadata is spared-merged with any existing metadata.
|
|
48
|
+
* Sets or updates the metadata for the contract.
|
|
49
|
+
* The provided metadata is spared-merged with any existing metadata in the contract.
|
|
358
50
|
*
|
|
359
51
|
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
|
360
52
|
*/
|
|
361
53
|
meta(meta) {
|
|
362
|
-
return new
|
|
54
|
+
return new ProcedureBuilder({
|
|
363
55
|
...this["~orpc"],
|
|
364
56
|
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
365
57
|
});
|
|
366
58
|
}
|
|
367
59
|
/**
|
|
368
|
-
* Sets or updates the route definition.
|
|
369
|
-
* The provided route is spared-merged with any existing route.
|
|
60
|
+
* Sets or updates the route definition for the contract.
|
|
61
|
+
* The provided route is spared-merged with any existing route in the contract.
|
|
370
62
|
* This option is typically relevant when integrating with OpenAPI.
|
|
371
63
|
*
|
|
372
64
|
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
373
65
|
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
374
66
|
*/
|
|
375
67
|
route(route) {
|
|
376
|
-
|
|
68
|
+
const { prefix } = this["~orpc"];
|
|
69
|
+
return new ProcedureBuilder({
|
|
377
70
|
...this["~orpc"],
|
|
378
|
-
route: mergeRoute(this["~orpc"].route, route)
|
|
71
|
+
route: mergeRoute(this["~orpc"].route, prefix ? prefixRoute(route, prefix) : route)
|
|
379
72
|
});
|
|
380
73
|
}
|
|
381
74
|
/**
|
|
@@ -384,9 +77,12 @@ class Builder {
|
|
|
384
77
|
* @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Input Validation Docs}
|
|
385
78
|
*/
|
|
386
79
|
input(schema) {
|
|
387
|
-
return new
|
|
80
|
+
return new ProcedureBuilder({
|
|
388
81
|
...this["~orpc"],
|
|
389
|
-
|
|
82
|
+
schemas: {
|
|
83
|
+
...this["~orpc"].schemas,
|
|
84
|
+
inputSchema: schema
|
|
85
|
+
},
|
|
390
86
|
inputValidationIndex: this["~orpc"].middlewares.length
|
|
391
87
|
});
|
|
392
88
|
}
|
|
@@ -396,12 +92,36 @@ class Builder {
|
|
|
396
92
|
* @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Output Validation Docs}
|
|
397
93
|
*/
|
|
398
94
|
output(schema) {
|
|
399
|
-
return new
|
|
95
|
+
return new ProcedureBuilder({
|
|
400
96
|
...this["~orpc"],
|
|
401
|
-
|
|
97
|
+
schemas: {
|
|
98
|
+
...this["~orpc"].schemas,
|
|
99
|
+
outputSchema: schema
|
|
100
|
+
},
|
|
402
101
|
outputValidationIndex: this["~orpc"].middlewares.length
|
|
403
102
|
});
|
|
404
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Uses a middleware to modify the context or improve the pipeline.
|
|
106
|
+
*
|
|
107
|
+
* @info Supports both normal middleware and inline middleware implementations.
|
|
108
|
+
* @note The current context must be satisfy middleware dependent-context
|
|
109
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
110
|
+
*/
|
|
111
|
+
use(middleware) {
|
|
112
|
+
return new this.constructor({
|
|
113
|
+
...this["~orpc"],
|
|
114
|
+
middlewares: addMiddleware(this["~orpc"].middlewares, middleware)
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
useGating(gates, isGateEnabled) {
|
|
118
|
+
return this.use(({ next, context }) => {
|
|
119
|
+
return gatingContext.run(
|
|
120
|
+
(gate) => isGateEnabled(gate, context),
|
|
121
|
+
() => next({ context: { isGateEnabled } })
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
405
125
|
/**
|
|
406
126
|
* Defines the handler of the procedure.
|
|
407
127
|
*
|
|
@@ -413,6 +133,8 @@ class Builder {
|
|
|
413
133
|
handler
|
|
414
134
|
});
|
|
415
135
|
}
|
|
136
|
+
}
|
|
137
|
+
class BuilderWithMiddlewares extends ProcedureBuilder {
|
|
416
138
|
/**
|
|
417
139
|
* Prefixes all procedures in the router.
|
|
418
140
|
* The provided prefix is post-appended to any existing router prefix.
|
|
@@ -422,7 +144,7 @@ class Builder {
|
|
|
422
144
|
* @see {@link https://orpc.unnoq.com/docs/openapi/routing#route-prefixes OpenAPI Route Prefixes Docs}
|
|
423
145
|
*/
|
|
424
146
|
prefix(prefix) {
|
|
425
|
-
return new
|
|
147
|
+
return new this.constructor({
|
|
426
148
|
...this["~orpc"],
|
|
427
149
|
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
428
150
|
});
|
|
@@ -434,7 +156,7 @@ class Builder {
|
|
|
434
156
|
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
435
157
|
*/
|
|
436
158
|
tag(...tags) {
|
|
437
|
-
return new
|
|
159
|
+
return new this.constructor({
|
|
438
160
|
...this["~orpc"],
|
|
439
161
|
tags: mergeTags(this["~orpc"].tags, tags)
|
|
440
162
|
});
|
|
@@ -447,33 +169,79 @@ class Builder {
|
|
|
447
169
|
router(router) {
|
|
448
170
|
return enhanceRouter(router, this["~orpc"]);
|
|
449
171
|
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function decorateMiddleware(middleware) {
|
|
175
|
+
const decorated = ((...args) => middleware(...args));
|
|
176
|
+
decorated.mapInput = (mapInput) => {
|
|
177
|
+
const mapped = decorateMiddleware(
|
|
178
|
+
(options, input, ...rest) => middleware(options, mapInput(input), ...rest)
|
|
179
|
+
);
|
|
180
|
+
return mapped;
|
|
181
|
+
};
|
|
182
|
+
decorated.concat = (concatMiddleware, mapInput) => {
|
|
183
|
+
const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
|
|
184
|
+
const concatted = decorateMiddleware((options, input, output, ...rest) => {
|
|
185
|
+
const merged = middleware(
|
|
186
|
+
{
|
|
187
|
+
...options,
|
|
188
|
+
next: (...[nextOptions1]) => mapped(
|
|
189
|
+
{
|
|
190
|
+
...options,
|
|
191
|
+
context: { ...options.context, ...nextOptions1?.context },
|
|
192
|
+
next: (...[nextOptions2]) => options.next({ context: { ...nextOptions1?.context, ...nextOptions2?.context } })
|
|
193
|
+
},
|
|
194
|
+
input,
|
|
195
|
+
output,
|
|
196
|
+
...rest
|
|
197
|
+
)
|
|
198
|
+
},
|
|
199
|
+
input,
|
|
200
|
+
output,
|
|
201
|
+
...rest
|
|
202
|
+
);
|
|
203
|
+
return merged;
|
|
204
|
+
});
|
|
205
|
+
return concatted;
|
|
206
|
+
};
|
|
207
|
+
return decorated;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
class Builder extends BuilderWithMiddlewares {
|
|
211
|
+
/**
|
|
212
|
+
* Set or override the initial context.
|
|
213
|
+
*
|
|
214
|
+
* @see {@link https://orpc.unnoq.com/docs/context Context Docs}
|
|
215
|
+
*/
|
|
216
|
+
$context() {
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
450
219
|
/**
|
|
451
|
-
*
|
|
452
|
-
* And applies all of the previously defined options to the specified router.
|
|
220
|
+
* Creates a middleware.
|
|
453
221
|
*
|
|
454
|
-
* @see {@link https://orpc.unnoq.com/docs/
|
|
222
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
455
223
|
*/
|
|
456
|
-
|
|
457
|
-
return
|
|
224
|
+
middleware(middleware) {
|
|
225
|
+
return decorateMiddleware(middleware);
|
|
458
226
|
}
|
|
459
227
|
}
|
|
460
228
|
function createApiBuilder(opts = {}) {
|
|
461
|
-
|
|
229
|
+
return new Builder({
|
|
462
230
|
route: {},
|
|
463
231
|
meta: opts.meta ?? {},
|
|
464
232
|
errorMap: {},
|
|
465
233
|
inputValidationIndex: 0,
|
|
466
234
|
outputValidationIndex: 0,
|
|
467
235
|
middlewares: [
|
|
468
|
-
onError((error,
|
|
236
|
+
onError((error, _options) => {
|
|
469
237
|
console.dir(error, { depth: null });
|
|
470
238
|
})
|
|
471
239
|
],
|
|
240
|
+
schemas: initialSchemas,
|
|
472
241
|
// NB: this is a relic from orpc -- I'm not convinced there's a need for this (or if there is, that it's
|
|
473
242
|
// the best solution). For now I've removed the interface to configure it externally.
|
|
474
243
|
dedupeLeadingMiddlewares: true
|
|
475
244
|
});
|
|
476
|
-
return base;
|
|
477
245
|
}
|
|
478
246
|
const os = createApiBuilder();
|
|
479
247
|
|
|
@@ -511,17 +279,6 @@ function implementerInternal(contract, middlewares) {
|
|
|
511
279
|
});
|
|
512
280
|
return setHiddenRouterContract(adapted, contract);
|
|
513
281
|
};
|
|
514
|
-
} else if (key === "lazyRoute") {
|
|
515
|
-
method = (loader) => {
|
|
516
|
-
const adapted = enhanceRouter(lazy(loader), {
|
|
517
|
-
middlewares,
|
|
518
|
-
errorMap: {},
|
|
519
|
-
prefix: void 0,
|
|
520
|
-
tags: void 0,
|
|
521
|
-
dedupeLeadingMiddlewares: true
|
|
522
|
-
});
|
|
523
|
-
return setHiddenRouterContract(adapted, contract);
|
|
524
|
-
};
|
|
525
282
|
}
|
|
526
283
|
const next = getContractRouter(target, [key]);
|
|
527
284
|
if (!next) {
|
|
@@ -563,7 +320,7 @@ function implement(contract) {
|
|
|
563
320
|
}
|
|
564
321
|
|
|
565
322
|
function createAssertedLazyProcedure(lazied) {
|
|
566
|
-
const lazyProcedure =
|
|
323
|
+
const lazyProcedure = lazyInternal(async () => {
|
|
567
324
|
const { default: maybeProcedure } = await unlazy(lazied);
|
|
568
325
|
if (!isProcedure(maybeProcedure)) {
|
|
569
326
|
throw new Error(`
|
|
@@ -614,4 +371,4 @@ function createRouterClient(router, ...rest) {
|
|
|
614
371
|
return recursive;
|
|
615
372
|
}
|
|
616
373
|
|
|
617
|
-
export { Builder, DecoratedProcedure, Procedure, addMiddleware, call,
|
|
374
|
+
export { Builder, BuilderWithMiddlewares, DecoratedProcedure, Procedure, ProcedureBuilder, addMiddleware, call, createApiBuilder, createAssertedLazyProcedure, createContractedProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, getLazyMeta, getRouter, implement, implementerInternal, isLazy, isProcedure, lazyInternal, os, setHiddenRouterContract, unlazy };
|