Skip to content

LinearRouter

reference
1 min readUpdated

Kind: Class

Source: src/router/linear-router/router.ts

Part of: Router

LinearRouter registers route entries through add() and resolves matching entries through match(). It stores handlers of type T and returns a Result<T> for each match attempt.

Implements: Router

Methods

MethodSignatureReturns
addadd(method: string, path: string, handler: T)void
matchmatch(method: string, path: string)Result<T>

Properties

PropertyType
namestring
#routes[string, string, T][]

Where it refuses work

  • LinearRouter stops the work with UnsupportedPathError when hasLabel && hasStar.

Diagram

mermaid
graph LR
  RouteDefinition --> add["LinearRouter.add()"]
  add --> RouteTable
  Request --> match["LinearRouter.match()"]
  RouteTable --> match
  match --> Result["Result<T>"]

Usage

ts
import { LinearRouter } from "./src/router/linear-router/router";

type Handler = () => string;

const router = new LinearRouter<Handler>();

router.add("GET", "/users/:id", () => "User route matched");
router.add("GET", "/health", () => "Service is available");

const result = router.match("GET", "/users/ada");

console.log(result);

AI Coding Instructions

  • Keep the handler type T consistent for every route registered on the same router instance.
  • Register routes with add() before attempting to resolve them with match().
  • Treat the value returned by match() as a Result<T>; handle both matching and non-matching outcomes according to its defined shape.
  • Preserve route registration order when changing route setup, since a linear router evaluates stored route entries during matching.

Relationships

  • IMPORTS → METHOD_NAME_ALL
  • IMPORTS → UnsupportedPathError
  • IMPORTS → checkOptionalParameter

Was this page helpful?

Download as PDF
LinearRouter — Hono (narrator proof)