|
1 | 1 | import 'reflect-metadata'; |
2 | 2 |
|
3 | | -import {FactoryMethod} from './definition'; |
| 3 | +import { FactoryMethod } from './definition'; |
4 | 4 |
|
5 | 5 | /** |
6 | 6 | * Decorator for class constructor injection |
7 | 7 | */ |
8 | | -export function ConstructorInject(targetOrFactoryMethodOrLiteral: any, factoryMethod?: FactoryMethod) { |
9 | | - let method: FactoryMethod = undefined; |
| 8 | +export function ConstructorInject(targetOrFactoryMethodOrLiteral: any, factoryMethod?: FactoryMethod) { |
| 9 | + let method: FactoryMethod | undefined = undefined; |
10 | 10 |
|
11 | | - switch (typeof targetOrFactoryMethodOrLiteral) { |
12 | | - // Simple @ConstructorInject at the top of class |
13 | | - case 'function': |
14 | | - Reflect.defineMetadata('inject:constructor', true, targetOrFactoryMethodOrLiteral); |
15 | | - return targetOrFactoryMethodOrLiteral; |
16 | | - // constructor (@ConstructorInject(FactoryMethod) param1: class, ...); |
17 | | - case 'number': |
18 | | - if (typeof targetOrFactoryMethodOrLiteral === 'number') { |
19 | | - method = targetOrFactoryMethodOrLiteral; |
20 | | - } |
21 | | - return function (target: Object, propertyKey: string|symbol, parameterIndex: number) { |
22 | | - // inject:constructor:param1:method |
23 | | - let metadataName = 'inject:constructor:param' + parameterIndex + ':method'; |
24 | | - Reflect.defineMetadata(metadataName, method, target); |
25 | | - }; |
26 | | - // constructor (@ConstructorInject('literal',FactoryMethod) param1: class, ...); |
27 | | - case 'string': |
28 | | - let literal: string = targetOrFactoryMethodOrLiteral; |
29 | | - method = factoryMethod; |
30 | | - return function (target:Object, propertyKey:string|symbol, parameterIndex:number) { |
31 | | - let metadataLiteralName = 'inject:constructor:param' + parameterIndex + ':literal'; |
32 | | - let metadataFactoryName = 'inject:constructor:param' + parameterIndex + ':method'; |
| 11 | + switch (typeof targetOrFactoryMethodOrLiteral) { |
| 12 | + // Simple @ConstructorInject at the top of class |
| 13 | + case 'function': |
| 14 | + Reflect.defineMetadata('inject:constructor', true, targetOrFactoryMethodOrLiteral); |
| 15 | + return targetOrFactoryMethodOrLiteral; |
| 16 | + // constructor (@ConstructorInject(FactoryMethod) param1: class, ...); |
| 17 | + case 'number': |
| 18 | + if (typeof targetOrFactoryMethodOrLiteral === 'number') { |
| 19 | + method = targetOrFactoryMethodOrLiteral; |
| 20 | + } |
| 21 | + return function (target: Object, _propertyKey: string | symbol, parameterIndex: number) { |
| 22 | + // inject:constructor:param1:method |
| 23 | + let metadataName = 'inject:constructor:param' + parameterIndex + ':method'; |
| 24 | + Reflect.defineMetadata(metadataName, method, target); |
| 25 | + }; |
| 26 | + // constructor (@ConstructorInject('literal',FactoryMethod) param1: class, ...); |
| 27 | + case 'string': |
| 28 | + let literal: string = targetOrFactoryMethodOrLiteral; |
| 29 | + method = factoryMethod; |
| 30 | + return function (target: Object, _propertyKey: string | symbol, parameterIndex: number) { |
| 31 | + let metadataLiteralName = 'inject:constructor:param' + parameterIndex + ':literal'; |
| 32 | + let metadataFactoryName = 'inject:constructor:param' + parameterIndex + ':method'; |
33 | 33 |
|
34 | | - Reflect.defineMetadata(metadataLiteralName, literal, target); |
35 | | - Reflect.defineMetadata(metadataFactoryName, method, target); |
36 | | - }; |
37 | | - } |
| 34 | + Reflect.defineMetadata(metadataLiteralName, literal, target); |
| 35 | + Reflect.defineMetadata(metadataFactoryName, method, target); |
| 36 | + }; |
| 37 | + } |
38 | 38 | } |
39 | 39 |
|
40 | 40 | /** |
41 | 41 | * Decorator for class property injection |
42 | 42 | */ |
43 | | -export function Inject(targetOrFactoryMethodOrLiteral: any, propertyKeyOrFactoryMethod?: string|symbol|FactoryMethod): any { |
44 | | - // means use default value or take from definition |
45 | | - let method: FactoryMethod = undefined; |
46 | | - // Because @Inject() and @Inject will be different |
| 43 | +export function Inject(targetOrFactoryMethodOrLiteral: any, propertyKeyOrFactoryMethod?: string | symbol | FactoryMethod): any { |
| 44 | + // means use default value or take from definition |
| 45 | + let method: FactoryMethod | undefined = undefined; |
| 46 | + // Because @Inject() and @Inject will be different |
47 | 47 |
|
48 | | - switch (typeof targetOrFactoryMethodOrLiteral) { |
49 | | - // That's simple @Inject |
50 | | - case 'object': |
51 | | - if (typeof propertyKeyOrFactoryMethod === 'string') { |
52 | | - let propertyKey: string = propertyKeyOrFactoryMethod; |
53 | | - if (!targetOrFactoryMethodOrLiteral.hasOwnProperty(propertyKey)) { |
54 | | - // TS compiler do not output empty properties, so explicitly define it here |
55 | | - Object.defineProperty(targetOrFactoryMethodOrLiteral, propertyKey, { |
56 | | - configurable: true, |
57 | | - enumerable: true, |
58 | | - writable: true, |
59 | | - value: null |
60 | | - }); |
61 | | - } |
62 | | - Reflect.defineMetadata("inject:property", method, targetOrFactoryMethodOrLiteral, propertyKey); |
63 | | - } |
64 | | - break; |
65 | | - // @Inject('literal') |
66 | | - case 'string': |
67 | | - let literal: string = targetOrFactoryMethodOrLiteral; |
68 | | - if (typeof propertyKeyOrFactoryMethod === "number") { |
69 | | - method = propertyKeyOrFactoryMethod; |
70 | | - } |
71 | | - return function (target:Object, propertyKey:string) { |
72 | | - if (!target.hasOwnProperty(propertyKey)) { |
73 | | - Object.defineProperty(target, propertyKey, { |
74 | | - configurable: true, |
75 | | - enumerable: true, |
76 | | - writable: true, |
77 | | - value: null |
78 | | - }); |
79 | | - } |
80 | | - Reflect.defineMetadata("inject:property", method, target, propertyKey); |
81 | | - Reflect.defineMetadata('inject:property:literal', literal, target, propertyKey); |
82 | | - }; |
83 | | - // @Inject(FactoryMethod) |
84 | | - case 'number': |
85 | | - method = targetOrFactoryMethodOrLiteral; |
86 | | - return function (target: Object, propertyKey: string) { |
87 | | - if (!target.hasOwnProperty(propertyKey)) { |
88 | | - Object.defineProperty(target, propertyKey, { |
89 | | - configurable: true, |
90 | | - enumerable: true, |
91 | | - writable: true, |
92 | | - value: null |
93 | | - }); |
94 | | - } |
95 | | - Reflect.defineMetadata("inject:property", method, target, propertyKey); |
96 | | - }; |
97 | | - } |
| 48 | + switch (typeof targetOrFactoryMethodOrLiteral) { |
| 49 | + // That's simple @Inject |
| 50 | + case 'object': |
| 51 | + if (typeof propertyKeyOrFactoryMethod === 'string') { |
| 52 | + let propertyKey: string = propertyKeyOrFactoryMethod; |
| 53 | + if (!targetOrFactoryMethodOrLiteral.hasOwnProperty(propertyKey)) { |
| 54 | + // TS compiler do not output empty properties, so explicitly define it here |
| 55 | + Object.defineProperty(targetOrFactoryMethodOrLiteral, propertyKey, { |
| 56 | + configurable: true, |
| 57 | + enumerable: true, |
| 58 | + writable: true, |
| 59 | + value: null |
| 60 | + }); |
| 61 | + } |
| 62 | + Reflect.defineMetadata("inject:property", method, targetOrFactoryMethodOrLiteral, propertyKey); |
| 63 | + } |
| 64 | + break; |
| 65 | + // @Inject('literal') |
| 66 | + case 'string': |
| 67 | + let literal: string = targetOrFactoryMethodOrLiteral; |
| 68 | + if (typeof propertyKeyOrFactoryMethod === "number") { |
| 69 | + method = propertyKeyOrFactoryMethod; |
| 70 | + } |
| 71 | + return function (target: Object, propertyKey: string) { |
| 72 | + if (!target.hasOwnProperty(propertyKey)) { |
| 73 | + Object.defineProperty(target, propertyKey, { |
| 74 | + configurable: true, |
| 75 | + enumerable: true, |
| 76 | + writable: true, |
| 77 | + value: null |
| 78 | + }); |
| 79 | + } |
| 80 | + Reflect.defineMetadata("inject:property", method, target, propertyKey); |
| 81 | + Reflect.defineMetadata('inject:property:literal', literal, target, propertyKey); |
| 82 | + }; |
| 83 | + // @Inject(FactoryMethod) |
| 84 | + case 'number': |
| 85 | + method = targetOrFactoryMethodOrLiteral; |
| 86 | + return function (target: Object, propertyKey: string) { |
| 87 | + if (!target.hasOwnProperty(propertyKey)) { |
| 88 | + Object.defineProperty(target, propertyKey, { |
| 89 | + configurable: true, |
| 90 | + enumerable: true, |
| 91 | + writable: true, |
| 92 | + value: null |
| 93 | + }); |
| 94 | + } |
| 95 | + Reflect.defineMetadata("inject:property", method, target, propertyKey); |
| 96 | + }; |
| 97 | + } |
98 | 98 | } |
99 | 99 |
|
100 | 100 | /** |
101 | 101 | * Specifies optional resolution (Don't throw error if not found) |
102 | 102 | * @param args |
103 | 103 | * @constructor |
104 | 104 | */ |
105 | | -export function Optional(...args: any[]): any { |
106 | | - return function (target: Object, propertyKey: string, parameterIndex: number) { |
107 | | - if (typeof parameterIndex === "undefined") { |
108 | | - // Property @Optional() |
109 | | - Reflect.defineMetadata("inject:property:optional", true, target, propertyKey); |
110 | | - } else { |
111 | | - // Constructor @Optional() |
112 | | - let metadataName = 'inject:constructor:param' + parameterIndex + ':optional'; |
113 | | - Reflect.defineMetadata(metadataName, true, target); |
114 | | - } |
| 105 | +export function Optional(..._args: any[]): any { |
| 106 | + return function (target: Object, propertyKey: string, parameterIndex: number) { |
| 107 | + if (typeof parameterIndex === "undefined") { |
| 108 | + // Property @Optional() |
| 109 | + Reflect.defineMetadata("inject:property:optional", true, target, propertyKey); |
| 110 | + } else { |
| 111 | + // Constructor @Optional() |
| 112 | + let metadataName = 'inject:constructor:param' + parameterIndex + ':optional'; |
| 113 | + Reflect.defineMetadata(metadataName, true, target); |
115 | 114 | } |
| 115 | + } |
116 | 116 | } |
117 | 117 |
|
118 | 118 | /** |
119 | 119 | * Decorator for creating auto-factories |
120 | 120 | */ |
121 | 121 | export function Factory(...args: any[]): any { |
122 | | - let target; |
| 122 | + let target; |
123 | 123 |
|
124 | | - switch (args.length) { |
125 | | - case 1: |
126 | | - target = args[0]; |
127 | | - Reflect.defineMetadata('inject:autofactory', true, target); |
128 | | - return target; |
129 | | - case 3: |
130 | | - target = args[0]; |
131 | | - let propertyKey = args[1]; |
132 | | - Reflect.defineMetadata('inject:factorymethod', true, target, propertyKey); |
133 | | - return args[2]; |
134 | | - default: |
135 | | - throw new Error("@Factory decorator is not allowed here"); |
136 | | - } |
| 124 | + switch (args.length) { |
| 125 | + case 1: |
| 126 | + target = args[0]; |
| 127 | + Reflect.defineMetadata('inject:autofactory', true, target); |
| 128 | + return target; |
| 129 | + case 3: |
| 130 | + target = args[0]; |
| 131 | + let propertyKey = args[1]; |
| 132 | + Reflect.defineMetadata('inject:factorymethod', true, target, propertyKey); |
| 133 | + return args[2]; |
| 134 | + default: |
| 135 | + throw new Error("@Factory decorator is not allowed here"); |
| 136 | + } |
137 | 137 | } |
0 commit comments