@riogz/router
Version:
A simple, lightweight, powerful, view-agnostic, modular and extensible router
43 lines (42 loc) • 1.37 kB
TypeScript
import { Router } from '../types/router';
/**
* Enhances a router with dependency injection capabilities.
*
* This module provides functionality to:
* - Set individual dependencies by name
* - Set multiple dependencies at once
* - Retrieve all dependencies
* - Get injectable parameters for factory functions
* - Execute factory functions with proper dependency injection
*
* Dependencies are used throughout the router for:
* - Route guards (canActivate/canDeactivate)
* - Middleware functions
* - Plugin factories
* - Route lifecycle hooks
*
* @template Dependencies - Type of dependencies object
* @param dependencies - Initial dependencies to inject into the router
* @returns Function that enhances a router with dependency injection
*
* @example
* ```typescript
* const dependencies = {
* api: new ApiService(),
* auth: new AuthService(),
* logger: new Logger()
* }
*
* const router = createRouter(routes, options, dependencies)
*
* // Dependencies are available in route guards
* const canActivate = (router, deps) => (toState, fromState, done) => {
* if (deps.auth.isAuthenticated()) {
* done()
* } else {
* done(new Error('Not authenticated'))
* }
* }
* ```
*/
export default function withDependencies<Dependencies>(dependencies: Dependencies): (router: Router<Dependencies>) => Router<Dependencies>;