@temporary-name/server 1.9.3-alpha.03f5d40e5b399f85012c2fb4e98167e26d551d36 → 1.9.3-alpha.0f2e1f4d66464608b85c66977bff51174cbb238f
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 -4
- package/dist/adapters/aws-lambda/index.d.ts +3 -4
- package/dist/adapters/aws-lambda/index.mjs +3 -3
- package/dist/adapters/fetch/index.d.mts +3 -4
- package/dist/adapters/fetch/index.d.ts +3 -4
- package/dist/adapters/fetch/index.mjs +3 -3
- package/dist/adapters/node/index.d.mts +3 -4
- package/dist/adapters/node/index.d.ts +3 -4
- package/dist/adapters/node/index.mjs +3 -3
- package/dist/adapters/standard/index.d.mts +10 -6
- package/dist/adapters/standard/index.d.ts +10 -6
- package/dist/adapters/standard/index.mjs +4 -4
- package/dist/helpers/index.mjs +3 -29
- package/dist/index.d.mts +73 -126
- package/dist/index.d.ts +73 -126
- package/dist/index.mjs +125 -234
- package/dist/openapi/index.d.mts +4 -4
- package/dist/openapi/index.d.ts +4 -4
- package/dist/openapi/index.mjs +61 -128
- package/dist/plugins/index.d.mts +5 -83
- package/dist/plugins/index.d.ts +5 -83
- package/dist/plugins/index.mjs +17 -189
- package/dist/shared/{server.Bk5r0-2R.d.ts → server.25yUS-xw.d.mts} +3 -4
- package/dist/shared/{server.Bs6ka_UE.d.mts → server.BKwU5-Ea.d.mts} +2 -2
- package/dist/shared/server.C1RJffw4.mjs +30 -0
- package/dist/shared/{server.CbLTWfgn.d.mts → server.Co-zpS8Y.d.ts} +3 -4
- package/dist/shared/{server.BVxcyR6X.mjs → server.DRYRuXpK.mjs} +17 -50
- package/dist/shared/{server.D2UFMrxf.d.ts → server.DV_PWBS2.d.ts} +2 -2
- package/dist/shared/server.DWEp52Gx.mjs +379 -0
- package/dist/shared/{server.BEHw7Eyx.mjs → server.JtIZ8YG7.mjs} +1 -11
- package/dist/shared/server.VtI8pLxV.d.mts +242 -0
- package/dist/shared/server.VtI8pLxV.d.ts +242 -0
- package/package.json +10 -22
- package/dist/shared/server.CZNLCQBm.d.mts +0 -192
- package/dist/shared/server.CZNLCQBm.d.ts +0 -192
- package/dist/shared/server.DcfsPloY.mjs +0 -202
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { isContractProcedure, mergePrefix, enhanceRoute, ValidationError } from '@temporary-name/contract';
|
|
2
|
+
import { resolveMaybeOptionalOptions, toArray, value, runWithSpan, intercept, isAsyncIteratorObject, overlayProxy, asyncIteratorWithSpan, ORPCError } from '@temporary-name/shared';
|
|
3
|
+
import { HibernationEventIterator, mapEventIterator } from '@temporary-name/standard-server';
|
|
4
|
+
import { safeDecodeAsync, safeEncodeAsync } from '@temporary-name/zod';
|
|
5
|
+
|
|
6
|
+
function isStartWithMiddlewares(middlewares, compare) {
|
|
7
|
+
if (compare.length > middlewares.length) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
for (let i = 0; i < middlewares.length; i++) {
|
|
11
|
+
if (compare[i] === void 0) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
if (middlewares[i] !== compare[i]) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
function mergeMiddlewares(first, second, options) {
|
|
21
|
+
if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
|
|
22
|
+
return second;
|
|
23
|
+
}
|
|
24
|
+
return [...first, ...second];
|
|
25
|
+
}
|
|
26
|
+
function addMiddleware(middlewares, addition) {
|
|
27
|
+
return [...middlewares, addition];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
class Procedure {
|
|
31
|
+
/**
|
|
32
|
+
* This property holds the defined options.
|
|
33
|
+
*/
|
|
34
|
+
"~orpc";
|
|
35
|
+
constructor(def) {
|
|
36
|
+
this["~orpc"] = def;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function isProcedure(item) {
|
|
40
|
+
if (item instanceof Procedure) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "handler" in item["~orpc"];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function mergeCurrentContext(context, other) {
|
|
47
|
+
return { ...context, ...other };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
|
|
51
|
+
function setHiddenRouterContract(router, contract) {
|
|
52
|
+
return new Proxy(router, {
|
|
53
|
+
get(target, key) {
|
|
54
|
+
if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
|
|
55
|
+
return contract;
|
|
56
|
+
}
|
|
57
|
+
return Reflect.get(target, key);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function getHiddenRouterContract(router) {
|
|
62
|
+
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function getRouter(router, path) {
|
|
66
|
+
let current = router;
|
|
67
|
+
for (let i = 0; i < path.length; i++) {
|
|
68
|
+
const segment = path[i];
|
|
69
|
+
if (!current) {
|
|
70
|
+
return void 0;
|
|
71
|
+
}
|
|
72
|
+
if (isProcedure(current)) {
|
|
73
|
+
return void 0;
|
|
74
|
+
}
|
|
75
|
+
if (!isLazy(current)) {
|
|
76
|
+
current = current[segment];
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const lazied = current;
|
|
80
|
+
const rest = path.slice(i);
|
|
81
|
+
return lazyInternal(async () => {
|
|
82
|
+
const unwrapped = await unlazy(lazied);
|
|
83
|
+
const next = getRouter(unwrapped.default, rest);
|
|
84
|
+
return unlazy(next);
|
|
85
|
+
}, getLazyMeta(lazied));
|
|
86
|
+
}
|
|
87
|
+
return current;
|
|
88
|
+
}
|
|
89
|
+
function createAccessibleLazyRouter(lazied) {
|
|
90
|
+
const recursive = new Proxy(lazied, {
|
|
91
|
+
get(target, key) {
|
|
92
|
+
if (typeof key !== "string") {
|
|
93
|
+
return Reflect.get(target, key);
|
|
94
|
+
}
|
|
95
|
+
const next = getRouter(lazied, [key]);
|
|
96
|
+
return createAccessibleLazyRouter(next);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
return recursive;
|
|
100
|
+
}
|
|
101
|
+
function enhanceRouter(router, options) {
|
|
102
|
+
if (isLazy(router)) {
|
|
103
|
+
const laziedMeta = getLazyMeta(router);
|
|
104
|
+
const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
|
|
105
|
+
const enhanced2 = lazyInternal(
|
|
106
|
+
async () => {
|
|
107
|
+
const { default: unlaziedRouter } = await unlazy(router);
|
|
108
|
+
const enhanced3 = enhanceRouter(unlaziedRouter, options);
|
|
109
|
+
return unlazy(enhanced3);
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
...laziedMeta,
|
|
113
|
+
prefix: enhancedPrefix
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
const accessible = createAccessibleLazyRouter(enhanced2);
|
|
117
|
+
return accessible;
|
|
118
|
+
}
|
|
119
|
+
if (isProcedure(router)) {
|
|
120
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, {
|
|
121
|
+
dedupeLeading: options.dedupeLeadingMiddlewares
|
|
122
|
+
});
|
|
123
|
+
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
|
124
|
+
const enhanced2 = new Procedure({
|
|
125
|
+
...router["~orpc"],
|
|
126
|
+
route: enhanceRoute(router["~orpc"].route, options),
|
|
127
|
+
middlewares: newMiddlewares,
|
|
128
|
+
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
|
129
|
+
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
|
130
|
+
});
|
|
131
|
+
return enhanced2;
|
|
132
|
+
}
|
|
133
|
+
const enhanced = {};
|
|
134
|
+
for (const key in router) {
|
|
135
|
+
enhanced[key] = enhanceRouter(router[key], options);
|
|
136
|
+
}
|
|
137
|
+
return enhanced;
|
|
138
|
+
}
|
|
139
|
+
function traverseContractProcedures(options, callback, lazyOptions = []) {
|
|
140
|
+
let currentRouter = options.router;
|
|
141
|
+
const hiddenContract = getHiddenRouterContract(options.router);
|
|
142
|
+
if (hiddenContract !== void 0) {
|
|
143
|
+
currentRouter = hiddenContract;
|
|
144
|
+
}
|
|
145
|
+
if (isLazy(currentRouter)) {
|
|
146
|
+
lazyOptions.push({
|
|
147
|
+
router: currentRouter,
|
|
148
|
+
path: options.path
|
|
149
|
+
});
|
|
150
|
+
} else if (isContractProcedure(currentRouter)) {
|
|
151
|
+
callback({
|
|
152
|
+
contract: currentRouter,
|
|
153
|
+
path: options.path
|
|
154
|
+
});
|
|
155
|
+
} else {
|
|
156
|
+
for (const key in currentRouter) {
|
|
157
|
+
traverseContractProcedures(
|
|
158
|
+
{
|
|
159
|
+
router: currentRouter[key],
|
|
160
|
+
path: [...options.path, key]
|
|
161
|
+
},
|
|
162
|
+
callback,
|
|
163
|
+
lazyOptions
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return lazyOptions;
|
|
168
|
+
}
|
|
169
|
+
async function resolveContractProcedures(options, callback) {
|
|
170
|
+
const pending = [options];
|
|
171
|
+
for (const options2 of pending) {
|
|
172
|
+
const lazyOptions = traverseContractProcedures(options2, callback);
|
|
173
|
+
for (const options3 of lazyOptions) {
|
|
174
|
+
const { default: router } = await unlazy(options3.router);
|
|
175
|
+
pending.push({
|
|
176
|
+
router,
|
|
177
|
+
path: options3.path
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async function unlazyRouter(router) {
|
|
183
|
+
if (isProcedure(router)) {
|
|
184
|
+
return router;
|
|
185
|
+
}
|
|
186
|
+
const unlazied = {};
|
|
187
|
+
for (const key in router) {
|
|
188
|
+
const item = router[key];
|
|
189
|
+
const { default: unlaziedRouter } = await unlazy(item);
|
|
190
|
+
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
|
191
|
+
}
|
|
192
|
+
return unlazied;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
|
|
196
|
+
function lazyInternal(loader, meta = {}) {
|
|
197
|
+
return {
|
|
198
|
+
[LAZY_SYMBOL]: {
|
|
199
|
+
loader,
|
|
200
|
+
meta
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function lazy(prefix, loader) {
|
|
205
|
+
return enhanceRouter(lazyInternal(loader), {
|
|
206
|
+
middlewares: [],
|
|
207
|
+
dedupeLeadingMiddlewares: true,
|
|
208
|
+
prefix
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
function isLazy(item) {
|
|
212
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
|
|
213
|
+
}
|
|
214
|
+
function getLazyMeta(lazied) {
|
|
215
|
+
return lazied[LAZY_SYMBOL].meta;
|
|
216
|
+
}
|
|
217
|
+
function unlazy(lazied) {
|
|
218
|
+
return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function middlewareOutputFn(output) {
|
|
222
|
+
return { output, context: {} };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function createProcedureClient(lazyableProcedure, ...rest) {
|
|
226
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
227
|
+
return async (...[input, callerOptions]) => {
|
|
228
|
+
const path = toArray(options.path);
|
|
229
|
+
const { default: procedure } = await unlazy(lazyableProcedure);
|
|
230
|
+
const clientContext = callerOptions?.context ?? {};
|
|
231
|
+
const context = await value(options.context ?? {}, clientContext);
|
|
232
|
+
const output = await runWithSpan({ name: "call_procedure", signal: callerOptions?.signal }, (span) => {
|
|
233
|
+
span?.setAttribute("procedure.path", [...path]);
|
|
234
|
+
return intercept(
|
|
235
|
+
toArray(options.interceptors),
|
|
236
|
+
{
|
|
237
|
+
context,
|
|
238
|
+
input,
|
|
239
|
+
path,
|
|
240
|
+
procedure,
|
|
241
|
+
request: callerOptions?.request,
|
|
242
|
+
signal: callerOptions?.signal,
|
|
243
|
+
lastEventId: callerOptions?.lastEventId
|
|
244
|
+
},
|
|
245
|
+
(interceptorOptions) => {
|
|
246
|
+
const { input: input2, ...opts } = interceptorOptions;
|
|
247
|
+
return executeProcedureInternal(interceptorOptions.procedure, input2, opts);
|
|
248
|
+
}
|
|
249
|
+
);
|
|
250
|
+
});
|
|
251
|
+
if (isAsyncIteratorObject(output)) {
|
|
252
|
+
if (output instanceof HibernationEventIterator) {
|
|
253
|
+
return output;
|
|
254
|
+
}
|
|
255
|
+
return overlayProxy(
|
|
256
|
+
output,
|
|
257
|
+
mapEventIterator(
|
|
258
|
+
asyncIteratorWithSpan(
|
|
259
|
+
{ name: "consume_event_iterator_output", signal: callerOptions?.signal },
|
|
260
|
+
output
|
|
261
|
+
),
|
|
262
|
+
{
|
|
263
|
+
value: (v) => v,
|
|
264
|
+
error: async (e) => e
|
|
265
|
+
}
|
|
266
|
+
)
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
return output;
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
async function validateInput(procedure, input) {
|
|
273
|
+
const schemas = procedure["~orpc"].schemas;
|
|
274
|
+
return runWithSpan({ name: "validate_input" }, async () => {
|
|
275
|
+
const resultBody = await safeDecodeAsync(schemas.bodySchema, input.body, { parseType: "body" });
|
|
276
|
+
const resultPath = await safeDecodeAsync(schemas.pathSchema, input.path, { parseType: "path" });
|
|
277
|
+
const resultQuery = await safeDecodeAsync(schemas.querySchema, input.query, { parseType: "query" });
|
|
278
|
+
const issues = [];
|
|
279
|
+
if (!resultBody.success) {
|
|
280
|
+
issues.push(...resultBody.error.issues.map((i) => ({ ...i, path: ["body", ...i.path] })));
|
|
281
|
+
}
|
|
282
|
+
if (!resultPath.success) {
|
|
283
|
+
issues.push(...resultPath.error.issues.map((i) => ({ ...i, path: ["path", ...i.path] })));
|
|
284
|
+
}
|
|
285
|
+
if (!resultQuery.success) {
|
|
286
|
+
issues.push(...resultQuery.error.issues.map((i) => ({ ...i, path: ["query", ...i.path] })));
|
|
287
|
+
}
|
|
288
|
+
if (issues.length > 0) {
|
|
289
|
+
throw new ORPCError("BAD_REQUEST", {
|
|
290
|
+
message: "Input validation failed",
|
|
291
|
+
data: {
|
|
292
|
+
issues
|
|
293
|
+
},
|
|
294
|
+
cause: new ValidationError({
|
|
295
|
+
message: "Input validation failed",
|
|
296
|
+
issues,
|
|
297
|
+
data: input
|
|
298
|
+
})
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
const results = {
|
|
302
|
+
body: resultBody.data,
|
|
303
|
+
path: resultPath.data,
|
|
304
|
+
query: resultQuery.data
|
|
305
|
+
};
|
|
306
|
+
return results;
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
async function validateOutput(procedure, output) {
|
|
310
|
+
const schema = procedure["~orpc"].schemas.outputSchema;
|
|
311
|
+
if (!schema) {
|
|
312
|
+
return output;
|
|
313
|
+
}
|
|
314
|
+
return runWithSpan({ name: "validate_output" }, async () => {
|
|
315
|
+
const result = await safeEncodeAsync(schema, output, { parseType: "output" });
|
|
316
|
+
if (!result.success) {
|
|
317
|
+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
|
318
|
+
message: "Output validation failed",
|
|
319
|
+
cause: new ValidationError({
|
|
320
|
+
message: "Output validation failed",
|
|
321
|
+
issues: result.error.issues,
|
|
322
|
+
data: output
|
|
323
|
+
})
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
return result.data;
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
async function executeProcedureInternal(procedure, input, options) {
|
|
330
|
+
const middlewares = procedure["~orpc"].middlewares;
|
|
331
|
+
const inputValidationIndex = Math.min(
|
|
332
|
+
Math.max(0, procedure["~orpc"].inputValidationIndex),
|
|
333
|
+
middlewares.length
|
|
334
|
+
);
|
|
335
|
+
const outputValidationIndex = Math.min(
|
|
336
|
+
Math.max(0, procedure["~orpc"].outputValidationIndex),
|
|
337
|
+
middlewares.length
|
|
338
|
+
);
|
|
339
|
+
const next = async (index, context, input2) => {
|
|
340
|
+
let currentInput = input2;
|
|
341
|
+
if (index === inputValidationIndex) {
|
|
342
|
+
currentInput = await validateInput(procedure, currentInput);
|
|
343
|
+
}
|
|
344
|
+
const mid = middlewares[index];
|
|
345
|
+
const output = mid ? await runWithSpan({ name: `middleware.${mid.name}`, signal: options.signal }, async (span) => {
|
|
346
|
+
span?.setAttribute("middleware.index", index);
|
|
347
|
+
span?.setAttribute("middleware.name", mid.name);
|
|
348
|
+
const result = await mid(
|
|
349
|
+
{
|
|
350
|
+
...options,
|
|
351
|
+
context,
|
|
352
|
+
next: async (...[nextOptions]) => {
|
|
353
|
+
const nextContext = nextOptions?.context ?? {};
|
|
354
|
+
return {
|
|
355
|
+
output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
|
|
356
|
+
// NB: Pretty sure this isn't used (or meant to be used) at runtime, it's just there
|
|
357
|
+
// to get type inference in the builder (via the caller returning the output of next() in
|
|
358
|
+
// the middleware function)
|
|
359
|
+
context: nextContext
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
currentInput,
|
|
364
|
+
middlewareOutputFn
|
|
365
|
+
);
|
|
366
|
+
return result.output;
|
|
367
|
+
}) : await runWithSpan(
|
|
368
|
+
{ name: "handler", signal: options.signal },
|
|
369
|
+
() => procedure["~orpc"].handler(currentInput, { ...options, context })
|
|
370
|
+
);
|
|
371
|
+
if (index === outputValidationIndex) {
|
|
372
|
+
return await validateOutput(procedure, output);
|
|
373
|
+
}
|
|
374
|
+
return output;
|
|
375
|
+
};
|
|
376
|
+
return next(0, options.context, input);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export { LAZY_SYMBOL as L, Procedure as P, addMiddleware as a, isLazy as b, createProcedureClient as c, getRouter as d, enhanceRouter as e, lazy as f, getLazyMeta as g, middlewareOutputFn as h, isProcedure as i, isStartWithMiddlewares as j, mergeMiddlewares as k, lazyInternal as l, mergeCurrentContext as m, getHiddenRouterContract as n, createAccessibleLazyRouter as o, unlazyRouter as p, resolveContractProcedures as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u };
|
|
@@ -234,14 +234,4 @@ function deserialize(data) {
|
|
|
234
234
|
return data;
|
|
235
235
|
}
|
|
236
236
|
|
|
237
|
-
|
|
238
|
-
return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
|
|
239
|
-
}
|
|
240
|
-
function getDynamicParams(path) {
|
|
241
|
-
return path ? standardizeHTTPPath(path).match(/\/\{[^}]+\}/g)?.map((v) => ({
|
|
242
|
-
raw: v,
|
|
243
|
-
name: v.match(/\{\+?([^}]+)\}/)[1]
|
|
244
|
-
})) : void 0;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
export { standardizeHTTPPath as a, deserialize as d, getDynamicParams as g, jsonSerialize as j, serialize as s };
|
|
237
|
+
export { bracketNotationDeserialize as b, deserialize as d, jsonSerialize as j, serialize as s };
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { Meta, Schemas, ContractProcedureDef, AnyContractRouter, ContractProcedure, InferProcedureClientInputs, InferSchemaOutput, EnhanceRouteOptions, AnyContractProcedure, ContractRouter, AnySchema } from '@temporary-name/contract';
|
|
2
|
+
import { Promisable, StandardLazyRequest, HTTPPath, ClientContext, Interceptor, Value, Client, MaybeOptionalOptions } from '@temporary-name/shared';
|
|
3
|
+
|
|
4
|
+
type Context = Record<PropertyKey, any>;
|
|
5
|
+
type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
|
|
6
|
+
type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
|
|
7
|
+
type BuildContextWithAuth<TContext extends Context, TAuthContext> = MergedCurrentContext<TContext, {
|
|
8
|
+
auth: TAuthContext | ('auth' extends keyof TContext ? TContext['auth'] : never);
|
|
9
|
+
}>;
|
|
10
|
+
declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
|
|
11
|
+
|
|
12
|
+
type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
|
13
|
+
output: TOutput;
|
|
14
|
+
context: TOutContext;
|
|
15
|
+
}>;
|
|
16
|
+
interface MiddlewareNextFn<TOutput> {
|
|
17
|
+
<U extends Context = {}>(options?: {
|
|
18
|
+
context?: U;
|
|
19
|
+
}): MiddlewareResult<U, TOutput>;
|
|
20
|
+
}
|
|
21
|
+
interface MiddlewareOutputFn<TOutput> {
|
|
22
|
+
(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
|
23
|
+
}
|
|
24
|
+
interface MiddlewareOptions<TInContext extends Context, TOutput, TMeta extends Meta> extends ProcedureHandlerOptions<TInContext, TMeta> {
|
|
25
|
+
next: MiddlewareNextFn<TOutput>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A function that represents a middleware.
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
31
|
+
*/
|
|
32
|
+
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TMeta extends Meta> {
|
|
33
|
+
(options: MiddlewareOptions<TInContext, TOutput, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
|
34
|
+
}
|
|
35
|
+
type AnyMiddleware = Middleware<any, any, any, any, any>;
|
|
36
|
+
interface MapInputMiddleware<TInput, TMappedInput> {
|
|
37
|
+
(input: TInput): TMappedInput;
|
|
38
|
+
}
|
|
39
|
+
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
|
40
|
+
|
|
41
|
+
type DefaultProcedureHandlerOptions = ProcedureHandlerOptions<Context, Meta>;
|
|
42
|
+
interface ProcedureHandlerOptions<TCurrentContext extends Context, TMeta extends Meta> {
|
|
43
|
+
context: TCurrentContext;
|
|
44
|
+
path: readonly string[];
|
|
45
|
+
procedure: Procedure<Context, Context, Schemas, TMeta>;
|
|
46
|
+
signal?: AbortSignal;
|
|
47
|
+
request?: StandardLazyRequest;
|
|
48
|
+
lastEventId: string | undefined;
|
|
49
|
+
}
|
|
50
|
+
interface ProcedureHandler<TCurrentContext extends Context, TInput extends {
|
|
51
|
+
path: any;
|
|
52
|
+
query: any;
|
|
53
|
+
body: any;
|
|
54
|
+
}, THandlerOutput, TMeta extends Meta> {
|
|
55
|
+
(input: TInput, opt: ProcedureHandlerOptions<TCurrentContext, TMeta>): Promisable<THandlerOutput>;
|
|
56
|
+
}
|
|
57
|
+
interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TMeta extends Meta> extends ContractProcedureDef<TSchemas, TMeta> {
|
|
58
|
+
__initialContext?: (type: TInitialContext) => unknown;
|
|
59
|
+
middlewares: readonly AnyMiddleware[];
|
|
60
|
+
authConfigs: TypedAuthConfig[];
|
|
61
|
+
inputValidationIndex: number;
|
|
62
|
+
outputValidationIndex: number;
|
|
63
|
+
handler: ProcedureHandler<TCurrentContext, any, any, any>;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* This class represents a procedure.
|
|
67
|
+
*
|
|
68
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
|
69
|
+
*/
|
|
70
|
+
declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TMeta extends Meta> {
|
|
71
|
+
/**
|
|
72
|
+
* This property holds the defined options.
|
|
73
|
+
*/
|
|
74
|
+
'~orpc': ProcedureDef<TInitialContext, TCurrentContext, TSchemas, TMeta>;
|
|
75
|
+
constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TSchemas, TMeta>);
|
|
76
|
+
}
|
|
77
|
+
type AnyProcedure = Procedure<any, any, Schemas, any>;
|
|
78
|
+
declare function isProcedure(item: unknown): item is AnyProcedure;
|
|
79
|
+
|
|
80
|
+
type ValidatedAuthContext = {};
|
|
81
|
+
interface BasicAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> {
|
|
82
|
+
tokenPrefix?: string;
|
|
83
|
+
validate: (username: string, password: string, options: ProcedureHandlerOptions<TCurrentContext, TMeta>) => Promisable<TAuthContext>;
|
|
84
|
+
}
|
|
85
|
+
interface TokenAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> {
|
|
86
|
+
tokenPrefix?: string;
|
|
87
|
+
validate: (token: string, options: ProcedureHandlerOptions<TCurrentContext, TMeta>) => Promisable<TAuthContext>;
|
|
88
|
+
}
|
|
89
|
+
interface NamedTokenAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> extends TokenAuthConfig<TAuthContext, TCurrentContext, TMeta> {
|
|
90
|
+
name: string;
|
|
91
|
+
}
|
|
92
|
+
type AuthType = 'header' | 'query' | 'cookie' | 'bearer' | 'basic' | 'none';
|
|
93
|
+
type AuthConfig<TAuthType extends AuthType, TAuthContext extends ValidatedAuthContext = ValidatedAuthContext, TCurrentContext extends Context = Context, TMeta extends Meta = Meta> = TAuthType extends 'basic' ? BasicAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'bearer' ? TokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'header' ? NamedTokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'query' ? NamedTokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'cookie' ? NamedTokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'none' ? {} : never;
|
|
94
|
+
type TypedAuthConfig<TAuthType extends AuthType = AuthType> = {
|
|
95
|
+
type: TAuthType;
|
|
96
|
+
} & AuthConfig<TAuthType, object>;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Represents a router, which defines a hierarchical structure of procedures.
|
|
100
|
+
*
|
|
101
|
+
* @info A procedure is a router too.
|
|
102
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
|
|
103
|
+
*/
|
|
104
|
+
type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer USchemas, infer UMeta> ? Procedure<TInitialContext, any, USchemas, UMeta> : {
|
|
105
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
|
|
106
|
+
};
|
|
107
|
+
type AnyRouter = Router<any, any>;
|
|
108
|
+
type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
|
|
109
|
+
/**
|
|
110
|
+
* Infer all initial context of the router.
|
|
111
|
+
*
|
|
112
|
+
* @info A procedure is a router too.
|
|
113
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
114
|
+
*/
|
|
115
|
+
type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any> ? UInitialContext : {
|
|
116
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Infer all current context of the router.
|
|
120
|
+
*
|
|
121
|
+
* @info A procedure is a router too.
|
|
122
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
123
|
+
*/
|
|
124
|
+
type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any> ? UCurrentContext : {
|
|
125
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Infer all router inputs
|
|
129
|
+
*
|
|
130
|
+
* @info A procedure is a router too.
|
|
131
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
132
|
+
*/
|
|
133
|
+
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any> ? InferProcedureClientInputs<USchemas> : {
|
|
134
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Infer all router outputs
|
|
138
|
+
*
|
|
139
|
+
* @info A procedure is a router too.
|
|
140
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
141
|
+
*/
|
|
142
|
+
type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any> ? InferSchemaOutput<USchemas['outputSchema']> : {
|
|
143
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
declare function getRouter<T extends Lazyable<AnyRouter | undefined>>(router: T, path: readonly string[]): T extends Lazy<any> ? Lazy<AnyRouter | undefined> : Lazyable<AnyRouter | undefined>;
|
|
147
|
+
type AccessibleLazyRouter<T extends Lazyable<AnyRouter | undefined>> = T extends Lazy<infer U extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> ? AccessibleLazyRouter<U> : T extends AnyProcedure | undefined ? Lazy<T> : Lazy<T> & {
|
|
148
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? AccessibleLazyRouter<T[K]> : never;
|
|
149
|
+
};
|
|
150
|
+
declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
|
|
151
|
+
type EnhancedRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context> = T extends Lazy<infer U extends AnyRouter> ? AccessibleLazyRouter<EnhancedRouter<U, TInitialContext, TCurrentContext>> : T extends Procedure<infer UInitialContext, infer UCurrentContext, infer USchemas, infer UMeta> ? Procedure<MergedInitialContext<TInitialContext, UInitialContext, TCurrentContext>, UCurrentContext, USchemas, UMeta> : {
|
|
152
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? EnhancedRouter<T[K], TInitialContext, TCurrentContext> : never;
|
|
153
|
+
};
|
|
154
|
+
interface EnhanceRouterOptions extends EnhanceRouteOptions {
|
|
155
|
+
middlewares: readonly AnyMiddleware[];
|
|
156
|
+
dedupeLeadingMiddlewares: boolean;
|
|
157
|
+
}
|
|
158
|
+
declare function enhanceRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context>(router: T, options: EnhanceRouterOptions): EnhancedRouter<T, TInitialContext, TCurrentContext>;
|
|
159
|
+
interface TraverseContractProceduresOptions {
|
|
160
|
+
router: AnyContractRouter | AnyRouter;
|
|
161
|
+
path: readonly string[];
|
|
162
|
+
}
|
|
163
|
+
interface TraverseContractProcedureCallbackOptions {
|
|
164
|
+
contract: AnyContractProcedure | AnyProcedure;
|
|
165
|
+
path: readonly string[];
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* @deprecated Use `TraverseContractProcedureCallbackOptions` instead.
|
|
169
|
+
*/
|
|
170
|
+
type ContractProcedureCallbackOptions = TraverseContractProcedureCallbackOptions;
|
|
171
|
+
interface LazyTraverseContractProceduresOptions {
|
|
172
|
+
router: Lazy<AnyRouter>;
|
|
173
|
+
path: readonly string[];
|
|
174
|
+
}
|
|
175
|
+
declare function traverseContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void, lazyOptions?: LazyTraverseContractProceduresOptions[]): LazyTraverseContractProceduresOptions[];
|
|
176
|
+
declare function resolveContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void): Promise<void>;
|
|
177
|
+
type UnlaziedRouter<T extends AnyRouter> = T extends AnyProcedure ? T : {
|
|
178
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? UnlaziedRouter<U> : never;
|
|
179
|
+
};
|
|
180
|
+
declare function unlazyRouter<T extends AnyRouter>(router: T): Promise<UnlaziedRouter<T>>;
|
|
181
|
+
|
|
182
|
+
declare const LAZY_SYMBOL: unique symbol;
|
|
183
|
+
interface LazyMeta {
|
|
184
|
+
prefix?: HTTPPath;
|
|
185
|
+
}
|
|
186
|
+
interface Lazy<T> {
|
|
187
|
+
[LAZY_SYMBOL]: {
|
|
188
|
+
loader: () => Promise<{
|
|
189
|
+
default: T;
|
|
190
|
+
}>;
|
|
191
|
+
meta: LazyMeta;
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
type Lazyable<T> = T | Lazy<T>;
|
|
195
|
+
/**
|
|
196
|
+
* @internal
|
|
197
|
+
*/
|
|
198
|
+
declare function lazyInternal<T>(loader: () => Promise<{
|
|
199
|
+
default: T;
|
|
200
|
+
}>, meta?: LazyMeta): Lazy<T>;
|
|
201
|
+
/**
|
|
202
|
+
* Creates a lazy-loaded item.
|
|
203
|
+
*
|
|
204
|
+
* @warning The `prefix` in `meta` only holds metadata and does not apply the prefix to the lazy router, use `os.prefix(...).lazyRoute(...)` instead.
|
|
205
|
+
*/
|
|
206
|
+
declare function lazy<T extends Router<ContractRouter<{}>, any>>(prefix: HTTPPath, loader: () => Promise<{
|
|
207
|
+
default: T;
|
|
208
|
+
}>): EnhancedRouter<Lazy<T>, {}, {}>;
|
|
209
|
+
declare function isLazy(item: unknown): item is Lazy<any>;
|
|
210
|
+
declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
|
|
211
|
+
declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
|
|
212
|
+
default: T extends Lazy<infer U> ? U : T;
|
|
213
|
+
}>;
|
|
214
|
+
|
|
215
|
+
type ProcedureClient<TClientContext extends ClientContext, TSchemas extends Schemas> = Client<TClientContext, InferProcedureClientInputs<TSchemas>, InferSchemaOutput<TSchemas['outputSchema']>>;
|
|
216
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TMeta extends Meta> extends ProcedureHandlerOptions<TInitialContext, TMeta> {
|
|
217
|
+
input: {
|
|
218
|
+
path: unknown;
|
|
219
|
+
query: unknown;
|
|
220
|
+
body: unknown;
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TMeta extends Meta, TClientContext extends ClientContext> = {
|
|
224
|
+
/**
|
|
225
|
+
* This is helpful for logging and analytics.
|
|
226
|
+
*/
|
|
227
|
+
path?: readonly string[];
|
|
228
|
+
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TMeta>, Promise<InferSchemaOutput<TOutputSchema>>>[];
|
|
229
|
+
} & (Record<never, never> extends TInitialContext ? {
|
|
230
|
+
context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
231
|
+
} : {
|
|
232
|
+
context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
233
|
+
});
|
|
234
|
+
/**
|
|
235
|
+
* Create Server-side client from a procedure.
|
|
236
|
+
*
|
|
237
|
+
* @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
|
|
238
|
+
*/
|
|
239
|
+
declare function createProcedureClient<TInitialContext extends Context, TSchemas extends Schemas, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TSchemas, TMeta>>, ...rest: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TSchemas['outputSchema'], TMeta, TClientContext>>): ProcedureClient<TClientContext, TSchemas>;
|
|
240
|
+
|
|
241
|
+
export { isProcedure as G, createProcedureClient as J, Procedure as P, getRouter as S, createAccessibleLazyRouter as W, enhanceRouter as X, traverseContractProcedures as a0, resolveContractProcedures as a1, unlazyRouter as a3, mergeCurrentContext as m, LAZY_SYMBOL as n, lazyInternal as p, lazy as q, isLazy as r, getLazyMeta as s, unlazy as u, middlewareOutputFn as y };
|
|
242
|
+
export type { LazyTraverseContractProceduresOptions as $, AuthType as A, BuildContextWithAuth as B, Context as C, DefaultProcedureHandlerOptions as D, EnhanceRouterOptions as E, ProcedureDef as F, ProcedureClientInterceptorOptions as H, InferRouterInitialContext as I, InferRouterInitialContexts as K, Lazy as L, Middleware as M, InferRouterCurrentContexts as N, InferRouterInputs as O, InferRouterOutputs as Q, Router as R, TypedAuthConfig as T, AccessibleLazyRouter as U, ValidatedAuthContext as V, TraverseContractProceduresOptions as Y, TraverseContractProcedureCallbackOptions as Z, ContractProcedureCallbackOptions as _, CreateProcedureClientOptions as a, UnlaziedRouter as a2, ProcedureClient as b, MergedInitialContext as c, MergedCurrentContext as d, AuthConfig as e, ProcedureHandler as f, EnhancedRouter as g, MapInputMiddleware as h, AnyMiddleware as i, AnyProcedure as j, Lazyable as k, AnyRouter as l, LazyMeta as o, MiddlewareResult as t, MiddlewareNextFn as v, MiddlewareOutputFn as w, MiddlewareOptions as x, ProcedureHandlerOptions as z };
|