Implement forRootAsync to allow dynamic configuration injection
Is there an existing issue that is already proposing this?
- [x] I have searched the existing issues
Is your feature request related to a problem? Please describe it
Currently, TerminusModule only supports forRoot(), which requires static configuration values at the time of module import. This makes it difficult to inject dynamic configurations, such as gracefulShutdownTimeoutMs, from ConfigService or other asynchronous providers.
For example, we want to configure gracefulShutdownTimeoutMs dynamically based on an environment variable managed by ConfigService:
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
ConfigModule,
TerminusModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
gracefulShutdownTimeoutMs: configService.get<number>('TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT', 16000),
}),
}),
],
})
export class HealthModule {}
Describe the solution you'd like
A forRootAsync impl that can set TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT + the other logging config values
Teachability, documentation, adoption, migration strategy
n/a
What is the motivation / use case for changing the behavior?
Having to work around like so
const terminusModule = TerminusModule.forRoot();
terminusModule.providers?.push({
provide: TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT,
useFactory: (configService: ConfigService) => configService.getOrThrow<number>('TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT'),
inject: [ConfigService]
});
terminusModule.providers?.push(GracefulShutdownService);
is worth mention that forRootAsync exists in @nestjs/terminus v7. I don't know why it was removed
Open for PRs for this!
@micalevisk @BrunnerLivio I have created PR to resolve the issue.