Skip to content

Commit 685565c

Browse files
committed
feat(): support load asynchronous
added support for load custom configuration files asynchronously
1 parent ee9dabd commit 685565c

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

lib/config.module.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,12 @@ export class ConfigModule {
8787
this.assignVariablesToProcess(config);
8888
}
8989

90-
const isConfigToLoad = options.load && options.load.length;
91-
const providers = (options.load || [])
90+
let configFactory: ConfigFactory[] = [];
91+
if(options.loadAsync) options.loadAsync.then((configFact) => { configFactory = configFact })
92+
if(options.load) configFactory = options.load
93+
94+
const isConfigToLoad = configFactory && configFactory.length;
95+
const providers = configFactory
9296
.map(factory =>
9397
createConfigProvider(factory as ConfigFactory & ConfigFactoryKeyHost),
9498
)

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

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export interface ConfigModuleOptions {
5959
*/
6060
load?: Array<ConfigFactory>;
6161

62+
/**
63+
* Promise of array of custom configuration files to be loaded.
64+
* See: https://docs.nestjs.com/techniques/configuration
65+
*/
66+
loadAsync?: Promise<Array<ConfigFactory>>;
67+
6268
/**
6369
* A boolean value indicating the use of expanded variables, or object
6470
* containing options to pass to dotenv-expand.

tests/e2e/load-priority.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ describe('Environment variables and .env files', () => {
8686
});
8787
});
8888

89+
describe('withAsyncLoadedConfigurations', () => {
90+
it('should load configurations asynchronously', async () => {
91+
// Mock the async loading of configuration factories
92+
const configFactoryPromise = Promise.resolve([
93+
() => ({ PORT: '8000' }), // Simulate loading configuration factories asynchronously
94+
]);
95+
96+
// Create a testing module with AppModule configured to load configurations asynchronously
97+
const moduleRef = await Test.createTestingModule({
98+
imports: [AppModule.withAsyncLoadedConfigurations(configFactoryPromise)],
99+
}).compile();
100+
101+
// Create the Nest application
102+
app = moduleRef.createNestApplication();
103+
await app.init();
104+
105+
// Retrieve the ConfigService
106+
const configService = app.get(ConfigService);
107+
108+
// Assert that the configurations are loaded correctly
109+
expect(configService.get('PORT')).toEqual('8000');
110+
});
111+
});
112+
89113
describe('with conflicts of multiple loaded configurations', () => {
90114
beforeAll(async () => {
91115
const moduleRef = await Test.createTestingModule({

tests/src/app.module.ts

+11
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ export class AppModule {
140140
};
141141
}
142142

143+
static withAsyncLoadedConfigurations(configFactoryPromise: Promise<ConfigFactory[]>): DynamicModule {
144+
return {
145+
module: AppModule,
146+
imports: [
147+
ConfigModule.forRoot({
148+
loadAsync: configFactoryPromise,
149+
}),
150+
],
151+
};
152+
}
153+
143154
static withNestedLoadedConfigurations(): DynamicModule {
144155
return {
145156
module: AppModule,

0 commit comments

Comments
 (0)