0
0
NestJSframework~10 mins

Response transformation in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Response transformation
Incoming HTTP Request
Controller Method Called
Return Raw Data Object
Interceptor Intercepts Response
Transform Data (e.g., format, filter)
Send Transformed Response to Client
The request hits the controller, which returns data. The interceptor catches this data, transforms it, then sends the modified response to the client.
Execution Sample
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { map } from 'rxjs/operators';

@Injectable()
export class TransformInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler) {
    return next.handle().pipe(map(data => ({ data })));
  }
}
This interceptor wraps the original response data inside an object with a 'data' key.
Execution Table
StepActionInput DataTransformationOutput Data
1Controller returns raw data{ id: 1, name: 'Alice' }None yet{ id: 1, name: 'Alice' }
2Interceptor intercepts response{ id: 1, name: 'Alice' }Wrap in { data: ... }{ data: { id: 1, name: 'Alice' } }
3Send response to client{ data: { id: 1, name: 'Alice' } }None{ data: { id: 1, name: 'Alice' } }
💡 Response sent after transformation wrapping original data in a 'data' object.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
responseDataundefined{ id: 1, name: 'Alice' }{ data: { id: 1, name: 'Alice' } }{ data: { id: 1, name: 'Alice' } }
Key Moments - 2 Insights
Why does the interceptor wrap the data inside another object?
Because the interceptor modifies the response by wrapping it in a 'data' key as shown in step 2 of the execution_table, so the client always receives a consistent response shape.
Does the controller need to know about the transformation?
No, the controller just returns raw data. The interceptor handles transformation separately, as seen in step 1 vs step 2 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output data after step 2?
A{ data: { id: 1, name: 'Alice' } }
B{ id: 1, name: 'Alice' }
Cundefined
Dnull
💡 Hint
Check the 'Output Data' column in row for step 2 in execution_table.
At which step does the response get transformed?
AStep 1
BStep 2
CStep 3
DNo transformation occurs
💡 Hint
Look at the 'Transformation' column in execution_table rows.
If the interceptor did not wrap data, what would the final response be?
A{ data: { id: 1, name: 'Alice' } }
Bnull
C{ id: 1, name: 'Alice' }
DAn error
💡 Hint
Compare 'Output Data' in step 1 and step 2 in execution_table.
Concept Snapshot
NestJS Response Transformation:
- Use an interceptor to catch controller responses.
- Modify or wrap data inside intercept() method.
- Return transformed data via next.handle().pipe(map(...)).
- Keeps controller code clean and consistent response format.
- Useful for adding metadata or filtering sensitive info.
Full Transcript
In NestJS, response transformation happens by using an interceptor. When a request comes in, the controller returns raw data. The interceptor catches this data before it goes to the client. It then changes the data, for example by wrapping it inside an object with a 'data' key. Finally, the transformed response is sent to the client. This keeps the controller simple and lets you control response format in one place.