How to sanitize NestJS API responses with Interceptor

In any API development project, ensuring that sensitive or unnecessary data is not exposed in the responses is crucial for security and efficiency. In NestJS, interceptors provide a powerful way to manipulate the flow of data. In this blog post, we’ll explore how to use an interceptor to sanitize API responses by removing specific properties. We’ll also look at how to write tests to ensure the interceptor behaves as expected. ...

How to use ConfigService in NestJS in application bootstrap

You might want to access the configuration, for example to set the microservice configuration based on the values from the configuration service. One way to do this is to create an application context from the AppModule. const appContext = await NestFactory.createApplicationContext(BootstrapConfigModule) const configService = appContext.get(ConfigService) const SERVICE_PORT = configService.get('SERVICE_PORT') appContext.close() But a better idea is to use a “temporary” module to avoid double instantiation of the whole app. See a full example below. import { Module } from '@nestjs/common' import { ConfigService } from '@nestjs/config' import { NestFactory } from '@nestjs/core' import { MicroserviceOptions, Transport } from '@nestjs/microservices' import { AppModule } from './app.module' // Bootstrap configuration module is needed to avoid // doing the DI resolution twice @Module({ providers: [ConfigService], exports: [ConfigService], }) class BootstrapConfigModule {} async function bootstrap() { const appContext = await NestFactory.createApplicationContext(BootstrapConfigModule) const configService = appContext.get(ConfigService) const SERVICE_PORT = configService.get('SERVICE_PORT') appContext.close() const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, { transport: Transport.TCP, options: { port: SERVICE_PORT, }, }) await app.listen() } bootstrap() Why would you need to use the configuration service instead of process.env you might ask. Well, while that may work, it doesn’t offer any type safety or support for fallbacks. ...