@riogz/router
Version:
A simple, lightweight, powerful, view-agnostic, modular and extensible router
50 lines (49 loc) • 1.79 kB
TypeScript
import { RouteNode } from '../lib/route-node';
import { Router, Route } from '../types/router';
import { Params } from '../types/base';
/**
* Enhances a router with route management capabilities.
*
* This module provides functionality for:
* - Route definition and management
* - Route tree construction and navigation
* - Path building and matching
* - Route forwarding and redirection
* - Active route checking
* - Parameter encoding/decoding
* - Default parameter handling
*
* Routes are organized in a hierarchical tree structure where:
* - Parent routes can have child routes (nested routing)
* - Route names use dot notation (e.g., 'app.users.profile')
* - Parameters can be passed between routes
* - Guards and lifecycle hooks can be attached to routes
*
* @template Dependencies - Type of dependencies available to route handlers
* @param routes - Array of route definitions or RouteNode instance
* @returns Function that enhances a router with route management
*
* @example
* ```typescript
* const routes = [
* { name: 'home', path: '/' },
* { name: 'users', path: '/users', children: [
* { name: 'list', path: '' },
* { name: 'detail', path: '/:id' }
* ]},
* { name: 'about', path: '/about' }
* ]
*
* const router = createRouter(routes)
*
* // Navigate to routes
* router.navigate('users.detail', { id: '123' })
*
* // Check if route is active
* if (router.isActive('users')) {
* console.log('Users section is active')
* }
* ```
*/
export default function withRoutes<Dependencies>(routes: Array<Route<Dependencies>> | RouteNode): (router: Router<Dependencies>) => Router<Dependencies>;
export declare function findFirstAccessibleChildAtPath(router: Router, routeName: string, params?: Params): Promise<string | null>;