31 lines
830 B
TypeScript
31 lines
830 B
TypeScript
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from "@nestjs/common";
|
|
import { Response } from "express";
|
|
import { Observable } from "rxjs";
|
|
import { map } from "rxjs/operators";
|
|
|
|
@Injectable()
|
|
export class ResponseInterceptor implements NestInterceptor {
|
|
//
|
|
intercept(context: ExecutionContext, next: CallHandler<Record<string, unknown>>): Observable<unknown> {
|
|
const ctx = context.switchToHttp().getResponse<Response>();
|
|
const statusCode = ctx.statusCode;
|
|
|
|
return next.handle().pipe(
|
|
map((data) => {
|
|
if (data && data.data !== undefined) {
|
|
return {
|
|
statusCode,
|
|
success: true,
|
|
data: data.data,
|
|
};
|
|
}
|
|
return {
|
|
statusCode,
|
|
success: true,
|
|
data,
|
|
};
|
|
}),
|
|
);
|
|
}
|
|
}
|