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