Added helper for handle methods
ADD
src/server/rest/router/RouterUtils.ts
+import { NextFunction, Request, Response } from 'express';
+
+import { ServerAction } from '../../../types/Server';
+
+export default class RouterUtils {
+ public static async handleServerAction(
+ handleMethod: (serverAction: ServerAction, req: Request, res: Response, next: NextFunction) => Promise<void>,
+ serverAction: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ try {
+ await handleMethod(serverAction, req, res, next);
+ next();
+ } catch (error) {
+ next(error);
+ }
+ }
+}

MODIFY
src/server/rest/router/api/TenantRouter.ts
src/server/rest/router/api/TenantRouter.ts
import express, { NextFunction, Request, Response } from 'express';
+import RouterUtils from '../RouterUtils';
import { ServerAction } from '../../../../types/Server';
import TenantService from '../../service/TenantService';
+import sanitize from 'mongo-sanitize';
export const tenantRouter = express.Router();
-// Define the home page route
tenantRouter.get('/tenants', async (req: Request, res: Response, next: NextFunction) => {
- try {
- await TenantService.handleGetTenants(ServerAction.TENANTS, req, res, next);
- next();
- } catch (error) {
- next(error);
- }
+ await RouterUtils.handleServerAction(TenantService.handleGetTenants.bind(this), ServerAction.TENANTS, req, res, next);
});
-// Define the about route
tenantRouter.get('/tenants/:id', async (req: Request, res: Response, next: NextFunction) => {
- try {
- req.query.ID = req.params.id;
- await TenantService.handleGetTenant(ServerAction.TENANT, req, res, next);
- next();
- } catch (error) {
- next(error);
- }
+ req.query.ID = sanitize(req.params.id);
+ await RouterUtils.handleServerAction(TenantService.handleGetTenant.bind(this), ServerAction.TENANT, req, res, next);
});

MODIFY
src/server/rest/router/auth/AuthRouter.ts
src/server/rest/router/auth/AuthRouter.ts
import express, { NextFunction, Request, Response } from 'express';
import AuthService from '../../service/AuthService';
+import RouterUtils from '../RouterUtils';
import { ServerAction } from '../../../../types/Server';
export const authRouter = express.Router();
authRouter.post('/login', async (req: Request, res: Response, next: NextFunction) => {
- try {
- await AuthService.handleLogIn(ServerAction.LOGIN, req, res, next);
- next();
- } catch (error) {
- next(error);
- }
+ await RouterUtils.handleServerAction(AuthService.handleLogIn.bind(this), ServerAction.LOGIN, req, res, next);
});

Added helper for handle methods