2023-04-26 03:15:45 +02:00
|
|
|
import type { Result, Router } from '../../router.ts'
|
|
|
|
import { METHOD_NAME_ALL } from '../../router.ts'
|
|
|
|
|
2023-06-03 02:08:01 +02:00
|
|
|
type Route<T> = [RegExp, string, T] // [pattern, method, handler, path]
|
2023-04-26 03:15:45 +02:00
|
|
|
|
|
|
|
export class PatternRouter<T> implements Router<T> {
|
2023-05-17 17:05:28 +02:00
|
|
|
name: string = 'PatternRouter'
|
2023-04-26 03:15:45 +02:00
|
|
|
private routes: Route<T>[] = []
|
2023-04-30 14:07:00 +02:00
|
|
|
private dNames: Record<string, number> = {} // Short name of duplicatedNames
|
2023-04-26 03:15:45 +02:00
|
|
|
|
|
|
|
add(method: string, path: string, handler: T) {
|
2023-04-30 14:18:32 +02:00
|
|
|
const endsWithWildcard = path[path.length - 1] === '*'
|
2023-04-26 03:15:45 +02:00
|
|
|
if (endsWithWildcard) {
|
|
|
|
path = path.slice(0, -2)
|
|
|
|
}
|
|
|
|
|
2023-05-18 08:03:35 +02:00
|
|
|
const parts = path.match(/\/?(:\w+(?:{[^}]+})?)|\/?[^\/\?]+|(\?)/g) || []
|
2023-04-26 03:15:45 +02:00
|
|
|
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++) {
|
|
|
|
// Check duplicated names
|
|
|
|
const match = parts[i].match(/^\/:([^{]+)(?:{(.*)})?/)
|
|
|
|
if (match) {
|
|
|
|
const label = match[1]
|
2023-04-30 14:07:00 +02:00
|
|
|
const pos = this.dNames[label]
|
2023-04-26 03:15:45 +02:00
|
|
|
if (typeof pos === 'number' && pos !== i) {
|
|
|
|
throw new Error(
|
|
|
|
`Duplicate param name, use another name instead of '${label}' - ${method} ${path} <--- '${label}'`
|
|
|
|
)
|
|
|
|
}
|
2023-04-30 14:07:00 +02:00
|
|
|
this.dNames[label] = i
|
2023-04-26 03:15:45 +02:00
|
|
|
|
|
|
|
parts[i] = `/(?<${label}>${match[2] || '[^/]+'})`
|
|
|
|
} else if (parts[i] === '/*') {
|
|
|
|
parts[i] = '/[^/]+'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.routes.push([
|
|
|
|
new RegExp(`^${parts.join('')}${endsWithWildcard ? '' : '/?$'}`),
|
|
|
|
method,
|
|
|
|
handler,
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
match(method: string, path: string): Result<T> | null {
|
|
|
|
const handlers: T[] = []
|
2023-06-03 02:08:01 +02:00
|
|
|
const params: Record<string, string> = {}
|
|
|
|
|
2023-04-26 03:15:45 +02:00
|
|
|
for (const [pattern, routeMethod, handler] of this.routes) {
|
2023-06-03 02:08:01 +02:00
|
|
|
const isRegExp = pattern.source.charCodeAt(pattern.source.length - 1) === 36
|
2023-04-26 03:15:45 +02:00
|
|
|
if (routeMethod === METHOD_NAME_ALL || routeMethod === method) {
|
|
|
|
const match = pattern.exec(path)
|
|
|
|
if (match) {
|
|
|
|
handlers.push(handler)
|
2023-06-03 02:08:01 +02:00
|
|
|
if (isRegExp) {
|
|
|
|
Object.assign(params, match.groups)
|
2023-04-26 03:15:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-03 02:08:01 +02:00
|
|
|
|
2023-04-26 03:15:45 +02:00
|
|
|
return handlers.length
|
|
|
|
? {
|
|
|
|
handlers,
|
2023-06-03 02:08:01 +02:00
|
|
|
params,
|
2023-04-26 03:15:45 +02:00
|
|
|
}
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
}
|