Kind: Class
Source: src/router/pattern-router/router.ts
Part of: Router
PatternRouter stores handlers or values under path patterns and resolves an incoming path against those patterns. Call add() to register a pattern, then call match() to receive a Result<T> for the matching route.
Implements: Router
Methods
| Method | Signature | Returns |
|---|---|---|
add | add(method: string, path: string, handler: T) | void |
match | match(method: string, path: string) | Result<T> |
Properties
| Property | Type |
|---|---|
name | string |
#routes | Route<T>[] |
When something fails
PatternRouterhandles failure in 1 place: it lets it reach the caller in all 1.
Diagram
mermaidgraph LR A[Pattern and value] --> B[PatternRouter.add] C[Incoming path] --> D[PatternRouter.match] B --> E[Registered patterns] E --> D D --> F[Result<T>]
Usage
tsimport { PatternRouter } from './router';
const router = new PatternRouter<string>();
router.add('/users/:id', 'user-detail');
router.add('/posts/:slug', 'post-detail');
const result = router.match('/users/42');
console.log(result);
AI Coding Instructions
- Register patterns through
add()before attempting to resolve paths withmatch(). - Keep the value type consistent with the router generic, such as
PatternRouter<string>orPatternRouter<RouteHandler>. - Treat the return value from
match()as aResult<T>and handle both matching and non-matching outcomes. - Keep pattern syntax consistent across registrations so route parameters are parsed predictably.
Relationships
- IMPORTS →
METHOD_NAME_ALL - IMPORTS →
UnsupportedPathError
Was this page helpful?