-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (33 loc) · 1.2 KB
/
index.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
require('reflect-metadata');
const methods = ["Get", "Post", "Delete", "Put", "Head"];
const controller = ['Namespace', 'Controller'];
const service = ['Service'];
const metakeys = ["Controller", "Get", "Post", "Delete", "Put", "Head"];
module.exports["methods"] = methods.map(item => item.toLowerCase());
metakeys.map(key => {
module.exports[key] = (path) => (target, name, descriptor) => {
Reflect.defineMetadata(key, {
path,
prototype: name,
method: key
}, target, name);
}
})
module.exports['Service'] = (service) => (target, name, descriptor) => {
Reflect.defineMetadata('Service', service, target, name);
}
module.exports['getServiceName'] = (classname) => {
return Reflect.getMetadata('Service', classname);
}
module.exports['iterator'] = function (classname, callback) {
const prefix = Reflect.getMetadata('Controller', classname) || '';
const ownPropertyNames = Object.getOwnPropertyNames(classname['prototype']);
ownPropertyNames.forEach(propertyName => {
metakeys.reduce((ret, cur) => {
let subroute = Reflect.getMetadata(cur, classname['prototype'], propertyName);
if (subroute) {
callback && callback(prefix, subroute)
}
}, '')
})
}