mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 10:51:01 +00:00
bd36ad10fc
* feat(dev): Introduce "dev" helper * feat(dev): Expose "dev" helper * refactor: Use "named function" in some middleware. * feat: `app.showRoutes()` is now deprecated. Use `showRoutes()` in `helper` instead. * refactor: export RouterRoute interface for utility * refactor: remove captureRouteStackTrace, add inspectRoutes `captureRouteStackTrace` will be implemented after some more thought. Instead, I added `inspectRoutes` to get routes as data. * test: add tests for helper/dev/index.ts * fix: run `format:fix` * refactor: use named functions for middleware * chore: denoify * docs: tweaks deprecation warning message * refactor(dev): Simplification of showList options * chore: denoify
63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import type { Hono } from '../../hono.ts'
|
|
import type { RouterRoute } from '../../hono-base.ts'
|
|
|
|
interface ShowRoutesOptions {
|
|
verbose?: boolean
|
|
}
|
|
|
|
interface RouteData {
|
|
path: string
|
|
method: string
|
|
name: string
|
|
isMiddleware: boolean
|
|
}
|
|
|
|
const isMiddleware = (handler: Function) => handler.length > 1
|
|
const handlerName = (handler: Function) => {
|
|
return handler.name || (isMiddleware(handler) ? '[middleware]' : '[handler]')
|
|
}
|
|
|
|
export const inspectRoutes = (hono: Hono): RouteData[] => {
|
|
return hono.routes.map(({ path, method, handler }: RouterRoute) => ({
|
|
path,
|
|
method,
|
|
name: handlerName(handler),
|
|
isMiddleware: isMiddleware(handler),
|
|
}))
|
|
}
|
|
|
|
export const showRoutes = (hono: Hono, opts?: ShowRoutesOptions) => {
|
|
const routeData: Record<string, RouteData[]> = {}
|
|
let maxMethodLength = 0
|
|
let maxPathLength = 0
|
|
|
|
inspectRoutes(hono)
|
|
.filter(({ isMiddleware }) => opts?.verbose || !isMiddleware)
|
|
.map((route) => {
|
|
const key = `${route.method}-${route.path}`
|
|
;(routeData[key] ||= []).push(route)
|
|
if (routeData[key].length > 1) {
|
|
return
|
|
}
|
|
maxMethodLength = Math.max(maxMethodLength, route.method.length)
|
|
maxPathLength = Math.max(maxPathLength, route.path.length)
|
|
return { method: route.method, path: route.path, routes: routeData[key] }
|
|
})
|
|
.forEach((data) => {
|
|
if (!data) {
|
|
return
|
|
}
|
|
const { method, path, routes } = data
|
|
|
|
console.log(`\x1b[32m${method}\x1b[0m ${' '.repeat(maxMethodLength - method.length)} ${path}`)
|
|
|
|
if (!opts?.verbose) {
|
|
return
|
|
}
|
|
|
|
routes.forEach(({ name }) => {
|
|
console.log(`${' '.repeat(maxMethodLength + 3)} ${name}`)
|
|
})
|
|
})
|
|
}
|