File tree Expand file tree Collapse file tree 3 files changed +39
-3
lines changed Expand file tree Collapse file tree 3 files changed +39
-3
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " typedi" ,
3
- "version" : " 0.5.1 " ,
3
+ "version" : " 0.5.2 " ,
4
4
"description" : " Dependency injection for TypeScript" ,
5
5
"license" : " MIT" ,
6
6
"readmeFilename" : " README.md" ,
Original file line number Diff line number Diff line change 1
1
import { Container } from "../Container" ;
2
+ import { Token } from "../Token" ;
2
3
3
4
/**
4
5
* Injects a service into a class property or constructor parameter.
@@ -13,7 +14,12 @@ export function Inject(serviceName?: string): Function;
13
14
/**
14
15
* Injects a service into a class property or constructor parameter.
15
16
*/
16
- export function Inject ( typeOrName ?: ( ( type ?: any ) => Function ) | string ) : Function {
17
+ export function Inject ( token : Token < any > ) : Function ;
18
+
19
+ /**
20
+ * Injects a service into a class property or constructor parameter.
21
+ */
22
+ export function Inject ( typeOrName ?: ( ( type ?: any ) => Function ) | string | Token < any > ) : Function {
17
23
return function ( target : Object , propertyName : string , index ?: number ) {
18
24
19
25
if ( ! typeOrName )
@@ -23,7 +29,19 @@ export function Inject(typeOrName?: ((type?: any) => Function)|string): Function
23
29
object : target ,
24
30
propertyName : propertyName ,
25
31
index : index ,
26
- value : ( ) => Container . get < any > ( typeof typeOrName === "string" ? typeOrName : typeOrName ( ) as any )
32
+ value : ( ) => {
33
+ let identifier : any ;
34
+ if ( typeof typeOrName === "string" ) {
35
+ identifier = typeOrName ;
36
+
37
+ } else if ( typeOrName instanceof Token ) {
38
+ identifier = typeOrName ;
39
+
40
+ } else {
41
+ identifier = typeOrName ( ) ;
42
+ }
43
+ return Container . get < any > ( identifier ) ;
44
+ }
27
45
} ) ;
28
46
} ;
29
47
}
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import "reflect-metadata";
2
2
import { Container } from "../../src/Container" ;
3
3
import { Service } from "../../src/decorators/Service" ;
4
4
import { Inject } from "../../src/decorators/Inject" ;
5
+ import { Token } from "../../src/Token" ;
5
6
6
7
describe ( "Inject Decorator" , function ( ) {
7
8
@@ -19,6 +20,23 @@ describe("Inject Decorator", function() {
19
20
Container . get ( SecondTestService ) . testService . should . be . instanceOf ( TestService ) ;
20
21
} ) ;
21
22
23
+ it ( "should inject token service properly" , function ( ) {
24
+ interface Test {
25
+
26
+ }
27
+ const ServiceToken = new Token < Test > ( ) ;
28
+
29
+ @Service ( ServiceToken )
30
+ class TestService {
31
+ }
32
+ @Service ( )
33
+ class SecondTestService {
34
+ @Inject ( ServiceToken )
35
+ testService : Test ;
36
+ }
37
+ Container . get ( SecondTestService ) . testService . should . be . instanceOf ( TestService ) ;
38
+ } ) ;
39
+
22
40
it ( "should inject named service into class property" , function ( ) {
23
41
@Service ( "mega.service" )
24
42
class NamedService {
You can’t perform that action at this time.
0 commit comments