-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.js
53 lines (48 loc) · 1.41 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const { METHODS } = require('http');
const Router = require('./core/router');
Router.route = (method = '*', path = '/', options = {}) => {
return (controller, action) => {
controller.__routes = controller.__routes || [];
controller.__routes.push({
method, path, action, options
});
};
};
// alias
METHODS.forEach(method => {
Router[method.toLowerCase()] = Router.route.bind(null, method);
});
Router.all = Router.route.bind(null, null);
Router.restful = (path, options) => {
const methods = {
get: 'index',
put: 'update',
post: 'create',
delete: 'remove'
};
return target => {
path = (path || `/${target.name}`).toLowerCase();
path += '/:id?';
target.prototype.__routes = target.prototype.__routes || [];
Object.keys(methods).forEach(method => {
target.prototype.__routes.push({
method, path, action: methods[method], options
});
});
};
};
/**
* prefix
*/
Router.prefix = (prefix = '') => {
if (typeof prefix !== 'string')
throw new TypeError(`[kelp-next] "prefix" must a string, but got a "${typeof prefix}".`)
return ctrl => {
if (!ctrl.prototype.__routes)
throw new Error(`[kelp-next] controller "${ctrl.name}" haven't set router yet, you may need to call functions like "@get", "@post", "@restfull".`);
ctrl.prototype.__routes.forEach(route => {
route.path = prefix + route.path;
});
};
};
module.exports = Router;