0
0
NestJSframework~20 mins

Response transformation in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Transformer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this interceptor?

Given the following NestJS interceptor that transforms the response by adding a status field, what will the client receive?

NestJS
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' }
A{"data":{"message":"Hello"}}
B{"message":"Hello","status":"success"}
C{"status":"success","data":{"message":"Hello"}}
D{"status":"success"}
Attempts:
2 left
💡 Hint

Think about how the map operator wraps the original data.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies a global response interceptor in NestJS?

Choose the correct way to apply a global interceptor that transforms all responses.

NestJS
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();
Aapp.useGlobalInterceptors(new TransformInterceptor());
Bapp.useGlobalInterceptor(new TransformInterceptor());
Capp.useGlobalFilters(new TransformInterceptor());
Dapp.useInterceptor(new TransformInterceptor());
Attempts:
2 left
💡 Hint

Check the exact method name for applying global interceptors in NestJS.

🔧 Debug
advanced
2:00remaining
Why does this interceptor not transform the response?

Consider this interceptor code. Why does the client receive the original response without transformation?

NestJS
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 }))
    );
  }
}
AThe interceptor should use tap instead of map to transform data.
BThe interceptor does not return the observable from next.handle(), so the pipeline is broken.
CThe map operator is not imported, so it causes a runtime error.
DThe interceptor must call next.handle() inside a try-catch block.
Attempts:
2 left
💡 Hint

Check if the observable is returned properly.

state_output
advanced
2:00remaining
What is the final response after applying this interceptor and controller?

Given this controller and interceptor, what will the client receive?

NestJS
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' };
  }
}
A{"modified":true,"message":"Hi"}
B{"message":"Hi","modified":true}
C{"message":"Hi"}
D{"modified":true,"data":{"message":"Hi"}}
Attempts:
2 left
💡 Hint

Remember how the spread operator works inside the map function.

🧠 Conceptual
expert
2:00remaining
Which statement about NestJS response transformation is TRUE?

Choose the correct statement about how response transformation works in NestJS.

AResponse transformation is only possible using middleware before the request reaches the controller.
BResponse transformation requires modifying the controller method's return type directly.
CGlobal filters are the recommended way to transform all responses in NestJS.
DInterceptors can modify the response stream by transforming the observable returned by the handler.
Attempts:
2 left
💡 Hint

Think about the role of interceptors versus filters and middleware.