Spaces:
Sleeping
Sleeping
| import { | |
| Injectable, | |
| NestInterceptor, | |
| ExecutionContext, | |
| CallHandler, | |
| StreamableFile, | |
| } from '@nestjs/common'; | |
| import { Response } from 'express'; | |
| import { Observable } from 'rxjs'; | |
| import { map } from 'rxjs/operators'; | |
| import { ApiResponse } from '../utils/response-wrapper.utils'; | |
| () | |
| export class ResponseInterceptor<T> | |
| implements NestInterceptor<T, ApiResponse<T> | T> | |
| { | |
| intercept( | |
| context: ExecutionContext, | |
| next: CallHandler, | |
| ): Observable<ApiResponse<T> | T> { | |
| const response = context.switchToHttp().getResponse<Response>(); | |
| return next.handle().pipe( | |
| map((data: T) => { | |
| if (data instanceof StreamableFile) return data; | |
| if (response.headersSent) return data; | |
| return { | |
| data, | |
| successful: true, | |
| }; | |
| }), | |
| ); | |
| } | |
| } | |