@orpc/server 0.0.0-next.85df466 → 0.0.0-next.8646d1e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -21
- package/dist/adapters/aws-lambda/index.d.mts +46 -0
- package/dist/adapters/aws-lambda/index.d.ts +46 -0
- package/dist/adapters/aws-lambda/index.mjs +42 -0
- package/dist/adapters/bun-ws/index.d.mts +36 -0
- package/dist/adapters/bun-ws/index.d.ts +36 -0
- package/dist/adapters/bun-ws/index.mjs +47 -0
- package/dist/adapters/crossws/index.d.mts +33 -0
- package/dist/adapters/crossws/index.d.ts +33 -0
- package/dist/adapters/crossws/index.mjs +47 -0
- package/dist/adapters/fetch/index.d.mts +63 -11
- package/dist/adapters/fetch/index.d.ts +63 -11
- package/dist/adapters/fetch/index.mjs +108 -7
- package/dist/adapters/message-port/index.d.mts +31 -0
- package/dist/adapters/message-port/index.d.ts +31 -0
- package/dist/adapters/message-port/index.mjs +41 -0
- package/dist/adapters/node/index.d.mts +65 -22
- package/dist/adapters/node/index.d.ts +65 -22
- package/dist/adapters/node/index.mjs +88 -20
- package/dist/adapters/standard/index.d.mts +13 -21
- package/dist/adapters/standard/index.d.ts +13 -21
- package/dist/adapters/standard/index.mjs +5 -4
- package/dist/adapters/standard-peer/index.d.mts +14 -0
- package/dist/adapters/standard-peer/index.d.ts +14 -0
- package/dist/adapters/standard-peer/index.mjs +7 -0
- package/dist/adapters/websocket/index.d.mts +54 -0
- package/dist/adapters/websocket/index.d.ts +54 -0
- package/dist/adapters/websocket/index.mjs +69 -0
- package/dist/adapters/ws/index.d.mts +31 -0
- package/dist/adapters/ws/index.d.ts +31 -0
- package/dist/adapters/ws/index.mjs +39 -0
- package/dist/helpers/index.d.mts +130 -0
- package/dist/helpers/index.d.ts +130 -0
- package/dist/helpers/index.mjs +182 -0
- package/dist/hibernation/index.d.mts +44 -0
- package/dist/hibernation/index.d.ts +44 -0
- package/dist/hibernation/index.mjs +65 -0
- package/dist/index.d.mts +679 -129
- package/dist/index.d.ts +679 -129
- package/dist/index.mjs +208 -52
- package/dist/plugins/index.d.mts +157 -19
- package/dist/plugins/index.d.ts +157 -19
- package/dist/plugins/index.mjs +190 -17
- package/dist/shared/server.B7b2w3_i.d.ts +12 -0
- package/dist/shared/server.BEFBl-Cb.d.mts +12 -0
- package/dist/shared/server.BU4WI18A.d.mts +32 -0
- package/dist/shared/server.BW-nUGgA.mjs +36 -0
- package/dist/shared/server.Bmh5xd4n.d.ts +74 -0
- package/dist/shared/server.C6Q5sqYw.mjs +20 -0
- package/dist/shared/server.CIL9uKTN.mjs +194 -0
- package/dist/shared/server.CYNGeoCm.d.mts +194 -0
- package/dist/shared/server.CYNGeoCm.d.ts +194 -0
- package/dist/shared/server.D0H-iaY3.d.ts +32 -0
- package/dist/shared/server.DZ5BIITo.mjs +9 -0
- package/dist/shared/server.DhJj-1X9.d.mts +42 -0
- package/dist/shared/{server.V6zT5iYQ.mjs → server.NeumLVdS.mjs} +172 -181
- package/dist/shared/server.gqRxT-yN.d.mts +74 -0
- package/dist/shared/server.jMTkVNIb.d.ts +42 -0
- package/package.json +70 -20
- package/dist/adapters/hono/index.d.mts +0 -20
- package/dist/adapters/hono/index.d.ts +0 -20
- package/dist/adapters/hono/index.mjs +0 -32
- package/dist/adapters/next/index.d.mts +0 -27
- package/dist/adapters/next/index.d.ts +0 -27
- package/dist/adapters/next/index.mjs +0 -29
- package/dist/shared/server.BBGuTxHE.mjs +0 -163
- package/dist/shared/server.BqEaivV1.d.ts +0 -9
- package/dist/shared/server.CHpDfeOK.d.mts +0 -77
- package/dist/shared/server.CI7U5gRZ.d.mts +0 -152
- package/dist/shared/server.CI7U5gRZ.d.ts +0 -152
- package/dist/shared/server.CUE4Aija.mjs +0 -24
- package/dist/shared/server.DSZ2XY8G.d.ts +0 -77
- package/dist/shared/server.MnOqRlBp.d.mts +0 -9
- package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
@@ -0,0 +1,42 @@
|
|
1
|
+
import { ErrorMap, EnhanceRouteOptions, MergedErrorMap, AnyContractRouter, AnyContractProcedure } from '@orpc/contract';
|
2
|
+
import { a as AnyMiddleware, L as Lazyable, b as AnyRouter, C as Context, c as Lazy, A as AnyProcedure, P as Procedure, M as MergedInitialContext } from './server.CYNGeoCm.mjs';
|
3
|
+
|
4
|
+
declare function getRouter<T extends Lazyable<AnyRouter | undefined>>(router: T, path: readonly string[]): T extends Lazy<any> ? Lazy<AnyRouter | undefined> : Lazyable<AnyRouter | undefined>;
|
5
|
+
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> & {
|
6
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? AccessibleLazyRouter<T[K]> : never;
|
7
|
+
};
|
8
|
+
declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
|
9
|
+
type EnhancedRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context, TErrorMap extends ErrorMap> = T extends Lazy<infer U extends AnyRouter> ? AccessibleLazyRouter<EnhancedRouter<U, TInitialContext, TCurrentContext, TErrorMap>> : T extends Procedure<infer UInitialContext, infer UCurrentContext, infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<MergedInitialContext<TInitialContext, UInitialContext, TCurrentContext>, UCurrentContext, UInputSchema, UOutputSchema, MergedErrorMap<TErrorMap, UErrorMap>, UMeta> : {
|
10
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? EnhancedRouter<T[K], TInitialContext, TCurrentContext, TErrorMap> : never;
|
11
|
+
};
|
12
|
+
interface EnhanceRouterOptions<TErrorMap extends ErrorMap> extends EnhanceRouteOptions {
|
13
|
+
middlewares: readonly AnyMiddleware[];
|
14
|
+
errorMap: TErrorMap;
|
15
|
+
dedupeLeadingMiddlewares: boolean;
|
16
|
+
}
|
17
|
+
declare function enhanceRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context, TErrorMap extends ErrorMap>(router: T, options: EnhanceRouterOptions<TErrorMap>): EnhancedRouter<T, TInitialContext, TCurrentContext, TErrorMap>;
|
18
|
+
interface TraverseContractProceduresOptions {
|
19
|
+
router: AnyContractRouter | AnyRouter;
|
20
|
+
path: readonly string[];
|
21
|
+
}
|
22
|
+
interface TraverseContractProcedureCallbackOptions {
|
23
|
+
contract: AnyContractProcedure | AnyProcedure;
|
24
|
+
path: readonly string[];
|
25
|
+
}
|
26
|
+
/**
|
27
|
+
* @deprecated Use `TraverseContractProcedureCallbackOptions` instead.
|
28
|
+
*/
|
29
|
+
type ContractProcedureCallbackOptions = TraverseContractProcedureCallbackOptions;
|
30
|
+
interface LazyTraverseContractProceduresOptions {
|
31
|
+
router: Lazy<AnyRouter>;
|
32
|
+
path: readonly string[];
|
33
|
+
}
|
34
|
+
declare function traverseContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void, lazyOptions?: LazyTraverseContractProceduresOptions[]): LazyTraverseContractProceduresOptions[];
|
35
|
+
declare function resolveContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void): Promise<void>;
|
36
|
+
type UnlaziedRouter<T extends AnyRouter> = T extends AnyProcedure ? T : {
|
37
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? UnlaziedRouter<U> : never;
|
38
|
+
};
|
39
|
+
declare function unlazyRouter<T extends AnyRouter>(router: T): Promise<UnlaziedRouter<T>>;
|
40
|
+
|
41
|
+
export { createAccessibleLazyRouter as c, enhanceRouter as e, getRouter as g, resolveContractProcedures as r, traverseContractProcedures as t, unlazyRouter as u };
|
42
|
+
export type { AccessibleLazyRouter as A, ContractProcedureCallbackOptions as C, EnhanceRouterOptions as E, LazyTraverseContractProceduresOptions as L, TraverseContractProcedureCallbackOptions as T, UnlaziedRouter as U, EnhancedRouter as a, TraverseContractProceduresOptions as b };
|
@@ -1,21 +1,54 @@
|
|
1
|
+
import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, enhanceRoute } from '@orpc/contract';
|
2
|
+
import { resolveMaybeOptionalOptions, toArray, value, intercept } from '@orpc/shared';
|
1
3
|
import { fallbackORPCErrorStatus, ORPCError } from '@orpc/client';
|
2
|
-
import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, adaptRoute } from '@orpc/contract';
|
3
|
-
import { value, intercept, toError } from '@orpc/shared';
|
4
4
|
|
5
|
-
const
|
6
|
-
function lazy(loader) {
|
5
|
+
const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
|
6
|
+
function lazy(loader, meta = {}) {
|
7
7
|
return {
|
8
|
-
[
|
8
|
+
[LAZY_SYMBOL]: {
|
9
|
+
loader,
|
10
|
+
meta
|
11
|
+
}
|
9
12
|
};
|
10
13
|
}
|
11
14
|
function isLazy(item) {
|
12
|
-
return (typeof item === "object" || typeof item === "function") && item !== null &&
|
15
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
|
16
|
+
}
|
17
|
+
function getLazyMeta(lazied) {
|
18
|
+
return lazied[LAZY_SYMBOL].meta;
|
13
19
|
}
|
14
20
|
function unlazy(lazied) {
|
15
|
-
return isLazy(lazied) ? lazied[
|
21
|
+
return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
|
22
|
+
}
|
23
|
+
|
24
|
+
function isStartWithMiddlewares(middlewares, compare) {
|
25
|
+
if (compare.length > middlewares.length) {
|
26
|
+
return false;
|
27
|
+
}
|
28
|
+
for (let i = 0; i < middlewares.length; i++) {
|
29
|
+
if (compare[i] === void 0) {
|
30
|
+
return true;
|
31
|
+
}
|
32
|
+
if (middlewares[i] !== compare[i]) {
|
33
|
+
return false;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
return true;
|
37
|
+
}
|
38
|
+
function mergeMiddlewares(first, second, options) {
|
39
|
+
if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
|
40
|
+
return second;
|
41
|
+
}
|
42
|
+
return [...first, ...second];
|
43
|
+
}
|
44
|
+
function addMiddleware(middlewares, addition) {
|
45
|
+
return [...middlewares, addition];
|
16
46
|
}
|
17
47
|
|
18
48
|
class Procedure {
|
49
|
+
/**
|
50
|
+
* This property holds the defined options.
|
51
|
+
*/
|
19
52
|
"~orpc";
|
20
53
|
constructor(def) {
|
21
54
|
this["~orpc"] = def;
|
@@ -28,50 +61,8 @@ function isProcedure(item) {
|
|
28
61
|
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
29
62
|
}
|
30
63
|
|
31
|
-
function
|
32
|
-
|
33
|
-
let current = await unlazy(lazied);
|
34
|
-
while (true) {
|
35
|
-
if (!isLazy(current.default)) {
|
36
|
-
break;
|
37
|
-
}
|
38
|
-
current = await unlazy(current.default);
|
39
|
-
}
|
40
|
-
return current;
|
41
|
-
};
|
42
|
-
return lazy(flattenLoader);
|
43
|
-
}
|
44
|
-
function createLazyProcedureFormAnyLazy(lazied) {
|
45
|
-
const lazyProcedure = lazy(async () => {
|
46
|
-
const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
|
47
|
-
if (!isProcedure(maybeProcedure)) {
|
48
|
-
throw new Error(`
|
49
|
-
Expected a lazy<procedure> but got lazy<unknown>.
|
50
|
-
This should be caught by TypeScript compilation.
|
51
|
-
Please report this issue if this makes you feel uncomfortable.
|
52
|
-
`);
|
53
|
-
}
|
54
|
-
return { default: maybeProcedure };
|
55
|
-
});
|
56
|
-
return lazyProcedure;
|
57
|
-
}
|
58
|
-
|
59
|
-
function dedupeMiddlewares(compare, middlewares) {
|
60
|
-
let min = 0;
|
61
|
-
for (let i = 0; i < middlewares.length; i++) {
|
62
|
-
const index = compare.indexOf(middlewares[i], min);
|
63
|
-
if (index === -1) {
|
64
|
-
return middlewares.slice(i);
|
65
|
-
}
|
66
|
-
min = index + 1;
|
67
|
-
}
|
68
|
-
return [];
|
69
|
-
}
|
70
|
-
function mergeMiddlewares(first, second) {
|
71
|
-
return [...first, ...dedupeMiddlewares(first, second)];
|
72
|
-
}
|
73
|
-
function addMiddleware(middlewares, addition) {
|
74
|
-
return [...middlewares, addition];
|
64
|
+
function mergeCurrentContext(context, other) {
|
65
|
+
return { ...context, ...other };
|
75
66
|
}
|
76
67
|
|
77
68
|
function createORPCErrorConstructorMap(errors) {
|
@@ -80,14 +71,15 @@ function createORPCErrorConstructorMap(errors) {
|
|
80
71
|
if (typeof code !== "string") {
|
81
72
|
return Reflect.get(target, code);
|
82
73
|
}
|
83
|
-
const item = (...
|
74
|
+
const item = (...rest) => {
|
75
|
+
const options = resolveMaybeOptionalOptions(rest);
|
84
76
|
const config = errors[code];
|
85
77
|
return new ORPCError(code, {
|
86
78
|
defined: Boolean(config),
|
87
79
|
status: config?.status,
|
88
|
-
message: options
|
89
|
-
data: options
|
90
|
-
cause: options
|
80
|
+
message: options.message ?? config?.message,
|
81
|
+
data: options.data,
|
82
|
+
cause: options.cause
|
91
83
|
});
|
92
84
|
};
|
93
85
|
return item;
|
@@ -115,16 +107,17 @@ function middlewareOutputFn(output) {
|
|
115
107
|
return { output, context: {} };
|
116
108
|
}
|
117
109
|
|
118
|
-
function createProcedureClient(lazyableProcedure, ...
|
110
|
+
function createProcedureClient(lazyableProcedure, ...rest) {
|
111
|
+
const options = resolveMaybeOptionalOptions(rest);
|
119
112
|
return async (...[input, callerOptions]) => {
|
120
|
-
const path = options
|
113
|
+
const path = toArray(options.path);
|
121
114
|
const { default: procedure } = await unlazy(lazyableProcedure);
|
122
115
|
const clientContext = callerOptions?.context ?? {};
|
123
|
-
const context = await value(options
|
116
|
+
const context = await value(options.context ?? {}, clientContext);
|
124
117
|
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
125
118
|
try {
|
126
119
|
return await intercept(
|
127
|
-
options
|
120
|
+
toArray(options.interceptors),
|
128
121
|
{
|
129
122
|
context,
|
130
123
|
input,
|
@@ -139,7 +132,7 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
|
|
139
132
|
);
|
140
133
|
} catch (e) {
|
141
134
|
if (!(e instanceof ORPCError)) {
|
142
|
-
throw
|
135
|
+
throw e;
|
143
136
|
}
|
144
137
|
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
145
138
|
throw validated;
|
@@ -181,193 +174,187 @@ async function executeProcedureInternal(procedure, options) {
|
|
181
174
|
const middlewares = procedure["~orpc"].middlewares;
|
182
175
|
const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
|
183
176
|
const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
|
184
|
-
|
185
|
-
|
186
|
-
let currentInput = options.input;
|
187
|
-
const next = async (...[nextOptions]) => {
|
188
|
-
const index = currentIndex;
|
189
|
-
currentIndex += 1;
|
190
|
-
currentContext = { ...currentContext, ...nextOptions?.context };
|
177
|
+
const next = async (index, context, input) => {
|
178
|
+
let currentInput = input;
|
191
179
|
if (index === inputValidationIndex) {
|
192
180
|
currentInput = await validateInput(procedure, currentInput);
|
193
181
|
}
|
194
182
|
const mid = middlewares[index];
|
195
|
-
const
|
183
|
+
const output = mid ? (await mid({
|
184
|
+
...options,
|
185
|
+
context,
|
186
|
+
next: async (...[nextOptions]) => {
|
187
|
+
const nextContext = nextOptions?.context ?? {};
|
188
|
+
return {
|
189
|
+
output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
|
190
|
+
context: nextContext
|
191
|
+
};
|
192
|
+
}
|
193
|
+
}, currentInput, middlewareOutputFn)).output : await procedure["~orpc"].handler({ ...options, context, input: currentInput });
|
196
194
|
if (index === outputValidationIndex) {
|
197
|
-
|
198
|
-
return {
|
199
|
-
...result,
|
200
|
-
output: validatedOutput
|
201
|
-
};
|
195
|
+
return await validateOutput(procedure, output);
|
202
196
|
}
|
203
|
-
return
|
197
|
+
return output;
|
204
198
|
};
|
205
|
-
return (
|
199
|
+
return next(0, options.context, options.input);
|
206
200
|
}
|
207
201
|
|
208
|
-
const
|
209
|
-
function
|
210
|
-
return new Proxy(
|
202
|
+
const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
|
203
|
+
function setHiddenRouterContract(router, contract) {
|
204
|
+
return new Proxy(router, {
|
211
205
|
get(target, key) {
|
212
|
-
if (key ===
|
206
|
+
if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
|
213
207
|
return contract;
|
214
208
|
}
|
215
209
|
return Reflect.get(target, key);
|
216
210
|
}
|
217
211
|
});
|
218
212
|
}
|
219
|
-
function
|
220
|
-
return
|
213
|
+
function getHiddenRouterContract(router) {
|
214
|
+
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
221
215
|
}
|
222
|
-
|
223
|
-
function
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
return deepSetLazyRouterPrefix(val, prefix);
|
230
|
-
}
|
231
|
-
return val;
|
232
|
-
}
|
233
|
-
return prefix;
|
216
|
+
|
217
|
+
function getRouter(router, path) {
|
218
|
+
let current = router;
|
219
|
+
for (let i = 0; i < path.length; i++) {
|
220
|
+
const segment = path[i];
|
221
|
+
if (!current) {
|
222
|
+
return void 0;
|
234
223
|
}
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
224
|
+
if (isProcedure(current)) {
|
225
|
+
return void 0;
|
226
|
+
}
|
227
|
+
if (!isLazy(current)) {
|
228
|
+
current = current[segment];
|
229
|
+
continue;
|
230
|
+
}
|
231
|
+
const lazied = current;
|
232
|
+
const rest = path.slice(i);
|
233
|
+
return lazy(async () => {
|
234
|
+
const unwrapped = await unlazy(lazied);
|
235
|
+
const next = getRouter(unwrapped.default, rest);
|
236
|
+
return unlazy(next);
|
237
|
+
}, getLazyMeta(lazied));
|
238
|
+
}
|
239
|
+
return current;
|
239
240
|
}
|
240
|
-
|
241
241
|
function createAccessibleLazyRouter(lazied) {
|
242
|
-
const
|
243
|
-
const recursive = new Proxy(flattenLazy, {
|
242
|
+
const recursive = new Proxy(lazied, {
|
244
243
|
get(target, key) {
|
245
244
|
if (typeof key !== "string") {
|
246
245
|
return Reflect.get(target, key);
|
247
246
|
}
|
248
|
-
const next =
|
247
|
+
const next = getRouter(lazied, [key]);
|
249
248
|
return createAccessibleLazyRouter(next);
|
250
249
|
}
|
251
250
|
});
|
252
251
|
return recursive;
|
253
252
|
}
|
254
|
-
|
255
|
-
function adaptRouter(router, options) {
|
253
|
+
function enhanceRouter(router, options) {
|
256
254
|
if (isLazy(router)) {
|
257
|
-
const
|
258
|
-
|
259
|
-
|
260
|
-
|
255
|
+
const laziedMeta = getLazyMeta(router);
|
256
|
+
const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
|
257
|
+
const enhanced2 = lazy(async () => {
|
258
|
+
const { default: unlaziedRouter } = await unlazy(router);
|
259
|
+
const enhanced3 = enhanceRouter(unlaziedRouter, options);
|
260
|
+
return unlazy(enhanced3);
|
261
|
+
}, {
|
262
|
+
...laziedMeta,
|
263
|
+
prefix: enhancedPrefix
|
261
264
|
});
|
262
|
-
const accessible = createAccessibleLazyRouter(
|
263
|
-
const currentPrefix = getLazyRouterPrefix(router);
|
264
|
-
const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
|
265
|
-
if (prefix) {
|
266
|
-
return deepSetLazyRouterPrefix(accessible, prefix);
|
267
|
-
}
|
265
|
+
const accessible = createAccessibleLazyRouter(enhanced2);
|
268
266
|
return accessible;
|
269
267
|
}
|
270
268
|
if (isProcedure(router)) {
|
271
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
|
269
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
|
272
270
|
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
273
|
-
const
|
271
|
+
const enhanced2 = new Procedure({
|
274
272
|
...router["~orpc"],
|
275
|
-
route:
|
273
|
+
route: enhanceRoute(router["~orpc"].route, options),
|
276
274
|
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
277
275
|
middlewares: newMiddlewares,
|
278
276
|
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
279
277
|
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
280
278
|
});
|
281
|
-
return
|
279
|
+
return enhanced2;
|
282
280
|
}
|
283
|
-
const
|
281
|
+
const enhanced = {};
|
284
282
|
for (const key in router) {
|
285
|
-
|
286
|
-
}
|
287
|
-
return adapted;
|
288
|
-
}
|
289
|
-
function getRouterChild(router, ...path) {
|
290
|
-
let current = router;
|
291
|
-
for (let i = 0; i < path.length; i++) {
|
292
|
-
const segment = path[i];
|
293
|
-
if (!current) {
|
294
|
-
return void 0;
|
295
|
-
}
|
296
|
-
if (isProcedure(current)) {
|
297
|
-
return void 0;
|
298
|
-
}
|
299
|
-
if (!isLazy(current)) {
|
300
|
-
current = current[segment];
|
301
|
-
continue;
|
302
|
-
}
|
303
|
-
const lazied = current;
|
304
|
-
const rest = path.slice(i);
|
305
|
-
const newLazy = lazy(async () => {
|
306
|
-
const unwrapped = await unlazy(lazied);
|
307
|
-
if (!unwrapped.default) {
|
308
|
-
return unwrapped;
|
309
|
-
}
|
310
|
-
const next = getRouterChild(unwrapped.default, ...rest);
|
311
|
-
return { default: next };
|
312
|
-
});
|
313
|
-
return flatLazy(newLazy);
|
283
|
+
enhanced[key] = enhanceRouter(router[key], options);
|
314
284
|
}
|
315
|
-
return
|
285
|
+
return enhanced;
|
316
286
|
}
|
317
|
-
|
318
|
-
|
319
|
-
const hiddenContract =
|
320
|
-
if (hiddenContract) {
|
321
|
-
|
322
|
-
{
|
323
|
-
router: hiddenContract,
|
324
|
-
path: options.path
|
325
|
-
},
|
326
|
-
callback,
|
327
|
-
laziedOptions
|
328
|
-
);
|
287
|
+
function traverseContractProcedures(options, callback, lazyOptions = []) {
|
288
|
+
let currentRouter = options.router;
|
289
|
+
const hiddenContract = getHiddenRouterContract(options.router);
|
290
|
+
if (hiddenContract !== void 0) {
|
291
|
+
currentRouter = hiddenContract;
|
329
292
|
}
|
330
|
-
if (isLazy(
|
331
|
-
|
332
|
-
|
293
|
+
if (isLazy(currentRouter)) {
|
294
|
+
lazyOptions.push({
|
295
|
+
router: currentRouter,
|
333
296
|
path: options.path
|
334
297
|
});
|
335
|
-
} else if (isContractProcedure(
|
298
|
+
} else if (isContractProcedure(currentRouter)) {
|
336
299
|
callback({
|
337
|
-
contract:
|
300
|
+
contract: currentRouter,
|
338
301
|
path: options.path
|
339
302
|
});
|
340
303
|
} else {
|
341
|
-
for (const key in
|
342
|
-
|
304
|
+
for (const key in currentRouter) {
|
305
|
+
traverseContractProcedures(
|
343
306
|
{
|
344
|
-
router:
|
307
|
+
router: currentRouter[key],
|
345
308
|
path: [...options.path, key]
|
346
309
|
},
|
347
310
|
callback,
|
348
|
-
|
311
|
+
lazyOptions
|
349
312
|
);
|
350
313
|
}
|
351
314
|
}
|
352
|
-
return
|
315
|
+
return lazyOptions;
|
353
316
|
}
|
354
|
-
async function
|
317
|
+
async function resolveContractProcedures(options, callback) {
|
355
318
|
const pending = [options];
|
356
|
-
for (const
|
357
|
-
const
|
358
|
-
for (const
|
359
|
-
const { default: router } = await unlazy(
|
319
|
+
for (const options2 of pending) {
|
320
|
+
const lazyOptions = traverseContractProcedures(options2, callback);
|
321
|
+
for (const options3 of lazyOptions) {
|
322
|
+
const { default: router } = await unlazy(options3.router);
|
360
323
|
pending.push({
|
361
|
-
|
362
|
-
|
324
|
+
router,
|
325
|
+
path: options3.path
|
363
326
|
});
|
364
327
|
}
|
365
328
|
}
|
366
329
|
}
|
367
|
-
function
|
368
|
-
|
330
|
+
async function unlazyRouter(router) {
|
331
|
+
if (isProcedure(router)) {
|
332
|
+
return router;
|
333
|
+
}
|
334
|
+
const unlazied = {};
|
335
|
+
for (const key in router) {
|
336
|
+
const item = router[key];
|
337
|
+
const { default: unlaziedRouter } = await unlazy(item);
|
338
|
+
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
339
|
+
}
|
340
|
+
return unlazied;
|
369
341
|
}
|
370
|
-
|
342
|
+
|
343
|
+
function createAssertedLazyProcedure(lazied) {
|
344
|
+
const lazyProcedure = lazy(async () => {
|
345
|
+
const { default: maybeProcedure } = await unlazy(lazied);
|
346
|
+
if (!isProcedure(maybeProcedure)) {
|
347
|
+
throw new Error(`
|
348
|
+
Expected a lazy<procedure> but got lazy<unknown>.
|
349
|
+
This should be caught by TypeScript compilation.
|
350
|
+
Please report this issue if this makes you feel uncomfortable.
|
351
|
+
`);
|
352
|
+
}
|
353
|
+
return { default: maybeProcedure };
|
354
|
+
}, getLazyMeta(lazied));
|
355
|
+
return lazyProcedure;
|
356
|
+
}
|
357
|
+
function createContractedProcedure(procedure, contract) {
|
371
358
|
return new Procedure({
|
372
359
|
...procedure["~orpc"],
|
373
360
|
errorMap: contract["~orpc"].errorMap,
|
@@ -375,5 +362,9 @@ function createContractedProcedure(contract, procedure) {
|
|
375
362
|
meta: contract["~orpc"].meta
|
376
363
|
});
|
377
364
|
}
|
365
|
+
function call(procedure, input, ...rest) {
|
366
|
+
const options = resolveMaybeOptionalOptions(rest);
|
367
|
+
return createProcedureClient(procedure, options)(input, options);
|
368
|
+
}
|
378
369
|
|
379
|
-
export {
|
370
|
+
export { LAZY_SYMBOL as L, Procedure as P, createContractedProcedure as a, addMiddleware as b, createProcedureClient as c, isLazy as d, enhanceRouter as e, createAssertedLazyProcedure as f, getRouter as g, createORPCErrorConstructorMap as h, isProcedure as i, getLazyMeta as j, middlewareOutputFn as k, lazy as l, mergeCurrentContext as m, isStartWithMiddlewares as n, mergeMiddlewares as o, call as p, getHiddenRouterContract as q, createAccessibleLazyRouter as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, resolveContractProcedures as w, unlazyRouter as x };
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import { HTTPPath, ORPCError } from '@orpc/client';
|
2
|
+
import { Meta } from '@orpc/contract';
|
3
|
+
import { Interceptor } from '@orpc/shared';
|
4
|
+
import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
|
5
|
+
import { C as Context, R as Router, b as AnyRouter, A as AnyProcedure, d as ProcedureClientInterceptorOptions } from './server.CYNGeoCm.mjs';
|
6
|
+
|
7
|
+
interface StandardHandlerPlugin<T extends Context> {
|
8
|
+
order?: number;
|
9
|
+
init?(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
10
|
+
}
|
11
|
+
declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
|
12
|
+
protected readonly plugins: TPlugin[];
|
13
|
+
constructor(plugins?: readonly TPlugin[]);
|
14
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
15
|
+
}
|
16
|
+
|
17
|
+
type StandardParams = Record<string, string>;
|
18
|
+
type StandardMatchResult = {
|
19
|
+
path: readonly string[];
|
20
|
+
procedure: AnyProcedure;
|
21
|
+
params?: StandardParams;
|
22
|
+
} | undefined;
|
23
|
+
interface StandardMatcher {
|
24
|
+
init(router: AnyRouter): void;
|
25
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
26
|
+
}
|
27
|
+
interface StandardCodec {
|
28
|
+
encode(output: unknown, procedure: AnyProcedure): StandardResponse;
|
29
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
30
|
+
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
|
31
|
+
}
|
32
|
+
|
33
|
+
interface StandardHandleOptions<T extends Context> {
|
34
|
+
prefix?: HTTPPath;
|
35
|
+
context: T;
|
36
|
+
}
|
37
|
+
type StandardHandleResult = {
|
38
|
+
matched: true;
|
39
|
+
response: StandardResponse;
|
40
|
+
} | {
|
41
|
+
matched: false;
|
42
|
+
response: undefined;
|
43
|
+
};
|
44
|
+
interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
|
45
|
+
request: StandardLazyRequest;
|
46
|
+
}
|
47
|
+
interface StandardHandlerOptions<TContext extends Context> {
|
48
|
+
plugins?: StandardHandlerPlugin<TContext>[];
|
49
|
+
/**
|
50
|
+
* Interceptors at the request level, helpful when you want catch errors
|
51
|
+
*/
|
52
|
+
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, Promise<StandardHandleResult>>[];
|
53
|
+
/**
|
54
|
+
* Interceptors at the root level, helpful when you want override the request/response
|
55
|
+
*/
|
56
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, Promise<StandardHandleResult>>[];
|
57
|
+
/**
|
58
|
+
*
|
59
|
+
* Interceptors for procedure client.
|
60
|
+
*/
|
61
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, Promise<unknown>>[];
|
62
|
+
}
|
63
|
+
declare class StandardHandler<T extends Context> {
|
64
|
+
private readonly matcher;
|
65
|
+
private readonly codec;
|
66
|
+
private readonly interceptors;
|
67
|
+
private readonly clientInterceptors;
|
68
|
+
private readonly rootInterceptors;
|
69
|
+
constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
|
70
|
+
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
71
|
+
}
|
72
|
+
|
73
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as f };
|
74
|
+
export type { StandardCodec as S, StandardParams as a, StandardHandleOptions as b, StandardHandleResult as c, StandardHandlerInterceptorOptions as d, StandardHandlerOptions as e, StandardHandlerPlugin as g, StandardMatchResult as h, StandardMatcher as i };
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import { ErrorMap, EnhanceRouteOptions, MergedErrorMap, AnyContractRouter, AnyContractProcedure } from '@orpc/contract';
|
2
|
+
import { a as AnyMiddleware, L as Lazyable, b as AnyRouter, C as Context, c as Lazy, A as AnyProcedure, P as Procedure, M as MergedInitialContext } from './server.CYNGeoCm.js';
|
3
|
+
|
4
|
+
declare function getRouter<T extends Lazyable<AnyRouter | undefined>>(router: T, path: readonly string[]): T extends Lazy<any> ? Lazy<AnyRouter | undefined> : Lazyable<AnyRouter | undefined>;
|
5
|
+
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> & {
|
6
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? AccessibleLazyRouter<T[K]> : never;
|
7
|
+
};
|
8
|
+
declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
|
9
|
+
type EnhancedRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context, TErrorMap extends ErrorMap> = T extends Lazy<infer U extends AnyRouter> ? AccessibleLazyRouter<EnhancedRouter<U, TInitialContext, TCurrentContext, TErrorMap>> : T extends Procedure<infer UInitialContext, infer UCurrentContext, infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<MergedInitialContext<TInitialContext, UInitialContext, TCurrentContext>, UCurrentContext, UInputSchema, UOutputSchema, MergedErrorMap<TErrorMap, UErrorMap>, UMeta> : {
|
10
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? EnhancedRouter<T[K], TInitialContext, TCurrentContext, TErrorMap> : never;
|
11
|
+
};
|
12
|
+
interface EnhanceRouterOptions<TErrorMap extends ErrorMap> extends EnhanceRouteOptions {
|
13
|
+
middlewares: readonly AnyMiddleware[];
|
14
|
+
errorMap: TErrorMap;
|
15
|
+
dedupeLeadingMiddlewares: boolean;
|
16
|
+
}
|
17
|
+
declare function enhanceRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context, TErrorMap extends ErrorMap>(router: T, options: EnhanceRouterOptions<TErrorMap>): EnhancedRouter<T, TInitialContext, TCurrentContext, TErrorMap>;
|
18
|
+
interface TraverseContractProceduresOptions {
|
19
|
+
router: AnyContractRouter | AnyRouter;
|
20
|
+
path: readonly string[];
|
21
|
+
}
|
22
|
+
interface TraverseContractProcedureCallbackOptions {
|
23
|
+
contract: AnyContractProcedure | AnyProcedure;
|
24
|
+
path: readonly string[];
|
25
|
+
}
|
26
|
+
/**
|
27
|
+
* @deprecated Use `TraverseContractProcedureCallbackOptions` instead.
|
28
|
+
*/
|
29
|
+
type ContractProcedureCallbackOptions = TraverseContractProcedureCallbackOptions;
|
30
|
+
interface LazyTraverseContractProceduresOptions {
|
31
|
+
router: Lazy<AnyRouter>;
|
32
|
+
path: readonly string[];
|
33
|
+
}
|
34
|
+
declare function traverseContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void, lazyOptions?: LazyTraverseContractProceduresOptions[]): LazyTraverseContractProceduresOptions[];
|
35
|
+
declare function resolveContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void): Promise<void>;
|
36
|
+
type UnlaziedRouter<T extends AnyRouter> = T extends AnyProcedure ? T : {
|
37
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? UnlaziedRouter<U> : never;
|
38
|
+
};
|
39
|
+
declare function unlazyRouter<T extends AnyRouter>(router: T): Promise<UnlaziedRouter<T>>;
|
40
|
+
|
41
|
+
export { createAccessibleLazyRouter as c, enhanceRouter as e, getRouter as g, resolveContractProcedures as r, traverseContractProcedures as t, unlazyRouter as u };
|
42
|
+
export type { AccessibleLazyRouter as A, ContractProcedureCallbackOptions as C, EnhanceRouterOptions as E, LazyTraverseContractProceduresOptions as L, TraverseContractProcedureCallbackOptions as T, UnlaziedRouter as U, EnhancedRouter as a, TraverseContractProceduresOptions as b };
|