index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var dev_exports = {};
  19. __export(dev_exports, {
  20. getRouterName: () => getRouterName,
  21. inspectRoutes: () => inspectRoutes,
  22. showRoutes: () => showRoutes
  23. });
  24. module.exports = __toCommonJS(dev_exports);
  25. var import_color = require("../../utils/color");
  26. var import_handler = require("../../utils/handler");
  27. const handlerName = (handler) => {
  28. return handler.name || ((0, import_handler.isMiddleware)(handler) ? "[middleware]" : "[handler]");
  29. };
  30. const inspectRoutes = (hono) => {
  31. return hono.routes.map(({ path, method, handler }) => {
  32. const targetHandler = (0, import_handler.findTargetHandler)(handler);
  33. return {
  34. path,
  35. method,
  36. name: handlerName(targetHandler),
  37. isMiddleware: (0, import_handler.isMiddleware)(targetHandler)
  38. };
  39. });
  40. };
  41. const showRoutes = (hono, opts) => {
  42. const colorEnabled = opts?.colorize ?? (0, import_color.getColorEnabled)();
  43. const routeData = {};
  44. let maxMethodLength = 0;
  45. let maxPathLength = 0;
  46. inspectRoutes(hono).filter(({ isMiddleware: isMiddleware2 }) => opts?.verbose || !isMiddleware2).map((route) => {
  47. const key = `${route.method}-${route.path}`;
  48. (routeData[key] ||= []).push(route);
  49. if (routeData[key].length > 1) {
  50. return;
  51. }
  52. maxMethodLength = Math.max(maxMethodLength, route.method.length);
  53. maxPathLength = Math.max(maxPathLength, route.path.length);
  54. return { method: route.method, path: route.path, routes: routeData[key] };
  55. }).forEach((data) => {
  56. if (!data) {
  57. return;
  58. }
  59. const { method, path, routes } = data;
  60. const methodStr = colorEnabled ? `\x1B[32m${method}\x1B[0m` : method;
  61. console.log(`${methodStr} ${" ".repeat(maxMethodLength - method.length)} ${path}`);
  62. if (!opts?.verbose) {
  63. return;
  64. }
  65. routes.forEach(({ name }) => {
  66. console.log(`${" ".repeat(maxMethodLength + 3)} ${name}`);
  67. });
  68. });
  69. };
  70. const getRouterName = (app) => {
  71. app.router.match("GET", "/");
  72. return app.router.name;
  73. };
  74. // Annotate the CommonJS export names for ESM import in node:
  75. 0 && (module.exports = {
  76. getRouterName,
  77. inspectRoutes,
  78. showRoutes
  79. });