@@ -10,6 +10,12 @@ import { mergeMiddlewares } from './middleware-utils'
1010import { isProcedure , Procedure } from './procedure'
1111import { getHiddenRouterContract } from './router-hidden'
1212
13+ /**
14+ * Navigates a nested router structure using a path array, resolving each segment
15+ * and handling lazy-loaded routers by wrapping remaining segments in a new lazy loader.
16+ *
17+ * Returns `undefined` if the path leads through a procedure or a falsy value.
18+ */
1319export function getRouter < T extends Lazyable < AnyRouter | undefined > > (
1420 router : T ,
1521 path : readonly string [ ] ,
@@ -57,6 +63,10 @@ export type AccessibleLazyRouter<T extends Lazyable<AnyRouter | undefined>>
5763 [ K in keyof T ] : T [ K ] extends Lazyable < AnyRouter > ? AccessibleLazyRouter < T [ K ] > : never
5864 }
5965
66+ /**
67+ * Creates a proxy around a lazy router that allows property access to transparently
68+ * return nested lazy routers, enabling dot-notation traversal without eagerly loading.
69+ */
6070export function createAccessibleLazyRouter < T extends Lazy < AnyRouter | undefined > > ( lazied : T ) : AccessibleLazyRouter < T > {
6171 const recursive = new Proxy ( lazied , {
6272 get ( target , key ) {
@@ -107,6 +117,12 @@ export interface EnhanceRouterOptions<TErrorMap extends ErrorMap> extends Enhanc
107117 dedupeLeadingMiddlewares : boolean
108118}
109119
120+ /**
121+ * Recursively enhances a router by applying middlewares, error maps, and route options
122+ * to every procedure in the tree. Lazy routers are wrapped so enhancement is deferred
123+ * until loading. Non-object primitive values are passed through unchanged to guard
124+ * against infinite recursion when router modules export non-router members.
125+ */
110126export function enhanceRouter <
111127 T extends Lazyable < AnyRouter > ,
112128 TInitialContext extends Context ,
@@ -183,6 +199,11 @@ export interface LazyTraverseContractProceduresOptions {
183199 path : readonly string [ ]
184200}
185201
202+ /**
203+ * Synchronously walks a router tree, invoking {@link callback} for each contract procedure
204+ * found and collecting lazy routers for deferred resolution. Non-object primitive values
205+ * in the tree are safely skipped.
206+ */
186207export function traverseContractProcedures (
187208 options : TraverseContractProceduresOptions ,
188209 callback : ( options : TraverseContractProcedureCallbackOptions ) => void ,
@@ -226,6 +247,11 @@ export function traverseContractProcedures(
226247 return lazyOptions
227248}
228249
250+ /**
251+ * Asynchronously resolves all procedures in a router tree, including those behind
252+ * lazy loaders. Iteratively unlazies deferred routers and invokes {@link callback}
253+ * for every contract procedure discovered.
254+ */
229255export async function resolveContractProcedures (
230256 options : TraverseContractProceduresOptions ,
231257 callback : ( options : TraverseContractProcedureCallbackOptions ) => void ,
@@ -253,6 +279,11 @@ export type UnlaziedRouter<T extends AnyRouter>
253279 [ K in keyof T ] : T [ K ] extends Lazyable < infer U extends AnyRouter > ? UnlaziedRouter < U > : never
254280 }
255281
282+ /**
283+ * Recursively resolves all lazy-loaded components in a router tree, returning a
284+ * fully eagerly-loaded copy. Procedures are returned as-is, and non-object primitive
285+ * values are passed through to avoid infinite recursion on non-router exports.
286+ */
256287export async function unlazyRouter < T extends AnyRouter > ( router : T ) : Promise < UnlaziedRouter < T > > {
257288 if ( isProcedure ( router ) ) {
258289 return router as any
0 commit comments