Skip to content

Commit 6c5bfe4

Browse files
authored
feat(server): unlazyRouter utility (#219)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced an enhanced router processing capability that converts deferred configurations into fully accessible structures for streamlined routing. - **Tests** - Expanded test coverage to ensure functional correctness and type consistency of the updated routing mechanism. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 1d3318c commit 6c5bfe4

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

packages/server/src/router-utils.test-d.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { MergedErrorMap, Meta, Schema } from '@orpc/contract'
22
import type { baseErrorMap, BaseMeta, inputSchema, outputSchema } from '../../contract/tests/shared'
3-
import type { CurrentContext, InitialContext, ping, pong, router } from '../tests/shared'
3+
import type { CurrentContext, InitialContext, router } from '../tests/shared'
44
import type { Context } from './context'
55
import type { Lazy } from './lazy'
66
import type { Procedure } from './procedure'
7-
import type { AccessibleLazyRouter, EnhancedRouter } from './router-utils'
7+
import type { AccessibleLazyRouter, EnhancedRouter, UnlaziedRouter } from './router-utils'
8+
import { ping, pong } from '../tests/shared'
89

910
it('AccessibleLazyRouter', () => {
1011
type Accessible = AccessibleLazyRouter<Lazy<typeof router>>
@@ -70,3 +71,16 @@ it('EnhancedRouter', () => {
7071
>
7172
>()
7273
})
74+
75+
it('UnlaziedRouter', () => {
76+
type Unlazied = UnlaziedRouter<typeof router>
77+
78+
expectTypeOf<Unlazied>().toEqualTypeOf({
79+
ping,
80+
pong,
81+
nested: {
82+
ping,
83+
pong,
84+
},
85+
})
86+
})

packages/server/src/router-utils.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { enhanceRoute } from '@orpc/contract'
22
import { contract, ping, pingMiddleware, pong, router } from '../tests/shared'
33
import { getLazyMeta, isLazy, unlazy } from './lazy'
44
import { setHiddenRouterContract } from './router-hidden'
5-
import { enhanceRouter, getRouter, resolveContractProcedures, traverseContractProcedures } from './router-utils'
5+
import { enhanceRouter, getRouter, resolveContractProcedures, traverseContractProcedures, unlazyRouter } from './router-utils'
66

77
it('getRouter', () => {
88
expect(getRouter(router, [])).toEqual(router)
@@ -188,3 +188,16 @@ it('resolveContractProcedures', async () => {
188188
path: ['nested', 'pong'],
189189
})
190190
})
191+
192+
it('unlazyRouter', async () => {
193+
const unlazied = await unlazyRouter(router)
194+
195+
expect(unlazied).toEqual({
196+
ping,
197+
pong,
198+
nested: {
199+
ping,
200+
pong,
201+
},
202+
})
203+
})

packages/server/src/router-utils.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,28 @@ export async function resolveContractProcedures(
223223
}
224224
}
225225
}
226+
227+
export type UnlaziedRouter<T extends AnyRouter> =
228+
T extends AnyProcedure
229+
? T
230+
: {
231+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? UnlaziedRouter<U> : never
232+
}
233+
234+
export async function unlazyRouter<T extends AnyRouter>(router: T): Promise<UnlaziedRouter<T>> {
235+
if (isProcedure(router)) {
236+
return router as any
237+
}
238+
239+
const unlazied = {} as Record<string, any>
240+
241+
for (const key in router) {
242+
const item: Lazyable<AnyRouter> = router[key]!
243+
244+
const { default: unlaziedRouter } = await unlazy(item)
245+
246+
unlazied[key] = await unlazyRouter(unlaziedRouter)
247+
}
248+
249+
return unlazied as any
250+
}

0 commit comments

Comments
 (0)