build-route-tree is a JavaScript/TypeScript library designed to create strongly typed route trees, significantly reducing errors associated with magic strings in URL paths. It provides a structured, object-oriented approach to defining application routes, enabling IDE autocompletion and compile-time validation for paths and parameters. Currently at version 1.0.3, the library appears to follow a release cadence driven by feature additions and bug fixes, typical for a new utility. Its key differentiator is the focus on full type inference for route paths and parameters, offering methods like `getRoutePath`, `getRedirectPath`, and `getElementKey` on each route node to retrieve path components and construct dynamic URLs safely. This helps encapsulate route logic and improve maintainability compared to manual string interpolation or less type-safe routing solutions.
npm install build-route-treeVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to define a typed route tree using `buildRouteTree` and `getParam` for dynamic segments, then accessing its generated paths and constructing dynamic URLs safely.
Always call `getRoutePath()` or `getRedirectPath(params)` on the route node to obtain the correct string path.
Ensure `getParam()` is assigned as the value to any route property that should represent a dynamic segment (e.g., `uuid: getParam<UserParams>()`).
Pass an object to `getRedirectPath` containing all necessary dynamic keys and their corresponding values, matching the `getParam` definitions higher up the route chain.
Call the appropriate path generation function, such as `.getRoutePath()` for static paths or `.getRedirectPath({ param: value })` for dynamic paths, on the route node.For dynamic routes defined with `getParam`, `getRedirectPath` is typically called on a *parent* node, or if `getParam` is at the end, it's the function that takes the params. Re-evaluate where dynamic parameters are expected and call `getRedirectPath` at the correct level, passing all required parameters for that path segment.
The `uuid` property, when defined with `getParam`, becomes the entry point for dynamically constructing the path. You should call `getRedirectPath` on the parent (`routes.home.user`) or a descendant (`routes.home.user.uuid.security['2fa']`) and provide the `uuid` value in the parameters object.
No dependency data recorded yet.