-
-
Notifications
You must be signed in to change notification settings - Fork 286
/
inject.ts
48 lines (41 loc) · 1.29 KB
/
inject.ts
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
import {getDecoratorType, Metadata, Store, UnsupportedDecoratorType} from "@tsed/core";
/**
*
* @param symbol
* @returns {Function}
* @decorator
*/
export function Inject(symbol?: any): Function {
return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<Function> | number): any => {
const bindingType = getDecoratorType([target, propertyKey, descriptor], true);
switch (bindingType) {
case "parameter":
case "parameter.constructor":
if (symbol) {
const paramTypes = Metadata.getParamTypes(target, propertyKey);
paramTypes[descriptor as number] = symbol;
Metadata.setParamTypes(target, propertyKey, paramTypes);
}
break;
case "property":
Store.from(target).merge("injectableProperties", {
[propertyKey]: {
bindingType,
propertyKey,
useType: symbol || Metadata.getType(target, propertyKey)
}
});
break;
case "method":
Store.from(target).merge("injectableProperties", {
[propertyKey]: {
bindingType,
propertyKey
}
});
return descriptor;
default:
throw new UnsupportedDecoratorType(Inject, [target, propertyKey, descriptor]);
}
};
}