router.ts
This file
Path
src/router.ts
103 lines · 2.7 KiB · TypeScript
Last changed by
Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–16why, one step back1/**2 * @module3 * This module provides types definitions and variables for the routers.4 */56/**7 * Constant representing all HTTP methods in uppercase.8 */9export const METHOD_NAME_ALL = 'ALL' as const10/**11 * Constant representing all HTTP methods in lowercase.12 */13export const METHOD_NAME_ALL_LOWERCASE = 'all' as const14/**15 * Array of supported HTTP methods.16 */Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
line 17why, one step back17export const METHODS = ['get', 'post', 'put', 'delete', 'options', 'patch', 'query'] as constMerge commit from forkYusuke Wadabefore adoptionf0b094d
lines 18–103why, one step back18/**19 * Error message indicating that a route cannot be added because the matcher is already built.20 */21export const MESSAGE_MATCHER_IS_ALREADY_BUILT =22 'Can not add a route since the matcher is already built.'2324/**25 * Interface representing a router.26 *27 * @template T - The type of the handler.28 */29export interface Router<T> {30 /**31 * The name of the router.32 */33 name: string3435 /**36 * Adds a route to the router.37 *38 * @param method - The HTTP method (e.g., 'get', 'post').39 * @param path - The path for the route.40 * @param handler - The handler for the route.41 */42 add(method: string, path: string, handler: T): void4344 /**45 * Matches a route based on the given method and path.46 *47 * @param method - The HTTP method (e.g., 'get', 'post').48 * @param path - The path to match.49 * @returns The result of the match.50 */51 match(method: string, path: string): Result<T>52}5354/**55 * Type representing a map of parameter indices.56 */57export type ParamIndexMap = Record<string, number>58/**59 * Type representing a stash of parameters.60 */61export type ParamStash = string[]62/**63 * Type representing a map of parameters.64 */65export type Params = Record<string, string>66/**67 * Type representing the result of a route match.68 *69 * The result can be in one of two formats:70 * 1. An array of handlers with their corresponding parameter index maps, followed by a parameter stash.71 * 2. An array of handlers with their corresponding parameter maps.72 *73 * Example:74 *75 * [[handler, paramIndexMap][], paramArray]76 * ```typescript77 * [78 * [79 * [middlewareA, {}], // '*'80 * [funcA, {'id': 0}], // '/user/:id/*'81 * [funcB, {'id': 0, 'action': 1}], // '/user/:id/:action'82 * ],83 * ['123', 'abc']84 * ]85 * ```86 *87 * [[handler, params][]]88 * ```typescript89 * [90 * [91 * [middlewareA, {}], // '*'92 * [funcA, {'id': '123'}], // '/user/:id/*'93 * [funcB, {'id': '123', 'action': 'abc'}], // '/user/:id/:action'94 * ]95 * ]96 * ```97 */98export type Result<T> = [[T, ParamIndexMap][], ParamStash] | [[T, Params][]]99100/**101 * Error class representing an unsupported path error.102 */103export class UnsupportedPathError extends Error {}