From 95608fc0a59dd3074db3b14d04967aad6961b4c5 Mon Sep 17 00:00:00 2001 From: creations Date: Mon, 12 Aug 2024 19:19:05 -0400 Subject: [PATCH] fix route registration --- src/fastify/manager.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/fastify/manager.ts b/src/fastify/manager.ts index 8210181..28b4432 100644 --- a/src/fastify/manager.ts +++ b/src/fastify/manager.ts @@ -58,7 +58,7 @@ class FastifyManager { ]; for (const [routePath, prefix, recursive] of routePaths) { - const modifiedRoutePath = join(environment.paths.www.root, routePath); + const modifiedRoutePath: string = join(environment.paths.www.root, routePath); let files: string[]; try { @@ -72,27 +72,30 @@ class FastifyManager { try { const routeModule = await import(file); const { default: routeData } = routeModule; - - if ( !routeData || !routeData.routeInfo || !routeData.route ) { + + if (!routeData || !routeData.routeInfo || !routeData.route) { console.error(`Failed to load route from ${file}:`, "Route data is missing"); continue; } - + if (routeData.routeInfo.enabled && routeData.route) { const { routeInfo, route } = routeData; - + let routePath = routeInfo.path || "/"; - - // Handle prefix and leading/trailing slashes + if (prefix) { routePath = routePath === "/" ? prefix : join(prefix, routePath); } - - // Normalize the path to avoid duplicate slashes + + routePath = routePath.replace(/\\/g, '/'); + + if (!routePath.startsWith('/')) { + routePath = '/' + routePath; + } + routePath = routePath.replace(/\/+/g, "/"); - const methods = Array.isArray(routeInfo.method) ? routeInfo.method : [routeInfo.method]; - + for (const method of methods) { this.server.route({ method, @@ -104,7 +107,7 @@ class FastifyManager { } catch (err) { console.error(`Failed to load route from ${file}:`, err); } - } + } } }