|
|
@@ -1,100 +1,126 @@
|
|
|
-import * as controllers from './controllers/'
|
|
|
-import * as middlewares from './middleware/'
|
|
|
-import { routes } from './routes'
|
|
|
-import express from 'express'
|
|
|
-import { ApiError } from './middleware/ApiError'
|
|
|
+import * as controllers from "./controllers/";
|
|
|
+import * as middlewares from "./middleware/";
|
|
|
+import { routes } from "./routes";
|
|
|
+import express from "express";
|
|
|
+import { ApiError } from "./middleware/ApiError";
|
|
|
|
|
|
interface ControllerCollection {
|
|
|
- [key: string]: any
|
|
|
+ [key: string]: any;
|
|
|
}
|
|
|
|
|
|
-const controllerInstances: ControllerCollection = {}
|
|
|
-const router = express.Router()
|
|
|
+const controllerInstances: ControllerCollection = {};
|
|
|
+const router = express.Router();
|
|
|
|
|
|
export function loadRoute(): express.Router {
|
|
|
for (const route of routes) {
|
|
|
- const [controllerName, controllerAction] = route.action.split('@')
|
|
|
- let middlewareList: Array<express.RequestHandler> = []
|
|
|
+ const [controllerName, controllerAction] = route.action.split("@");
|
|
|
+ let middlewareList: Array<express.RequestHandler> = [];
|
|
|
|
|
|
// Check whether controller exists
|
|
|
if (!isControllerInitialized(controllerName)) {
|
|
|
if (isControllerExisted(controllerName)) {
|
|
|
- controllerInstances[controllerName] = new controllers[controllerName]()
|
|
|
+ controllerInstances[controllerName] = new controllers[controllerName]();
|
|
|
} else {
|
|
|
- throw new Error(`Controller ${controllerName} not exist.`)
|
|
|
+ throw new Error(`Controller ${controllerName} not exist.`);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (route.middlewares) {
|
|
|
- middlewareList = getMiddlewareList(route.middlewares)
|
|
|
+ middlewareList = getMiddlewareList(route.middlewares);
|
|
|
}
|
|
|
|
|
|
if (route.validator) {
|
|
|
- middlewareList.push(...route.validator, ApiError)
|
|
|
+ middlewareList.push(...route.validator, ApiError);
|
|
|
}
|
|
|
|
|
|
switch (route.method) {
|
|
|
- case 'get':
|
|
|
- router.get(route.url, middlewareList, controllerInstances[controllerName][controllerAction])
|
|
|
- break
|
|
|
-
|
|
|
- case 'post':
|
|
|
- router.post(route.url, middlewareList, controllerInstances[controllerName][controllerAction])
|
|
|
- break
|
|
|
-
|
|
|
- case 'delete':
|
|
|
- router.delete(route.url, middlewareList, controllerInstances[controllerName][controllerAction])
|
|
|
- break
|
|
|
-
|
|
|
- case 'patch':
|
|
|
- router.patch(route.url, middlewareList, controllerInstances[controllerName][controllerAction])
|
|
|
- break
|
|
|
-
|
|
|
- case 'put':
|
|
|
- router.put(route.url, middlewareList, controllerInstances[controllerName][controllerAction])
|
|
|
- break
|
|
|
+ case "get":
|
|
|
+ router.get(
|
|
|
+ route.url,
|
|
|
+ middlewareList,
|
|
|
+ controllerInstances[controllerName][controllerAction]
|
|
|
+ );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "post":
|
|
|
+ router.post(
|
|
|
+ route.url,
|
|
|
+ middlewareList,
|
|
|
+ controllerInstances[controllerName][controllerAction]
|
|
|
+ );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "delete":
|
|
|
+ router.delete(
|
|
|
+ route.url,
|
|
|
+ middlewareList,
|
|
|
+ controllerInstances[controllerName][controllerAction]
|
|
|
+ );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "patch":
|
|
|
+ router.patch(
|
|
|
+ route.url,
|
|
|
+ middlewareList,
|
|
|
+ controllerInstances[controllerName][controllerAction]
|
|
|
+ );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "put":
|
|
|
+ router.put(
|
|
|
+ route.url,
|
|
|
+ middlewareList,
|
|
|
+ controllerInstances[controllerName][controllerAction]
|
|
|
+ );
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return router
|
|
|
+ return router;
|
|
|
}
|
|
|
|
|
|
-function getMiddlewareList(middlewareNames: Array<string>): Array<express.RequestHandler> {
|
|
|
- const middlewareList = []
|
|
|
+function getMiddlewareList(
|
|
|
+ middlewareNames: Array<string>
|
|
|
+): Array<express.RequestHandler> {
|
|
|
+ const middlewareList = [];
|
|
|
|
|
|
for (const middlewareName of middlewareNames) {
|
|
|
if (!isMiddlewareExists(middlewareName)) {
|
|
|
- throw new Error(`Middleware ${middlewareName} not exist.`)
|
|
|
+ throw new Error(`Middleware ${middlewareName} not exist.`);
|
|
|
}
|
|
|
|
|
|
- middlewareList.push(middlewares[middlewareName])
|
|
|
+ middlewareList.push(middlewares[middlewareName]);
|
|
|
}
|
|
|
|
|
|
- return middlewareList
|
|
|
+ return middlewareList;
|
|
|
}
|
|
|
|
|
|
function isControllerInitialized(controller: string): boolean {
|
|
|
- return controller in controllerInstances
|
|
|
+ return controller in controllerInstances;
|
|
|
}
|
|
|
|
|
|
-function isControllerExisted(controller: string): controller is keyof typeof controllers {
|
|
|
- return controller in controllers
|
|
|
+function isControllerExisted(
|
|
|
+ controller: string
|
|
|
+): controller is keyof typeof controllers {
|
|
|
+ return controller in controllers;
|
|
|
}
|
|
|
|
|
|
function isActionExisted(controller: object, action: string) {
|
|
|
- return action in controller
|
|
|
+ return action in controller;
|
|
|
}
|
|
|
|
|
|
-function isMiddlewareExists(middlewareName: string): middlewareName is keyof typeof middlewares {
|
|
|
- return middlewareName in middlewares
|
|
|
+function isMiddlewareExists(
|
|
|
+ middlewareName: string
|
|
|
+): middlewareName is keyof typeof middlewares {
|
|
|
+ return middlewareName in middlewares;
|
|
|
}
|
|
|
|
|
|
function isMethodExisted(method: string): method is keyof typeof router {
|
|
|
- const httpMethods = ['get', 'post', 'delete', 'put', 'patch']
|
|
|
+ const httpMethods = ["get", "post", "delete", "put", "patch"];
|
|
|
|
|
|
if (!httpMethods.includes(method)) {
|
|
|
- return false
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
- return method in router
|
|
|
+ return method in router;
|
|
|
}
|