nucklejs
Version:

36 lines (35 loc) • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRouter = void 0;
const express_1 = require("express");
const createRouter = (routes) => {
if (!routes)
throw Error('No routes defined. Expects an array of objects -> createRouter()');
if (typeof (routes) !== 'object')
throw new Error('createRouter() expect an array but got ' + typeof (routes));
const router = (0, express_1.Router)();
routes.forEach((route) => {
if (route.guard) {
if (route.method === 'GET')
router.get(route.path, route.guard, route.controller);
if (route.method === 'POST')
router.post(route.path, route.guard, route.controller);
if (route.method === 'PUT')
router.put(route.path, route.guard, route.controller);
if (route.method === 'DELETE')
router.delete(route.path, route.guard, route.controller);
}
else {
if (route.method === 'GET')
router.get(route.path, route.controller);
if (route.method === 'POST')
router.post(route.path, route.controller);
if (route.method === 'PUT')
router.put(route.path, route.controller);
if (route.method === 'DELETE')
router.delete(route.path, route.controller);
}
});
return router;
};
exports.createRouter = createRouter;