2023-04-21 22:43:30 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
import OriginalRouter from '@medley/router'
|
|
|
|
// Should be exported from `hono/router`
|
|
|
|
import type { Result, Router } from 'hono/dist/types/router'
|
|
|
|
|
|
|
|
export class MedleyRouter<T> implements Router<T> {
|
|
|
|
router: any
|
2023-06-26 15:32:30 +08:00
|
|
|
name: string = 'MedleyRouter'
|
2023-04-21 22:43:30 +08:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.router = new OriginalRouter()
|
|
|
|
}
|
|
|
|
|
|
|
|
add(method: string, path: string, handler: T) {
|
|
|
|
const store = this.router.register(path)
|
|
|
|
store[method] = handler
|
|
|
|
}
|
|
|
|
|
|
|
|
match(method: string, path: string): Result<T> | null {
|
|
|
|
const route = this.router.find(path)
|
|
|
|
|
|
|
|
if (route) {
|
|
|
|
return {
|
|
|
|
handlers: [route['store'][method]],
|
|
|
|
params: route['params'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|