Skip to content

Commit

Permalink
handling errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspaonrocks committed Dec 20, 2017
1 parent 577a021 commit c1a16c0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 47 deletions.
42 changes: 26 additions & 16 deletions dist/src/route-mapper/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,51 @@ function default_1(context, dirName) {
'GET': function (router, path) {
var options = utils.options(path);
var ctrl = options.ctrl;
utils.hasReqParams(path) ?
router.get(options.url, function (req, res, next) { return modulesIndex[ctrl].find(req, res, next); }) :
router.get(path, function (req, res, next) { return modulesIndex[ctrl].list(req, res, next); });
if (modulesIndex[ctrl] != null) {
utils.hasReqParams(path) ?
router.get(options.url, function (req, res, next) { return modulesIndex[ctrl].find(req, res, next); }) :
router.get(path, function (req, res, next) { return modulesIndex[ctrl].list(req, res, next); });
}
},
'POST': function (router, path) {
var ctrl = path.replace('/', '');
router.post(path, function (req, res, next) {
return modulesIndex[ctrl].create(req, res, next);
});
if (modulesIndex[ctrl] != null) {
router.post(path, function (req, res, next) {
return modulesIndex[ctrl].create(req, res, next);
});
}
},
'PUT': function (router, path) {
var options = utils.options(path);
var ctrl = options.ctrl;
router.put(options.url, function (req, res, next) {
return modulesIndex[ctrl].update(req, res, next);
});
if (modulesIndex[ctrl] != null) {
router.put(options.url, function (req, res, next) {
return modulesIndex[ctrl].update(req, res, next);
});
}
},
'PATCH': function (router, path) {
var options = utils.options(path);
var ctrl = options.ctrl;
router.patch(options.url, function (req, res, next) {
return modulesIndex[ctrl].update(req, res, next);
});
if (modulesIndex[ctrl] != null) {
router.patch(options.url, function (req, res, next) {
return modulesIndex[ctrl].update(req, res, next);
});
}
},
'DELETE': function (router, path) {
var options = utils.options(path);
var ctrl = options.ctrl;
router.delete(options.url, function (req, res, next) {
return modulesIndex[ctrl].delete(req, res, next);
});
if (modulesIndex[ctrl] != null) {
router.delete(options.url, function (req, res, next) {
return modulesIndex[ctrl].delete(req, res, next);
});
}
}
};
router.all('*', function (req, res, next) {
if (typeof RoutesMapping[req.method] !== 'function') {
res.status(500).json("request not handled, it must be one of GET, POST, PUT, PATCH or DELETE.");
res.status(500).json("Request not handled, it must be one of GET, POST, PUT, PATCH or DELETE.");
}
else {
RoutesMapping[req.method](router, req.path);
Expand Down
41 changes: 26 additions & 15 deletions src/route-mapper/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,50 @@ module.exports = function (context, dirName) {
let options = utils.options(path);
let ctrl = options.ctrl;

utils.hasReqParams(path) ?
router.get(options.url, (req, res, next) => { return indexCtrl[ctrl].find(req, res, next); }) :
router.get(path, (req, res, next) => { return indexCtrl[ctrl].list(req, res, next); });
if (indexCtrl[ctrl] != null) {
utils.hasReqParams(path) ?
router.get(options.url, (req, res, next) => { return indexCtrl[ctrl].find(req, res, next); }) :
router.get(path, (req, res, next) => { return indexCtrl[ctrl].list(req, res, next); });
}
},
'POST': function (router, path) {
let ctrl = path.replace('/', '');
router.post(path, (req, res, next) => {
return indexCtrl[ctrl].create(req, res, next);
})

if (indexCtrl[ctrl] != null) {
router.post(path, (req, res, next) => {
return indexCtrl[ctrl].create(req, res, next);
})
}
},
'PUT': function (router, path) {
let options = utils.options(path);
let ctrl = options.ctrl;

router.put(options.url, (req, res, next) => {
return indexCtrl[ctrl].update(req, res, next);
})
if (indexCtrl[ctrl] != null) {
router.put(options.url, (req, res, next) => {
return indexCtrl[ctrl].update(req, res, next);
})
}
},
'PATCH': function (router, path) {
let options = utils.options(path);
let ctrl = options.ctrl;

router.patch(options.url, (req, res, next) => {
return indexCtrl[ctrl].update(req, res, next);
})
if (indexCtrl[ctrl] != null) {
router.patch(options.url, (req, res, next) => {
return indexCtrl[ctrl].update(req, res, next);
})
}
},
'DELETE': function (router, path) {
let options = utils.options(path);
let ctrl = options.ctrl;

router.delete(options.url, (req, res, next) => {
return indexCtrl[ctrl].delete(req, res, next);
})
if (indexCtrl[ctrl] != null) {
router.delete(options.url, (req, res, next) => {
return indexCtrl[ctrl].delete(req, res, next);
})
}
}
}

Expand Down
43 changes: 27 additions & 16 deletions src/route-mapper/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,56 @@ export default function (context: string, dirName: string): Router {
let options = utils.options(path);
let ctrl = options.ctrl;

utils.hasReqParams(path) ?
router.get(options.url, (req, res, next) => { return modulesIndex[ctrl].find(req, res, next); }) :
router.get(path, (req, res, next) => { return modulesIndex[ctrl].list(req, res, next); });
if (modulesIndex[ctrl] != null) {
utils.hasReqParams(path) ?
router.get(options.url, (req, res, next) => { return modulesIndex[ctrl].find(req, res, next); }) :
router.get(path, (req, res, next) => { return modulesIndex[ctrl].list(req, res, next); });
}
},
'POST': (router: Router, path: string): void => {
let ctrl = path.replace('/', '');
router.post(path, (req, res, next) => {
return modulesIndex[ctrl].create(req, res, next);
})

if (modulesIndex[ctrl] != null) {
router.post(path, (req, res, next) => {
return modulesIndex[ctrl].create(req, res, next);
})
}
},
'PUT': (router: Router, path: string): void => {
let options = utils.options(path);
let ctrl = options.ctrl;

router.put(options.url, (req, res, next) => {
return modulesIndex[ctrl].update(req, res, next);
})
if (modulesIndex[ctrl] != null) {
router.put(options.url, (req, res, next) => {
return modulesIndex[ctrl].update(req, res, next);
})
}
},
'PATCH': (router: Router, path: string): void => {
let options = utils.options(path);
let ctrl = options.ctrl;

router.patch(options.url, (req, res, next) => {
return modulesIndex[ctrl].update(req, res, next);
})
if (modulesIndex[ctrl] != null) {
router.patch(options.url, (req, res, next) => {
return modulesIndex[ctrl].update(req, res, next);
})
}
},
'DELETE': (router: Router, path: string): void => {
let options = utils.options(path);
let ctrl = options.ctrl;

router.delete(options.url, (req, res, next) => {
return modulesIndex[ctrl].delete(req, res, next);
})
if (modulesIndex[ctrl] != null) {
router.delete(options.url, (req, res, next) => {
return modulesIndex[ctrl].delete(req, res, next);
})
}
}
}

router.all('*', (req, res, next) => {
if (typeof RoutesMapping[req.method] !== 'function') {
res.status(500).json(`request not handled, it must be one of GET, POST, PUT, PATCH or DELETE.`)
res.status(500).json(`Request not handled, it must be one of GET, POST, PUT, PATCH or DELETE.`)
} else {
RoutesMapping[req.method](router, req.path);
next();
Expand Down

0 comments on commit c1a16c0

Please sign in to comment.