31 lines
959 B
TypeScript
31 lines
959 B
TypeScript
|
|
// Función para listar rutas
|
||
|
|
export function listRoutes(appOrRouter, basePath = "") {
|
||
|
|
const routes = [];
|
||
|
|
|
||
|
|
appOrRouter.stack.forEach((middleware) => {
|
||
|
|
if (middleware.route) {
|
||
|
|
// Es una ruta directa
|
||
|
|
const methods = Object.keys(middleware.route.methods).map((m) => m.toUpperCase());
|
||
|
|
routes.push({
|
||
|
|
path: basePath + middleware.route.path,
|
||
|
|
methods,
|
||
|
|
});
|
||
|
|
} else if (middleware.name === "router" && middleware.handle.stack) {
|
||
|
|
// Es un router anidado
|
||
|
|
const newBasePath =
|
||
|
|
basePath +
|
||
|
|
(middleware.regexp?.source !== "^\\/?$"
|
||
|
|
? middleware.regexp
|
||
|
|
?.toString()
|
||
|
|
.replace(/^\/\^\\/, "")
|
||
|
|
.replace(/\\\/\?\(\?=\\\/\|\$\)\/i$/, "")
|
||
|
|
.replace(/\\\//g, "/")
|
||
|
|
: "");
|
||
|
|
const childRoutes = listRoutes(middleware.handle, basePath + (middleware?.path || ""));
|
||
|
|
routes.push(...childRoutes);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
return routes;
|
||
|
|
}
|