Spaces:
Sleeping
Sleeping
File size: 826 Bytes
22df730 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
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';
@Injectable()
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,
};
}),
);
}
}
|