2

Im working on a team that has a bunch of services so we have a npm package that contains code shared between the services.

We have a Health check module that sets the path to globalPrefix/health. Im attempting to make this value configurable in a maintainable way.

@Injectable()
@Controller()
export class HealthController {
  private readonly healthCheckOptions: HealthConfigurationOptions; 
  private readonly url: string; 

  constructor(
    @Inject('CONFIGURATION_OPTIONS') private options: HealthConfigurationOptions,
    private readonly healthService: HealthService,
    ) {
      this.healthCheckOptions = options || {}
      this.url = options.url
    }

  @Get(this.url)
  async healthHandler(): Promise<HealthDto | TmoHttpException> {
    return this.healthService.getStatus();
  }
}

My idea was to create a Dynamic Module that can take a path as an option. In the example above There is a Dynamic Health Module that accepts an options object. But it seems like during compilation the route handler is set before the class is constructed meaning that i cannot use this.url like @Get(this.url) because there is no this yet.

At this point Im a bit stumped and haven't found anything online doing what I need.

  • I guess you could create a function that will return that controller, pretty much like the [`FilesInterceptor`](https://docs.nestjs.com/techniques/file-upload#array-of-files): https://github.com/nestjs/nest/blob/d9d628d112ff992c8075bae44b826300fd5ca6c8/packages/platform-express/multer/interceptors/files.interceptor.ts#L19 – Micael Levi Apr 15 '22 at 23:22
  • Seems like this would be trivial for NestJs and that an Interceptor would be overkill. I'll be watching this for the solution. – navanjr Nov 08 '22 at 00:01
  • This looks promising: https://github.com/nestjs/nest/issues/1438#issuecomment-863446608 – navanjr Nov 08 '22 at 00:21

1 Answers1

0
Reflect.defineMetadata(PATH_METADATA, 'my_custom_path', MyController);

while registering your custom dynamic module will change the path of your controller. however there are still issues with this approach.

see here: https://github.com/nestjs/nest/issues/1438#issuecomment-1324011241

navanjr
  • 558
  • 3
  • 15