terminus icon indicating copy to clipboard operation
terminus copied to clipboard

Implement forRootAsync to allow dynamic configuration injection

Open drew-r opened this issue 9 months ago • 3 comments

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);

drew-r avatar Mar 17 '25 13:03 drew-r

is worth mention that forRootAsync exists in @nestjs/terminus v7. I don't know why it was removed

micalevisk avatar Mar 17 '25 14:03 micalevisk

Open for PRs for this!

BrunnerLivio avatar Mar 17 '25 18:03 BrunnerLivio

@micalevisk @BrunnerLivio I have created PR to resolve the issue.

mag123c avatar Aug 07 '25 12:08 mag123c