Files
SnowLuma/.pnpm-store/v11/files/4e/921b19406e869fa529810a1ed78c744a3998ab558c3f9406d5b15b14385db98c7d5cf362682c4e35790950e4d6a4d817364391f0b7286102894bc59a8d4826
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

51 lines
1.7 KiB
Plaintext

import { getColorEnabled } from "../../utils/color.js";
import { findTargetHandler, isMiddleware } from "../../utils/handler.js";
//#region src/helper/dev/index.ts
const handlerName = (handler) => {
return handler.name || (isMiddleware(handler) ? "[middleware]" : "[handler]");
};
const inspectRoutes = (hono) => {
return hono.routes.map(({ path, method, handler }) => {
const targetHandler = findTargetHandler(handler);
return {
path,
method,
name: handlerName(targetHandler),
isMiddleware: isMiddleware(targetHandler)
};
});
};
const showRoutes = (hono, opts) => {
const colorEnabled = opts?.colorize ?? getColorEnabled();
const 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;
const methodStr = colorEnabled ? `\x1b[32m${method}\x1b[0m` : method;
console.log(`${methodStr} ${" ".repeat(maxMethodLength - method.length)} ${path}`);
if (!opts?.verbose) return;
routes.forEach(({ name }) => {
console.log(`${" ".repeat(maxMethodLength + 3)} ${name}`);
});
});
};
const getRouterName = (app) => {
app.router.match("GET", "/");
return app.router.name;
};
//#endregion
export { getRouterName, inspectRoutes, showRoutes };