From 0a7332bd520e9901f37576e4d07870d23430ac63 Mon Sep 17 00:00:00 2001 From: Alexinhans <103999185+Alexinhans@users.noreply.github.com> Date: Sun, 21 Jan 2024 13:11:10 +0000 Subject: [PATCH] fix: now nested routes registering aswell --- .env.development | 4 ---- src/api/index.ts | 10 +++++++--- src/application.ts | 13 ++++++------- 3 files changed, 13 insertions(+), 14 deletions(-) delete mode 100644 .env.development diff --git a/.env.development b/.env.development deleted file mode 100644 index af53529..0000000 --- a/.env.development +++ /dev/null @@ -1,4 +0,0 @@ -NODE_ENV=development -NUMBER=0 -BOOLEAN=true -PORT=5000 \ No newline at end of file diff --git a/src/api/index.ts b/src/api/index.ts index 5a09d30..ebf2865 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,5 +1,4 @@ import { Endpoint, Endpoints } from '../interfaces'; -import { appLogger } from '../lib/logger'; import systemHealthRouter from './system-health'; import mainRouter from './v1/main'; @@ -9,8 +8,14 @@ const v1Endpoints: Endpoint = { router: mainRouter, endpoints: [ { - route: '/system-health', + route: '/hi', router: systemHealthRouter, + endpoints: [ + { + route: '/hello', + router: systemHealthRouter, + }, + ], }, ], }; @@ -23,5 +28,4 @@ const endpoints: Endpoints = [ }, ]; -appLogger.info(endpoints); export default endpoints; diff --git a/src/application.ts b/src/application.ts index cbdd746..08e1664 100644 --- a/src/application.ts +++ b/src/application.ts @@ -22,18 +22,17 @@ app.get('/', (req, res) => { res.redirect('https://google.com'); }); -// FIXME: REGISTER NESTED ROUTES AS WELL | IMPORTANT | -const useEndpoints = (es: Endpoints) => +const registerEndpoints = (es: Endpoints, parentRoute: string = '') => es.forEach((e) => { - appLogger.info(e.route); - + const currentRoute = `${parentRoute}${e.route}`; + appLogger.info('Registering route:', currentRoute); if (e.endpoints) { - useEndpoints(e.endpoints); + registerEndpoints(e.endpoints, currentRoute); } - app.use(e.route, e.router); + app.use(currentRoute, e.router); }); -useEndpoints(endpoints); +registerEndpoints(endpoints); app.use(notFoundHandler); app.use(errorHandler);