Skip to content

Commit

Permalink
better typings, unknown request method error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspaonrocks committed Dec 20, 2017
1 parent a30a080 commit 208d1ea
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 124 deletions.
2 changes: 0 additions & 2 deletions dist/global-modules-indexer/index.d.ts

This file was deleted.

25 changes: 0 additions & 25 deletions dist/global-modules-indexer/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions dist/route-mapper/map.d.ts

This file was deleted.

52 changes: 0 additions & 52 deletions dist/route-mapper/map.js

This file was deleted.

2 changes: 1 addition & 1 deletion dist/src/global-modules-indexer/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
declare function GlobalModulesIndexer(context: string, dirName: string): Object;
declare function GlobalModulesIndexer(context: string, dirName: string): object;
export default GlobalModulesIndexer;
9 changes: 7 additions & 2 deletions dist/src/route-mapper/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ function default_1(context, dirName) {
}
};
router.all('*', function (req, res, next) {
RoutesMapping[req.method](router, req.path);
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.");
}
else {
RoutesMapping[req.method](router, req.path);
next();
}
});
return router;
}
Expand Down
5 changes: 0 additions & 5 deletions dist/utils/utils.d.ts

This file was deleted.

21 changes: 0 additions & 21 deletions dist/utils/utils.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/global-modules-indexer/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs = require('fs');
import path = require('path');

let modulesIndex: Object = {};
let modulesIndex: object = {};

function startRecursiveCheck(path: string): void {
fs.readdirSync(path).forEach(e => {
Expand All @@ -19,7 +19,7 @@ function nextChecks(name: string, path: string): void {
};


function GlobalModulesIndexer(context: string, dirName: string): Object {
function GlobalModulesIndexer(context: string, dirName: string): object {
let absolutePath = path.join(context, dirName);

startRecursiveCheck(absolutePath);
Expand Down
8 changes: 6 additions & 2 deletions src/route-mapper/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ module.exports = function (context, dirName) {
}

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

return router;
Expand Down
18 changes: 11 additions & 7 deletions src/route-mapper/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@ export default function (context: string, dirName: string): Router {
let utils = Utils;

const RoutesMapping = {
'GET': function (router: Router, path: string) {
'GET': (router: Router, path: string): void => {
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); });
},
'POST': function (router, path) {
'POST': (router: Router, path: string): void => {
let ctrl = path.replace('/', '');
router.post(path, (req, res, next) => {
return modulesIndex[ctrl].create(req, res, next);
})
},
'PUT': function (router, path) {
'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);
})
},
'PATCH': function (router, path) {
'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);
})
},
'DELETE': function (router, path) {
'DELETE': (router: Router, path: string): void => {
let options = utils.options(path);
let ctrl = options.ctrl;

Expand All @@ -50,8 +50,12 @@ export default function (context: string, dirName: string): Router {
}

router.all('*', (req, res, next) => {
RoutesMapping[req.method](router, req.path);
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.`)
} else {
RoutesMapping[req.method](router, req.path);
next();
}
});

return router;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default {
hasReqParams: function (path: string): boolean {
hasReqParams: (path: string): boolean => {
return path.split('/').filter(e => e.length > 0).length > 1;
},
options: function (path: string): any {
options: (path: string): any => {
let config = {
ctrl: '',
url: '/'
Expand Down

0 comments on commit 208d1ea

Please sign in to comment.