What if you could fix all your API response issues with one simple setup?
Why Response transformation in NestJS? - Purpose & Use Cases
Imagine building an API that returns raw database data directly to users without any changes.
Every time you want to hide sensitive info or change the format, you must manually edit each response.
Manually changing every response is slow and easy to forget.
This leads to inconsistent data, leaks of private info, and lots of repeated code.
Response transformation lets you define rules once to automatically change all outgoing data.
This keeps your API clean, safe, and consistent without extra work each time.
return { id: user.id, password: user.password, email: user.email };@UseInterceptors(ClassSerializerInterceptor)
return user;You can easily control and customize all API responses globally or per route, improving security and user experience.
A social app hides users' passwords and internal IDs from API responses automatically, showing only public info like username and avatar.
Manual response editing is error-prone and repetitive.
Response transformation automates and centralizes output formatting.
This improves security, consistency, and developer productivity.
Practice
Solution
Step 1: Understand response transformation concept
Response transformation means changing the data before it is sent to the user.Step 2: Identify the correct purpose in NestJS context
In NestJS, response transformation is used to modify or format the response data.Final Answer:
To modify data before sending it to the client -> Option AQuick Check:
Response transformation = modify data before sending [OK]
- Confusing response transformation with authentication
- Thinking it manages database connections
- Assuming it logs errors
Solution
Step 1: Recall NestJS interceptor usage
Interceptors are applied using the @UseInterceptors decorator on controllers or methods.Step 2: Match correct syntax
The correct syntax is @UseInterceptors(new InterceptorClass()).Final Answer:
@UseInterceptors(new TransformInterceptor()) -> Option BQuick Check:
Use @UseInterceptors with new instance [OK]
- Using @Injectable instead of @UseInterceptors
- Calling interceptor as a method on controller
- Trying to use app.use for interceptors
intercept(context, next) {
return next.handle().pipe(map(data => ({ result: data })));
}Solution
Step 1: Understand the interceptor's map function
The map operator wraps the original data inside an object with key 'result'.Step 2: Predict the transformed output
The response will be an object: { result: originalData }.Final Answer:
{ result: originalData } -> Option CQuick Check:
map wraps data in { result: ... } [OK]
- Assuming original data is unchanged
- Confusing key name as 'data' instead of 'result'
- Expecting an error from this code
intercept(context, next) {
next.handle().pipe(map(data => data.trim()));
}Solution
Step 1: Check interceptor return value
Interceptors must return the observable from next.handle().pipe(...).Step 2: Identify missing return
The code calls next.handle().pipe(...) but does not return it, so the response is undefined.Final Answer:
Missing return statement for the observable -> Option DQuick Check:
Always return next.handle().pipe(...) [OK]
- Forgetting to return the observable
- Assuming map cannot be used in intercept
- Misusing trim() on non-string data
Solution
Step 1: Understand response transformation goal
We want to remove sensitive info from outgoing responses, not database or requests.Step 2: Choose best NestJS method
Interceptors can transform responses globally, ideal for removing password fields before sending.Step 3: Evaluate other options
Modifying DB schema or middleware won't affect response data; setting password null in controllers is repetitive.Final Answer:
Create an interceptor that maps the response and deletes the password field -> Option AQuick Check:
Use interceptor to hide sensitive response data [OK]
- Trying to filter password in middleware (affects requests)
- Changing database schema instead of response
- Manually nulling password in every controller
Ready to prove it?
Pass the test and this pattern moves unknown
