#Using ConfigService to configure NestApplication

2 messages · Page 1 of 1 (latest)

cerulean crest
#

I'm assuming this isn't possible to do exactly as the title implies, but wondering if there is another way. I have this code snippet in my main.ts:

const app = await NestFactory.create<NestFastifyApplication>(
  AppModule,
  new FastifyAdapter({ bodyLimit: process.env.BODY_LIMIT ? parseInt(process.env.BODY_LIMIT) : 1000000000 }),
  {
    logger: WinstonModule.createLogger({ instance: winstonLogger }),
  },
);

I am moving my application to use the @nestjs/config package and using the ConfigService.

My question is, is it possible to use the ConfigService to replace the bodyLimit configuration in the above code snippet?

Something like this:

const configService = someWayToGetTheConfigServiceThatIsRegistered(); // I assume not possible because nothing is registered at this point.

const app = await NestFactory.create<NestFastifyApplication>(
  AppModule,
  new FastifyAdapter({ bodyLimit: configService.get<number>('BODY_LIMIT', 1000000000) },
  {
    logger: WinstonModule.createLogger({ instance: winstonLogger }),
  },
);

Again, as I said before and in the code snippet, I don't expect that to be possible. What I'm thinking might be possible is:

const app = await NestFactory.create<NestFastifyApplication>(AppModule);

const configService = app.get(ConfigService);
const bodyLimit = configService.get<number>('BODY_LIMIT', 1000000000);
const logger = app.get(Logger);

app.useHttpAdaptor(new FastifyAdapter({ 
  bodyLimit
});
app.useLogger(logger); // Though, I am realizing I probably don't need this, if I am registering the `Logger` from a module. But that isn't the real question.
worldly glacier