Skip to content

Commit

Permalink
fix: now nested routes registering aswell
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexinhans committed Jan 21, 2024
1 parent c734527 commit 0a7332b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
4 changes: 0 additions & 4 deletions .env.development

This file was deleted.

10 changes: 7 additions & 3 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Endpoint, Endpoints } from '../interfaces';
import { appLogger } from '../lib/logger';
import systemHealthRouter from './system-health';

import mainRouter from './v1/main';
Expand All @@ -9,8 +8,14 @@ const v1Endpoints: Endpoint = {
router: mainRouter,
endpoints: [
{
route: '/system-health',
route: '/hi',
router: systemHealthRouter,
endpoints: [
{
route: '/hello',
router: systemHealthRouter,
},
],
},
],
};
Expand All @@ -23,5 +28,4 @@ const endpoints: Endpoints = [
},
];

appLogger.info(endpoints);
export default endpoints;
13 changes: 6 additions & 7 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0a7332b

Please sign in to comment.