Skip to content
Myr

refactor(router): Make Router an interface, not a class (#268)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Allow initialization with router objects instead of routerClass. * Make Router an interface, not a class. * Make Result an interface, not a class. * Apply getSortedHandlers for result.

authored by Taku Amano · committed by GitHub · parent ef65308 · Browse files · 4 files+15 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

4 files+15 added−20 removed

  • other:+15 added−20 removed

4 files+15 added−20 removed

modified:src/router/reg-exp-router/router.ts+6 added-5 removed
1import { Router, Result, METHOD_NAME_ALL } from '../../router'1import type { Router, Result } from '../../router'2import { METHOD_NAME_ALL } from '../../router'3import type { ParamMap } from './trie'4import { Trie } from './trie'56interface Hint {
193 unmodified lines
200201 return true202}203203export class RegExpRouter<T> extends Router<T> {204export class RegExpRouter<T> implements Router<T> {205 routeData?: {206 index: number207 routes: Route<T>[]208 methods: Set<string>
80 unmodified lines
289 )290 match = path.match(new RegExp(regExpSrc))291 }292292 return new Result(getSortedHandlers(handlers.values()), params)293 return { handlers: getSortedHandlers(handlers.values()), params }294 }295 : (method, path) => {296 let matcher = (primaryMatchers[method] || primaryMatchers[METHOD_NAME_ALL]) as Matcher<T>297 let match = path.match(matcher[0])
12 unmodified lines
310311 const index = match.indexOf('', 1)312 const [handlers, paramMap] = matcher[1][index]313 if (!paramMap) {313 return new Result(handlers, emptyParam)314 return { handlers, params: emptyParam }315 }316317 const params: Record<string, string> = {}318 for (let i = 0, len = paramMap.length; i < len; i++) {319 params[paramMap[i][0]] = match[paramMap[i][1]]320 }321321 return new Result(handlers, params)322 return { handlers, params }323 }324325 return this.match(method, path)326 }
136 unmodified lines
modified:src/router/trie-router/router.ts+2 added-4 removed
modified:src/router.ts+4 added-8 removed
modified:README.md+3 added-3 removed
516 unmodified lines
517 expect(res.status).toBe(200)518})519```520521## routerClass521## router522523The `routerClass` option specify which router is used inside. The default router is `TrieRouter`. If you want to use `RexExpRouter`, write like this:523The `router` option specify which router is used inside. The default router is `TrieRouter`. If you want to use `RexExpRouter`, write like this:524525```ts526import { RegExpRouter } from 'hono/router/reg-exp-router'527528const app = new Hono({ routerClass: RegExpRouter })528const app = new Hono({ router: new RegExpRouter() })529```530531## Cloudflare Workers with Hono532
155 unmodified lines