vika-server / src /interceptors /response.interceptor.ts
Viktoria435
Initialize NestJS project with basic structure and configurations
22df730
raw
history blame contribute delete
826 Bytes
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,
};
}),
);
}
}