Skip to content

Commit 30d9e09

Browse files
feat(core): Make ConfigModule importable in every NgModule (#157)
* Updated config.module.ts Removed the check in the constructor, this way every module can import ConfigModule and have access to ConfigPipe. * Update README.md * Provide a forChild() method This change makes ConfigModule importable in lazy loaded modules with forChild() but also preserves the forRoot() check. * Updated README to reflect last commit * Update config.module.ts Removed the generic from InjectionToken * Update config.module.ts (tslint) Co-authored-by: Burak Tasci <burak.tasci@deliveryhero.com>
1 parent d9105fd commit 30d9e09

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

packages/@ngx-config/core/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ export class AnyClass {
211211
<span id="property">{{['some', 'setting'] | config}}</span>
212212
```
213213

214+
In order to use this pipe in lazy-loaded modules, you must import `ConfigModule.forChild()`.
215+
214216
## <a name="license"></a> License
215217

216218
The MIT License (MIT)

packages/@ngx-config/core/src/config.module.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { APP_INITIALIZER, ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';
1+
import { APP_INITIALIZER, Inject, InjectionToken, ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';
22

33
import { ConfigLoader, ConfigStaticLoader } from './config.loader';
44
import { ConfigPipe } from './config.pipe';
@@ -13,17 +13,30 @@ export const initializerFactory = (config: ConfigService) => {
1313
return res;
1414
};
1515

16+
export const CONFIG_FORROOT_GUARD = new InjectionToken('CONFIG_FORROOT_GUARD');
17+
18+
// tslint:disable-next-line:only-arrow-functions
19+
export function provideForRootGuard(config: ConfigService): any {
20+
if (config) {
21+
throw new Error(
22+
`ConfigModule.forRoot() called twice. Lazy loaded modules should use ConfigModule.forChild() instead.`);
23+
}
24+
25+
return 'guarded';
26+
}
27+
1628
@NgModule({
1729
declarations: [ConfigPipe],
1830
exports: [ConfigPipe]
1931
})
2032
export class ConfigModule {
33+
2134
static forRoot(
2235
configuredProvider: any = {
2336
provide: ConfigLoader,
2437
useFactory: configFactory
2538
}
26-
): ModuleWithProviders {
39+
): ModuleWithProviders<ConfigModule> {
2740
return {
2841
ngModule: ConfigModule,
2942
providers: [
@@ -34,14 +47,22 @@ export class ConfigModule {
3447
useFactory: initializerFactory,
3548
deps: [ConfigService],
3649
multi: true
50+
},
51+
{
52+
provide: CONFIG_FORROOT_GUARD,
53+
useFactory: provideForRootGuard,
54+
deps: [[ConfigService, new Optional(), new SkipSelf()]]
3755
}
3856
]
3957
};
4058
}
41-
42-
constructor(@Optional() @SkipSelf() parentModule?: ConfigModule) {
43-
if (parentModule) {
44-
throw new Error('ConfigModule already loaded; import in root module only.');
45-
}
59+
60+
static forChild(): ModuleWithProviders<ConfigModule> {
61+
return {
62+
ngModule: ConfigModule
63+
};
4664
}
65+
66+
// tslint:disable-next-line:no-empty
67+
constructor(@Optional() @Inject(CONFIG_FORROOT_GUARD) guard: any) {}
4768
}

0 commit comments

Comments
 (0)