-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.js
62 lines (47 loc) · 1.64 KB
/
decorators.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
import 'reflect-metadata';
const metakeys = ["namespace", "Get", "Post", "Delete", "Put", "Head"];
const decorators = {}
metakeys.map(key => {
decorators[key] = (path) => (target, name, descriptor) => {
Reflect.defineMetadata(key, {
path,
prototype: name,
method: key
}, target, name);
}
})
function getRoutes(classname, app) {
const c = new classname(app.ctx)
let routes = {};
const prefix = Reflect.getMetadata('namespace', classname) || '';
const ownPropertyNames = Object.getOwnPropertyNames(classname['prototype']);
ownPropertyNames.forEach(propertyName => {
metakeys.reduce((ret, cur) => {
let subroute = Reflect.getMetadata(cur, classname['prototype'], propertyName);
if (subroute) {
let path;
if (prefix.path && prefix.path.length > 1) { //: prefix='/'
path = `${prefix.path}${subroute.path}`
} else {
path = `${subroute.path}`
}
app.$route.register(path, {
method: subroute.method.toLowerCase()
}, c[subroute.prototype].bind(c))
}
}, '')
})
}
decorators['routes'] = getRoutes;
export default decorators;
// console.log(Reflect.getMetadata('Get', Controller.prototype, 'renderEmpty'))
// console.log(Reflect.getMetadata('Get', Controller.prototype))
// console.log('@@@@-----')
// console.log(Reflect.getMetadata('namespace', Controller))
// const keys = Object.getOwnPropertyNames(Controller.prototype);
// console.log(keys)
// keys.forEach(key => {
// console.log(Reflect.getMetadata('Get', Controller.prototype, key))
// })
// console.log('@@@@-----end')
// console.log(Object.keys(Controller.prototype))