-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathrouter-utils.ts
More file actions
276 lines (224 loc) · 7.84 KB
/
router-utils.ts
File metadata and controls
276 lines (224 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import type { AnyContractProcedure, AnyContractRouter, EnhanceRouteOptions, ErrorMap, MergedErrorMap } from '@orpc/contract'
import type { Context, MergedInitialContext } from './context'
import type { Lazy, Lazyable } from './lazy'
import type { AnyMiddleware } from './middleware'
import type { AnyProcedure } from './procedure'
import type { AnyRouter } from './router'
import { enhanceRoute, isContractProcedure, mergeErrorMap, mergePrefix } from '@orpc/contract'
import { getLazyMeta, isLazy, lazy, unlazy } from './lazy'
import { mergeMiddlewares } from './middleware-utils'
import { isProcedure, Procedure } from './procedure'
import { getHiddenRouterContract } from './router-hidden'
export function getRouter<T extends Lazyable<AnyRouter | undefined>>(
router: T,
path: readonly string[],
): T extends Lazy<any> ? Lazy<AnyRouter | undefined> : Lazyable<AnyRouter | undefined> {
let current: Lazyable<AnyRouter | undefined> = router
for (let i = 0; i < path.length; i++) {
const segment = path[i]!
if (!current) {
return undefined as any
}
if (isProcedure(current)) {
return undefined as any
}
if (!isLazy(current)) {
current = current[segment]
continue
}
const lazied = current
const rest = path.slice(i)
return lazy(async () => {
const unwrapped = await unlazy(lazied)
const next = getRouter(unwrapped.default, rest)
return unlazy(next)
}, getLazyMeta(lazied))
}
return current as any
}
export 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> & {
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? AccessibleLazyRouter<T[K]> : never
}
export function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T> {
const recursive = new Proxy(lazied, {
get(target, key) {
if (typeof key !== 'string') {
return Reflect.get(target, key)
}
const next = getRouter(lazied, [key])
return createAccessibleLazyRouter(next)
},
})
return recursive as any
}
export 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
>
: {
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? EnhancedRouter<T[K], TInitialContext, TCurrentContext, TErrorMap> : never
}
export interface EnhanceRouterOptions<TErrorMap extends ErrorMap> extends EnhanceRouteOptions {
middlewares: readonly AnyMiddleware[]
errorMap: TErrorMap
dedupeLeadingMiddlewares: boolean
}
export 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> {
if (isLazy(router)) {
const laziedMeta = getLazyMeta(router)
const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix
const enhanced = lazy(async () => {
const { default: unlaziedRouter } = await unlazy(router)
const enhanced = enhanceRouter(unlaziedRouter, options)
return unlazy(enhanced)
}, {
...laziedMeta,
prefix: enhancedPrefix,
})
const accessible = createAccessibleLazyRouter(enhanced)
return accessible as any
}
if (isProcedure(router)) {
const newMiddlewares = mergeMiddlewares(options.middlewares, router['~orpc'].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares })
const newMiddlewareAdded = newMiddlewares.length - router['~orpc'].middlewares.length
const enhanced = new Procedure({
...router['~orpc'],
route: enhanceRoute(router['~orpc'].route, options),
errorMap: mergeErrorMap(options.errorMap, router['~orpc'].errorMap),
middlewares: newMiddlewares,
inputValidationIndex: router['~orpc'].inputValidationIndex + newMiddlewareAdded,
outputValidationIndex: router['~orpc'].outputValidationIndex + newMiddlewareAdded,
})
return enhanced as any
}
if (typeof router !== 'object' || router === null) {
return router as any
}
const enhanced = {} as Record<string, any>
for (const key in router) {
enhanced[key] = enhanceRouter(router[key]!, options)
}
return enhanced as any
}
export interface TraverseContractProceduresOptions {
router: AnyContractRouter | AnyRouter
path: readonly string[]
}
export interface TraverseContractProcedureCallbackOptions {
contract: AnyContractProcedure | AnyProcedure
path: readonly string[]
}
/**
* @deprecated Use `TraverseContractProcedureCallbackOptions` instead.
*/
export type ContractProcedureCallbackOptions = TraverseContractProcedureCallbackOptions
export interface LazyTraverseContractProceduresOptions {
router: Lazy<AnyRouter>
path: readonly string[]
}
export function traverseContractProcedures(
options: TraverseContractProceduresOptions,
callback: (options: TraverseContractProcedureCallbackOptions) => void,
lazyOptions: LazyTraverseContractProceduresOptions[] = [],
): LazyTraverseContractProceduresOptions[] {
let currentRouter: AnyContractRouter | Lazyable<AnyRouter> = options.router
const hiddenContract = getHiddenRouterContract(options.router)
if (hiddenContract !== undefined) {
currentRouter = hiddenContract
}
if (isLazy(currentRouter)) {
lazyOptions.push({
router: currentRouter,
path: options.path,
})
}
else if (isContractProcedure(currentRouter)) {
callback({
contract: currentRouter,
path: options.path,
})
}
else if (typeof currentRouter === 'object' && currentRouter !== null) {
for (const key in currentRouter) {
traverseContractProcedures(
{
router: (currentRouter as any)[key],
path: [...options.path, key],
},
callback,
lazyOptions,
)
}
}
return lazyOptions
}
export async function resolveContractProcedures(
options: TraverseContractProceduresOptions,
callback: (options: TraverseContractProcedureCallbackOptions) => void,
) {
const pending: TraverseContractProceduresOptions[] = [options]
for (const options of pending) {
const lazyOptions = traverseContractProcedures(options, callback)
for (const options of lazyOptions) {
const { default: router } = await unlazy(options.router)
pending.push({
router,
path: options.path,
})
}
}
}
export type UnlaziedRouter<T extends AnyRouter>
= T extends AnyProcedure
? T
: {
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? UnlaziedRouter<U> : never
}
export async function unlazyRouter<T extends AnyRouter>(router: T): Promise<UnlaziedRouter<T>> {
if (isProcedure(router)) {
return router as any
}
if (typeof router !== 'object' || router === null) {
return router as any
}
const unlazied = {} as Record<string, any>
for (const key in router) {
const item: Lazyable<AnyRouter> = router[key]!
const { default: unlaziedRouter } = await unlazy(item)
unlazied[key] = await unlazyRouter(unlaziedRouter)
}
return unlazied as any
}