-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
60 lines (60 loc) · 1.85 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
54
55
56
57
58
59
60
'use strict';
const is = require('is-type-of');
const assert = require('assert');
const methods = ['head', 'options', 'get', 'put', 'patch', 'post', 'delete', 'del', 'all', 'resources'];
const Router = require("koa-router");
class RouterPlus extends Router {
constructor(opt) {
super(opt);
}
/**
* get sub router
*
* @method Router#namespace
* @param {String} prefix - sub router prefix
* @param {...Function} [middlewares] - optional middlewares
* @return {Router} Return sub router with special prefix
*/
namespace(prefix, ...middlewares) {
assert(is.string(prefix), `only support prefix with string, but got ${prefix}`);
const fnCache = {};
// mock router
const proxy = new Proxy(this, {
get(target, property) {
if (methods.includes(property)) {
if (!fnCache[property]) {
fnCache[property] = proxyFn(target, property, prefix, middlewares, proxy);
}
return fnCache[property];
}
return target[property];
},
});
return proxy;
}
}
function proxyFn(target, property, prefix, middlewares, routerProxy) {
const fn = target[property];
const proxy = new Proxy(fn, {
apply(targetFn, ctx, args) {
if (args.length >= 3 && (is.string(args[1]) || is.regExp(args[1]))) {
// app.get(name, url, [...middleware], controller)
args[1] = addPrefix(prefix, args[1]);
args.splice(2, 0, ...middlewares);
}
else {
// app.get(url, [...middleware], controller)
args[0] = addPrefix(prefix, args[0]);
args.splice(1, 0, ...middlewares);
}
Reflect.apply(targetFn, ctx, args);
return routerProxy;
},
});
return proxy;
}
function addPrefix(prefix, path) {
assert(is.string(path), `only support path with string, but got ${path}`);
return prefix + path;
}
module.exports = RouterPlus;