Kind: Class
Source: src/router/trie-router/router.ts
Part of: Router
TrieRouter stores route patterns and their handlers in a trie-based route tree. Use add() to register a method and path, then call match() to resolve an incoming method and path to a Result<T>.
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 |
#node | Node<T> |
Diagram
mermaidgraph LR Route[Route registration] --> Add[add()] Add --> Trie[Trie route tree] Request[Incoming method and path] --> Match[match()] Match --> Trie Trie --> Result[Result<T>]
Usage
tsconst router = new TrieRouter<string>()
router.add('GET', '/articles/:slug', 'getArticle')
router.add('POST', '/articles', 'createArticle')
const result = router.match('GET', '/articles/intro')
// Pass result to the request dispatcher.
console.log(result)
AI Coding Instructions
- Keep the handler type consistent for every route registered in the same
TrieRouterinstance. - Register routes through
add()before matching requests throughmatch(). - Pass the HTTP method and request path to
match()using the same format used during registration. - Handle the returned
Result<T>in the caller that dispatches matched handlers and processes route parameters.
How it works
-
TrieRouter<T>is an exported generic router class implementingRouter<T>. Its publicnamefield is"TrieRouter", and each instance initializes one privateNode<T>as its route storage and matcher.src/router/trie-router/router.ts:5-11 -
add(method, path, handler)registershandlerfor the supplied method and path by inserting it into that private node structure. This mutates the router’s stored routes and has no returned value.src/router/trie-router/router.ts:13-23The underlying insertion splits routing paths into segments, creates child nodes as needed, and appends a method/handler entry at the terminal node.src/router/trie-router/node.ts:44-84 -
Before insertion,
addchecks for an optional named parameter. Whenpathends in?and contains:, the helper returns expanded path variants;TrieRouterinserts the same handler for every variant.src/router/trie-router/router.ts:13-20For example,/api/animals/:type?expands to/api/animalsand/api/animals/:type; a root optional parameter expands to/and its parameterized form.src/utils/url.ts:171-205src/utils/url.test.ts:235-255 -
match(method, path)delegates directly to the node search and returnsResult<T>, whose concrete form here is a one-element tuple containing an array of[handler, params]pairs.src/router/trie-router/router.ts:25-27src/router/trie-router/node.ts:114-115src/router/trie-router/node.ts:237-244A non-match returns that same shape with an empty handler array.src/router/trie-router/node.test.ts:14-20 -
Paths support literal segments, named segments such as
:id, wildcard segments (*), and named segments constrained by a regular expression such as:id{[0-9]+}. Pattern recognition is performed while adding routes.src/utils/url.ts:50-77Named and constrained captures are returned in the parameter record.src/router/trie-router/node.ts:165-229src/router/trie-router/node.test.ts:74-108src/router/trie-router/node.test.ts:216-234 -
A registered method is matched exactly; if no exact method entry exists at a matched route node, an entry registered as
ALLis selected.src/router/trie-router/node.ts:94-110src/router.ts:6-13Wildcards can match the remainder of a path and also match when no further segment follows, such as/hello/*matching/hello.src/router/trie-router/node.ts:136-146src/router/trie-router/node.ts:153-163 -
Matching can return more than one handler when multiple registered routes match. Returned matches are sorted by their route insertion score, which is incremented for each insertion.
src/router/trie-router/node.ts:44-46src/router/trie-router/node.ts:237-244For example, a literal route and a wildcard route can both be returned.src/router/trie-router/node.test.ts:125-134 -
TrieRouteritself contains no explicit argument validation or explicit error handling.src/router/trie-router/router.ts:9-27Route patterns with{...}are passed tonew RegExp(...)during insertion, without a catch in the shown code.src/utils/url.ts:60-74
Relationships
- IMPORTS →
checkOptionalParameter - IMPORTS →
Node
Was this page helpful?