Skip to content

Commit

Permalink
feat(): support for async configuration in load
Browse files Browse the repository at this point in the history
  • Loading branch information
lovesharma95 committed Apr 7, 2024
1 parent ee9dabd commit ff13342
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ConfigModule {
* Also, registers custom configurations globally.
* @param options
*/
static forRoot(options: ConfigModuleOptions = {}): DynamicModule {
static async forRoot(options: ConfigModuleOptions = {}): Promise<DynamicModule> {
const envFilePaths = Array.isArray(options.envFilePath)
? options.envFilePath
: [options.envFilePath || resolve(process.cwd(), '.env')];
Expand Down Expand Up @@ -88,7 +88,8 @@ export class ConfigModule {
}

const isConfigToLoad = options.load && options.load.length;
const providers = (options.load || [])
const configFactory = options.load ? await Promise.all(options.load.map((configFactory) => configFactory)) : [];
const providers = configFactory
.map(factory =>
createConfigProvider(factory as ConfigFactory & ConfigFactoryKeyHost),
)
Expand Down
2 changes: 1 addition & 1 deletion lib/interfaces/config-module-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface ConfigModuleOptions {
* Array of custom configuration files to be loaded.
* See: https://docs.nestjs.com/techniques/configuration
*/
load?: Array<ConfigFactory>;
load?: Array<ConfigFactory | Promise<ConfigFactory>>;

/**
* A boolean value indicating the use of expanded variables, or object
Expand Down
31 changes: 31 additions & 0 deletions tests/e2e/load-files-async.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { AppModule } from '../src/app.module';

describe('Async Files', () => {
let app: INestApplication;

beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [AppModule.withLoadedAsyncConfigurations()],
}).compile();

app = module.createNestApplication();
await app.init();
});

it(`should return loaded configuration`, () => {
const host = app.get(AppModule).getDatabaseHost();
expect(host).toEqual('host');
});

it(`should return loaded configuration (injected through constructor)`, () => {
const config = app.get(AppModule).getDatabaseConfig();
expect(config.host).toEqual('host');
expect(config.port).toEqual(4000);
});

afterEach(async () => {
await app.close();
});
});
11 changes: 11 additions & 0 deletions tests/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ export class AppModule {
};
}

static withLoadedAsyncConfigurations() {
return {
module: AppModule,
imports: [
ConfigModule.forRoot({
load: [Promise.resolve(databaseConfig)],
}),
],
};
}

static withNestedLoadedConfigurations(): DynamicModule {
return {
module: AppModule,
Expand Down

0 comments on commit ff13342

Please sign in to comment.