index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // src/helper/dev/index.ts
  2. import { getColorEnabled } from "../../utils/color.js";
  3. import { findTargetHandler, isMiddleware } from "../../utils/handler.js";
  4. var handlerName = (handler) => {
  5. return handler.name || (isMiddleware(handler) ? "[middleware]" : "[handler]");
  6. };
  7. var inspectRoutes = (hono) => {
  8. return hono.routes.map(({ path, method, handler }) => {
  9. const targetHandler = findTargetHandler(handler);
  10. return {
  11. path,
  12. method,
  13. name: handlerName(targetHandler),
  14. isMiddleware: isMiddleware(targetHandler)
  15. };
  16. });
  17. };
  18. var showRoutes = (hono, opts) => {
  19. const colorEnabled = opts?.colorize ?? getColorEnabled();
  20. const routeData = {};
  21. let maxMethodLength = 0;
  22. let maxPathLength = 0;
  23. inspectRoutes(hono).filter(({ isMiddleware: isMiddleware2 }) => opts?.verbose || !isMiddleware2).map((route) => {
  24. const key = `${route.method}-${route.path}`;
  25. (routeData[key] ||= []).push(route);
  26. if (routeData[key].length > 1) {
  27. return;
  28. }
  29. maxMethodLength = Math.max(maxMethodLength, route.method.length);
  30. maxPathLength = Math.max(maxPathLength, route.path.length);
  31. return { method: route.method, path: route.path, routes: routeData[key] };
  32. }).forEach((data) => {
  33. if (!data) {
  34. return;
  35. }
  36. const { method, path, routes } = data;
  37. const methodStr = colorEnabled ? `\x1B[32m${method}\x1B[0m` : method;
  38. console.log(`${methodStr} ${" ".repeat(maxMethodLength - method.length)} ${path}`);
  39. if (!opts?.verbose) {
  40. return;
  41. }
  42. routes.forEach(({ name }) => {
  43. console.log(`${" ".repeat(maxMethodLength + 3)} ${name}`);
  44. });
  45. });
  46. };
  47. var getRouterName = (app) => {
  48. app.router.match("GET", "/");
  49. return app.router.name;
  50. };
  51. export {
  52. getRouterName,
  53. inspectRoutes,
  54. showRoutes
  55. };