Skip to content

Commit ff13342

Browse files
committed
feat(): support for async configuration in load
1 parent ee9dabd commit ff13342

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

lib/config.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class ConfigModule {
5353
* Also, registers custom configurations globally.
5454
* @param options
5555
*/
56-
static forRoot(options: ConfigModuleOptions = {}): DynamicModule {
56+
static async forRoot(options: ConfigModuleOptions = {}): Promise<DynamicModule> {
5757
const envFilePaths = Array.isArray(options.envFilePath)
5858
? options.envFilePath
5959
: [options.envFilePath || resolve(process.cwd(), '.env')];
@@ -88,7 +88,8 @@ export class ConfigModule {
8888
}
8989

9090
const isConfigToLoad = options.load && options.load.length;
91-
const providers = (options.load || [])
91+
const configFactory = options.load ? await Promise.all(options.load.map((configFactory) => configFactory)) : [];
92+
const providers = configFactory
9293
.map(factory =>
9394
createConfigProvider(factory as ConfigFactory & ConfigFactoryKeyHost),
9495
)

lib/interfaces/config-module-options.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface ConfigModuleOptions {
5757
* Array of custom configuration files to be loaded.
5858
* See: https://docs.nestjs.com/techniques/configuration
5959
*/
60-
load?: Array<ConfigFactory>;
60+
load?: Array<ConfigFactory | Promise<ConfigFactory>>;
6161

6262
/**
6363
* A boolean value indicating the use of expanded variables, or object

tests/e2e/load-files-async.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { INestApplication } from '@nestjs/common';
2+
import { Test } from '@nestjs/testing';
3+
import { AppModule } from '../src/app.module';
4+
5+
describe('Async Files', () => {
6+
let app: INestApplication;
7+
8+
beforeEach(async () => {
9+
const module = await Test.createTestingModule({
10+
imports: [AppModule.withLoadedAsyncConfigurations()],
11+
}).compile();
12+
13+
app = module.createNestApplication();
14+
await app.init();
15+
});
16+
17+
it(`should return loaded configuration`, () => {
18+
const host = app.get(AppModule).getDatabaseHost();
19+
expect(host).toEqual('host');
20+
});
21+
22+
it(`should return loaded configuration (injected through constructor)`, () => {
23+
const config = app.get(AppModule).getDatabaseConfig();
24+
expect(config.host).toEqual('host');
25+
expect(config.port).toEqual(4000);
26+
});
27+
28+
afterEach(async () => {
29+
await app.close();
30+
});
31+
});

tests/src/app.module.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ export class AppModule {
140140
};
141141
}
142142

143+
static withLoadedAsyncConfigurations() {
144+
return {
145+
module: AppModule,
146+
imports: [
147+
ConfigModule.forRoot({
148+
load: [Promise.resolve(databaseConfig)],
149+
}),
150+
],
151+
};
152+
}
153+
143154
static withNestedLoadedConfigurations(): DynamicModule {
144155
return {
145156
module: AppModule,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy