-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrouter_decorator.js
64 lines (57 loc) · 1.72 KB
/
router_decorator.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
61
62
63
64
const routerDecorator = {
__classPrefix__:{},
__router__: [],
get:undefined,
post:undefined,
put:undefined,
delete:undefined,
options:undefined,
head:undefined,
patch:undefined
};
module.exports = routerDecorator;
/**
* 路由初始化
* @param app Application对象
* @param options 默认值:{prefix: ''}
*/
routerDecorator.initRouter = (app,options={prefix: ''}) => {
routerDecorator.__router__.forEach(function(opt){//路由参数
const controllerPrefixData = routerDecorator.__classPrefix__[opt.className] || { prefix: ''};
const fullUrl = `${options.prefix}${controllerPrefixData.prefix}${opt.url}`;
app.router[opt.httpMethod](fullUrl,async (ctx) => {
const ist = new opt.constructorFn(ctx);
const res=await ist[opt.handlerName](ctx);
if(res!=undefined)
ctx.body=res;
});
});
};
/**
* 类装饰器,定义路由前缀
* @param prefix 类装饰器前缀
*/
routerDecorator.prefix=function(prefix){
return function(target){
routerDecorator.__classPrefix__[target.name]={
prefix
}
};
}
const methods = ['get', 'post', 'put', 'delete', 'options', 'head', 'patch'];
/**
* 方法装饰器,注入基本HTTP方法. 当方法有返回值时,会自动将返回值给ctx.body
*/
methods.forEach(httpMethod => {
routerDecorator[httpMethod] = (url='') => {
return function (target, name, descriptor) {
routerDecorator.__router__.push({
url,
httpMethod,
handlerName:name,
constructorFn: target.constructor,
className: target.constructor.name
});
}
}
});