Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9935 from random42/feature/configurable-module-in…
…ject-providers feat(common): pass options to nested async modules
- Loading branch information
Showing
5 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/common/module-utils/utils/get-injection-providers.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
InjectionToken, | ||
Provider, | ||
FactoryProvider, | ||
OptionalFactoryDependency, | ||
} from '../../interfaces'; | ||
|
||
/** | ||
* check if x is OptionalFactoryDependency, based on prototype presence | ||
* (to avoid classes with a static 'token' field) | ||
* @param x | ||
* @returns x is OptionalFactoryDependency | ||
*/ | ||
function isOptionalFactoryDependency( | ||
x: InjectionToken | OptionalFactoryDependency, | ||
): x is OptionalFactoryDependency { | ||
return !!((x as any)?.token && !(x as any)?.prototype); | ||
} | ||
|
||
const mapInjectToTokens = (t: InjectionToken | OptionalFactoryDependency) => | ||
isOptionalFactoryDependency(t) ? t.token : t; | ||
|
||
/** | ||
* | ||
* @param providers List of a module's providers | ||
* @param tokens Injection tokens needed for a useFactory function (usually the module's options' token) | ||
* @returns All the providers needed for the tokens' injection (searched recursively) | ||
*/ | ||
export function getInjectionProviders( | ||
providers: Provider[], | ||
tokens: FactoryProvider['inject'], | ||
): Provider[] { | ||
const result: Provider[] = []; | ||
let search: InjectionToken[] = tokens.map(mapInjectToTokens); | ||
while (search.length > 0) { | ||
const match = (providers ?? []).filter( | ||
p => | ||
!result.includes(p) && // this prevents circular loops and duplication | ||
(search.includes(p as any) || search.includes((p as any)?.provide)), | ||
); | ||
result.push(...match); | ||
// get injection tokens of the matched providers, if any | ||
search = match | ||
.filter(p => (p as any)?.inject) | ||
.map(p => (p as FactoryProvider).inject) | ||
.flat() | ||
.map(mapInjectToTokens); | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/common/test/module-utils/utils/get-injection-providers.util.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { expect } from 'chai'; | ||
import { Provider } from '../../../interfaces'; | ||
import { getInjectionProviders } from '../../../module-utils/utils/get-injection-providers.util'; | ||
|
||
describe('getInjectionProviders', () => { | ||
it('should take only required providers', () => { | ||
class C { | ||
static token = 'a'; | ||
} | ||
const p: Provider[] = [ | ||
{ | ||
provide: 'a', | ||
useValue: 'a', | ||
}, | ||
{ | ||
provide: 'b', | ||
useValue: 'b', | ||
}, | ||
C, | ||
{ | ||
provide: 'd', | ||
useFactory: (c, b) => [c, b], | ||
inject: [ | ||
C, | ||
{ | ||
token: 'b', | ||
optional: true, | ||
}, | ||
'x', | ||
], | ||
}, | ||
{ | ||
provide: 'e', | ||
useFactory: (d, b) => [d, b], | ||
inject: ['d', 'b'], | ||
}, | ||
{ | ||
provide: 'f', | ||
useValue: 'f', | ||
}, | ||
]; | ||
// should not include 'a' and 'f' | ||
const expected = p.slice(1, -1); | ||
const result = getInjectionProviders(p, ['e']); | ||
expect(result).to.have.length(expected.length); | ||
expect(result).to.have.members(expected); | ||
expect(result).not.to.have.members([p[0], p[5]]); | ||
}); | ||
}); |