Created
April 12, 2023 12:38
-
-
Save micaiah-effiong/59447b634718cba3b92e65136b0594bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| CallHandler, | |
| ExecutionContext, | |
| Injectable, | |
| NestInterceptor, | |
| } from '@nestjs/common'; | |
| import { Response } from 'express'; | |
| import { map, Observable } from 'rxjs'; | |
| @Injectable() | |
| export class ResponseTransformerInterceptor implements NestInterceptor { | |
| intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | |
| const httpCtx = context.switchToHttp(); | |
| const response = httpCtx.getResponse<Response>(); | |
| const status = response.statusCode; | |
| return next.handle().pipe( | |
| map((data) => { | |
| if (status < 400) { | |
| return { | |
| statusCode: status, | |
| result: data, | |
| }; | |
| } else { | |
| return data; | |
| } | |
| }), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment