Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass configs from config service to Nest.js decorator?

Tags:

node.js

nestjs

I use @UseInterceptors with FilesInterceptor from @nestjs/platform-express. FilesInterceptor allows me to pass some configs (like files storage folder). I would like to pass not exact values (e.g. 'public') but to get them from configService.

From this answer (https://stackoverflow.com/a/59828094/13128344) I understood why calling this.configService from UseInterceptors results in undefined.

@UseInterceptors(FilesInterceptor('cat_pic', 2, this.configService.get('storageConfigs')))

So how can I pass configs to FilesInterceptor? Or how to access a service if I move file interceptor function to a separate module?

like image 624
Tatsiana Slabodchykava Avatar asked Aug 12 '20 11:08

Tatsiana Slabodchykava


Video Answer


1 Answers

One option you have is to call config() from the dotenv package to load everything into process.env in your main.ts. You can still use the ConfigModule and ConfigService everywhere else, but in decorators you would use that process.env.storageConfigs variable.

Another option would be a package like @golevelup/profiguration which is a configuration service that can be used in decorators because of the way it was written.

like image 161
Jay McDoniel Avatar answered Sep 25 '22 10:09

Jay McDoniel