File tree Expand file tree Collapse file tree 4 files changed +49
-4
lines changed Expand file tree Collapse file tree 4 files changed +49
-4
lines changed Original file line number Diff line number Diff line change @@ -53,7 +53,9 @@ export class ConfigModule {
53
53
* Also, registers custom configurations globally.
54
54
* @param options
55
55
*/
56
- static forRoot ( options : ConfigModuleOptions = { } ) : DynamicModule {
56
+ static async forRoot (
57
+ options : ConfigModuleOptions = { } ,
58
+ ) : Promise < DynamicModule > {
57
59
const envFilePaths = Array . isArray ( options . envFilePath )
58
60
? options . envFilePath
59
61
: [ options . envFilePath || resolve ( process . cwd ( ) , '.env' ) ] ;
@@ -88,7 +90,8 @@ export class ConfigModule {
88
90
}
89
91
90
92
const isConfigToLoad = options . load && options . load . length ;
91
- const providers = ( options . load || [ ] )
93
+ const configFactory = await Promise . all ( options . load || [ ] ) ;
94
+ const providers = configFactory
92
95
. map ( factory =>
93
96
createConfigProvider ( factory as ConfigFactory & ConfigFactoryKeyHost ) ,
94
97
)
Original file line number Diff line number Diff line change
1
+ import { DotenvExpandOptions } from 'dotenv-expand' ;
1
2
import { ConfigFactory } from './config-factory.interface' ;
2
- import { DotenvExpandOptions } from 'dotenv-expand'
3
3
4
4
/**
5
5
* @publicApi
@@ -57,7 +57,7 @@ export interface ConfigModuleOptions {
57
57
* Array of custom configuration files to be loaded.
58
58
* See: https://docs.nestjs.com/techniques/configuration
59
59
*/
60
- load ?: Array < ConfigFactory > ;
60
+ load ?: Array < ConfigFactory | Promise < ConfigFactory > > ;
61
61
62
62
/**
63
63
* A boolean value indicating the use of expanded variables, or object
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -140,6 +140,17 @@ export class AppModule {
140
140
} ;
141
141
}
142
142
143
+ static withLoadedAsyncConfigurations ( ) {
144
+ return {
145
+ module : AppModule ,
146
+ imports : [
147
+ ConfigModule . forRoot ( {
148
+ load : [ Promise . resolve ( databaseConfig ) ] ,
149
+ } ) ,
150
+ ] ,
151
+ } ;
152
+ }
153
+
143
154
static withNestedLoadedConfigurations ( ) : DynamicModule {
144
155
return {
145
156
module : AppModule ,
You can’t perform that action at this time.
0 commit comments