Given the following NestJS interceptor that transforms the response by adding a status field, what will the client receive?
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { map } from 'rxjs/operators'; import { Observable } from 'rxjs'; @Injectable() export class StatusInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe( map(data => ({ status: 'success', data })) ); } } // Controller method returns: { message: 'Hello' }
Think about how the map operator wraps the original data.
The interceptor uses map to wrap the original response inside an object with a status field and a data field containing the original response. So the client receives an object with status and data keys.
Choose the correct way to apply a global interceptor that transforms all responses.
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { TransformInterceptor } from './transform.interceptor'; async function bootstrap() { const app = await NestFactory.create(AppModule); // Apply global interceptor here await app.listen(3000); } bootstrap();
Check the exact method name for applying global interceptors in NestJS.
The correct method to apply a global interceptor is useGlobalInterceptors (note the plural). Other options are either misspelled or refer to filters, not interceptors.
Consider this interceptor code. Why does the client receive the original response without transformation?
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; @Injectable() export class NoTransformInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe( map(data => ({ transformed: true, data })) ); } }
Check if the observable is returned properly.
The interceptor must return the observable from next.handle().pipe(...). Here, the return statement is missing, so the transformation never happens and the original response is sent.
Given this controller and interceptor, what will the client receive?
import { Controller, Get, UseInterceptors } from '@nestjs/common'; import { map } from 'rxjs/operators'; @Controller('test') @UseInterceptors(class { intercept(context, next) { return next.handle().pipe(map(data => ({ modified: true, ...data }))); } }) export class TestController { @Get() getData() { return { message: 'Hi' }; } }
Remember how the spread operator works inside the map function.
The interceptor spreads the original data inside a new object with modified: true. So the final object has keys modified and message at the same level.
Choose the correct statement about how response transformation works in NestJS.
Think about the role of interceptors versus filters and middleware.
Interceptors wrap the handler's observable and can transform the response stream. Filters handle exceptions, and middleware runs before controllers but does not transform responses directly.