Skip to content

Commit ba7a312

Browse files
fix(core): add missing capability to control global dynamic modules
1 parent 915621c commit ba7a312

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

packages/common/interfaces/modules/dynamic-module.interface.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ModuleMetadata } from './module-metadata.interface';
21
import { Type } from '../type.interface';
2+
import { ModuleMetadata } from './module-metadata.interface';
33

44
/**
55
* Interface defining a Dynamic Module.
@@ -10,7 +10,18 @@ import { Type } from '../type.interface';
1010
*/
1111
export interface DynamicModule extends ModuleMetadata {
1212
/**
13-
* A module
13+
* A module reference
1414
*/
1515
module: Type<any>;
16+
17+
/**
18+
* When "true", makes a module global-scoped.
19+
*
20+
* Once imported into any module, a global-scoped module will be visible
21+
* in all modules. Thereafter, modules that wish to inject a service exported
22+
* from a global module do not need to import the provider module.
23+
*
24+
* Default: false
25+
*/
26+
isGlobal?: boolean;
1627
}

packages/core/injector/container.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ export class NestContainer {
6565
}
6666
const module = new Module(type, scope, this);
6767
this.modules.set(token, module);
68-
6968
this.addDynamicMetadata(token, dynamicMetadata, [].concat(scope, type));
70-
this.isGlobalModule(type) && this.addGlobalModule(module);
7169

70+
if (this.isGlobalModule(type, dynamicMetadata)) {
71+
this.addGlobalModule(module);
72+
}
7273
return module;
7374
}
7475

@@ -93,7 +94,13 @@ export class NestContainer {
9394
modules.forEach(module => this.addModule(module, scope));
9495
}
9596

96-
public isGlobalModule(metatype: Type<any>): boolean {
97+
public isGlobalModule(
98+
metatype: Type<any>,
99+
dynamicMetadata?: Partial<DynamicModule>,
100+
): boolean {
101+
if (dynamicMetadata && dynamicMetadata.isGlobal) {
102+
return true;
103+
}
97104
return !!Reflect.getMetadata(GLOBAL_MODULE_METADATA, metatype);
98105
}
99106

packages/core/test/injector/container.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,20 @@ describe('NestContainer', () => {
8383
});
8484
});
8585
describe('isGlobalModule', () => {
86-
describe('when module is not global scoped', () => {
86+
describe('when module is not globally scoped', () => {
8787
it('should return false', () => {
88-
expect(container.isGlobalModule(TestModule as any)).to.be.false;
88+
expect(container.isGlobalModule(TestModule)).to.be.false;
8989
});
9090
});
91-
describe('when module is not global scoped', () => {
91+
describe('when module is globally scoped', () => {
9292
it('should return true', () => {
93-
expect(container.isGlobalModule(GlobalTestModule as any)).to.be.true;
93+
expect(container.isGlobalModule(GlobalTestModule)).to.be.true;
94+
});
95+
});
96+
describe('when dynamic module is globally scoped', () => {
97+
it('should return true', () => {
98+
expect(container.isGlobalModule(TestModule, { isGlobal: true })).to.be
99+
.true;
94100
});
95101
});
96102
});

0 commit comments

Comments
 (0)