Complete the code to apply a response transformation using an interceptor.
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { map } from 'rxjs/operators'; import { Observable } from 'rxjs'; @Injectable() export class TransformInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe([1](data => ({ data }))); } }
The map operator transforms the response data before sending it to the client.
Complete the code to apply the TransformInterceptor globally in the main bootstrap function.
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { TransformInterceptor } from './transform.interceptor'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.useGlobalInterceptors(new [1]()); await app.listen(3000); } bootstrap();
new.To apply response transformation globally, you create an instance of TransformInterceptor and pass it to useGlobalInterceptors.
Fix the error in the interceptor to correctly transform the response by wrapping it in an object with a 'result' key.
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { map } from 'rxjs/operators'; import { Observable } from 'rxjs'; @Injectable() export class ResponseInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe([1](data => ({ result: data }))); } }
tap which does not change the data.filter which filters values instead of transforming.The map operator is used to transform the response data by wrapping it inside an object with a result key.
Fill both blanks to create a custom interceptor that adds a timestamp to the response data.
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { map } from 'rxjs/operators'; import { Observable } from 'rxjs'; @Injectable() export class TimestampInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe( [1](data => ({ ...data, [2]: Date.now() })) ); } }
map for transformation.The map operator transforms the response by adding a timestamp property with the current time.
Fill all three blanks to create an interceptor that transforms the response by converting the username to uppercase and adding a status.
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { map } from 'rxjs/operators'; import { Observable } from 'rxjs'; @Injectable() export class UserTransformInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe( [1](user => ({ username: user.[2](), status: '[3]' })) ); } }
toLowerCase instead of toUpperCase.The map operator transforms the user object by converting the username to uppercase using toUpperCase() and adding a status of 'active'.