1- import { InjectableFactory } from '@asuka/di'
1+ import { InjectableFactory , Inject } from '@asuka/di'
22import { Request } from 'express'
33
44import { ConstructorOf } from '../types'
@@ -25,16 +25,29 @@ export function createOrGetInstanceInScope<T>(constructor: ConstructorOf<T>, sco
2525}
2626
2727function createInstanceInScope < T > ( constructor : ConstructorOf < T > , scope : Scope ) : T {
28- const constructorParams : ConstructorOf < any > [ ] =
28+ const paramTypes : ConstructorOf < any > [ ] =
2929 Reflect . getMetadata ( 'design:paramtypes' , constructor ) || [ ]
3030
31+ // parameters decorated by @Inject
32+ const paramAnnotations = Reflect . getMetadata ( 'parameters' , constructor ) || [ ]
33+
3134 const sameScopeParams : number [ ] = Reflect . getMetadata ( SameScopeMetadataKey , constructor ) || [ ]
32- const deps = constructorParams . map ( ( paramConstructor , index ) => {
35+
36+ const deps = paramTypes . map ( ( type , index ) => {
3337 if ( sameScopeParams [ index ] ) {
34- return createOrGetInstanceInScope ( paramConstructor , scope )
35- } else {
36- return InjectableFactory . getInstance ( paramConstructor )
38+ return createOrGetInstanceInScope ( type , scope )
3739 }
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 )
3851 } )
3952
4053 const newInstance = new constructor ( ...deps )
0 commit comments