1
- import { InjectableFactory } from '@asuka/di'
1
+ import { InjectableFactory , Inject } from '@asuka/di'
2
2
import { Request } from 'express'
3
3
4
4
import { ConstructorOf } from '../types'
@@ -25,16 +25,29 @@ export function createOrGetInstanceInScope<T>(constructor: ConstructorOf<T>, sco
25
25
}
26
26
27
27
function createInstanceInScope < T > ( constructor : ConstructorOf < T > , scope : Scope ) : T {
28
- const constructorParams : ConstructorOf < any > [ ] =
28
+ const paramTypes : ConstructorOf < any > [ ] =
29
29
Reflect . getMetadata ( 'design:paramtypes' , constructor ) || [ ]
30
30
31
+ // parameters decorated by @Inject
32
+ const paramAnnotations = Reflect . getMetadata ( 'parameters' , constructor ) || [ ]
33
+
31
34
const sameScopeParams : number [ ] = Reflect . getMetadata ( SameScopeMetadataKey , constructor ) || [ ]
32
- const deps = constructorParams . map ( ( paramConstructor , index ) => {
35
+
36
+ const deps = paramTypes . map ( ( type , index ) => {
33
37
if ( sameScopeParams [ index ] ) {
34
- return createOrGetInstanceInScope ( paramConstructor , scope )
35
- } else {
36
- return InjectableFactory . getInstance ( paramConstructor )
38
+ return createOrGetInstanceInScope ( type , scope )
37
39
}
40
+
41
+ if ( paramAnnotations [ index ] !== null ) {
42
+ let metadata : any [ ] = paramAnnotations [ index ]
43
+ metadata = Array . isArray ( metadata ) ? metadata : [ metadata ]
44
+ const inject : Inject | undefined = metadata . find ( ( factory ) => factory instanceof Inject )
45
+ if ( inject && inject . token ) {
46
+ return InjectableFactory . getInstanceByToken ( inject . token )
47
+ }
48
+ }
49
+
50
+ return InjectableFactory . getInstance ( type )
38
51
} )
39
52
40
53
const newInstance = new constructor ( ...deps )
0 commit comments