0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 11:51:01 +01:00
hono/deno_dist/router/pattern-router/router.ts
Taku Amano ec94acd909
feat: one params per a handler (optimized for RegExpRouter) (#1566)
Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
2023-10-16 08:39:37 +09:00

55 lines
1.6 KiB
TypeScript

import type { Result, Router, Params } from '../../router.ts'
import { METHOD_NAME_ALL, UnsupportedPathError } from '../../router.ts'
type Route<T> = [RegExp, string, T] // [pattern, method, handler, path]
export class PatternRouter<T> implements Router<T> {
name: string = 'PatternRouter'
private routes: Route<T>[] = []
add(method: string, path: string, handler: T) {
const endsWithWildcard = path[path.length - 1] === '*'
if (endsWithWildcard) {
path = path.slice(0, -2)
}
const parts = path.match(/\/?(:\w+(?:{[^}]+})?)|\/?[^\/\?]+|(\?)/g) || []
if (parts[parts.length - 1] === '?') {
this.add(method, parts.slice(0, parts.length - 2).join(''), handler)
parts.pop()
}
for (let i = 0, len = parts.length; i < len; i++) {
const match = parts[i].match(/^\/:([^{]+)(?:{(.*)})?/)
if (match) {
parts[i] = `/(?<${match[1]}>${match[2] || '[^/]+'})`
} else if (parts[i] === '/*') {
parts[i] = '/[^/]+'
}
}
let re
try {
re = new RegExp(`^${parts.join('')}${endsWithWildcard ? '' : '/?$'}`)
} catch (e) {
throw new UnsupportedPathError()
}
this.routes.push([re, method, handler])
}
match(method: string, path: string): Result<T> {
const handlers: [T, Params][] = []
for (const [pattern, routeMethod, handler] of this.routes) {
if (routeMethod === METHOD_NAME_ALL || routeMethod === method) {
const match = pattern.exec(path)
if (match) {
handlers.push([handler, match.groups || {}])
}
}
}
return [handlers]
}
}