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