/** * Universal Router (https://www.kriasoft.com/universal-router/) * * Copyright (c) 2015-present Kriasoft. * * This source code is licensed under the MIT license found in the * LICENSE.txt file in the root directory of this source tree. */ import { Path, MatchFunction, ParseOptions, TokensToRegexpOptions, RegexpToFunctionOptions } from 'path-to-regexp'; /** * Params is a key/value object that represents extracted URL parameters. */ export interface RouteParams { [paramName: string]: string | string[]; } /** * In addition to a URL path string, any arbitrary data can be passed to * the `router.resolve()` method, that becomes available inside action functions. */ export interface RouterContext { [propName: string]: any; } export interface ResolveContext extends RouterContext { /** * URL which was transmitted to `router.resolve()`. */ pathname: string; } export interface RouteContext extends ResolveContext { /** * Current router instance. */ router: UniversalRouterSync; /** * Matched route object. */ route: Route; /** * Base URL path relative to the path of the current route. */ baseUrl: string; /** * Matched path. */ path: string; /** * Matched path params. */ params: RouteParams; /** * Middleware style function which can continue resolving. */ next: (resume?: boolean) => R; } export declare type RouteResultSync = T | null | undefined; /** * A Route is a singular route in your application. It contains a path, an * action function, and optional children which are an array of Route. * @template C User context that is made union with RouterContext. * @template R Result that every action function resolves to. */ export interface Route { /** * A string, array of strings, or a regular expression. Defaults to an empty string. */ path?: Path; /** * A unique string that can be used to generate the route URL. */ name?: string; /** * The link to the parent route is automatically populated by the router. Useful for breadcrumbs. */ parent?: Route | null; /** * An array of Route objects. Nested routes are perfect to be used in middleware routes. */ children?: Routes | null; /** * Action method should return anything except `null` or `undefined` to be resolved by router * otherwise router will throw `Page not found` error if all matched routes returned nothing. */ action?: (context: RouteContext, params: RouteParams) => RouteResultSync; /** * The route path match function. Used for internal caching. */ match?: MatchFunction; } /** * Routes is an array of type Route. * @template C User context that is made union with RouterContext. * @template R Result that every action function resolves to. */ export declare type Routes = Array>; export declare type ResolveRoute = (context: RouteContext, params: RouteParams) => RouteResultSync; export declare type RouteError = Error & { status?: number; }; export declare type ErrorHandler = (error: RouteError, context: ResolveContext) => RouteResultSync; export interface RouterOptions extends ParseOptions, TokensToRegexpOptions, RegexpToFunctionOptions { context?: C; baseUrl?: string; resolveRoute?: ResolveRoute; errorHandler?: ErrorHandler; } export interface RouteMatch { route: Route; baseUrl: string; path: string; params: RouteParams; } declare class UniversalRouterSync { root: Route; baseUrl: string; options: RouterOptions; constructor(routes: Routes | Route, options?: RouterOptions); /** * Traverses the list of routes in the order they are defined until it finds * the first route that matches provided URL path string and whose action function * returns anything other than `null` or `undefined`. */ resolve(pathnameOrContext: string | ResolveContext): RouteResultSync; } export default UniversalRouterSync;