8000 Merge branch 'lovesharma95-lovesharma95-feature/loadAsync' · nestjs/config@fdff9c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit fdff9c1

Browse files
Merge branch 'lovesharma95-lovesharma95-feature/loadAsync'
2 parents ebdb8c2 + 7ab5a27 commit fdff9c1

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

lib/config.module.ts

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

9092
const isConfigToLoad = options.load && options.load.length;
91-
const providers = (options.load || [])
93+
const configFactory = await Promise.all(options.load || []);
94+
const providers = configFactory
9295
.map(factory =>
9396
createConfigProvider(factory as ConfigFactory & ConfigFactoryKeyHost),
9497
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { DotenvExpandOptions } from 'dotenv-expand';
12
import { ConfigFactory } from './config-factory.interface';
2-
import { DotenvExpandOptions } from 'dotenv-expand'
33

44
/**
55
* @publicApi
@@ -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)
0