我正在使用拦截器来转换我的响应。我想设置HttpStatus
内部,但我现在使用的代码不起作用。
import { CallHandler, ExecutionContext, NestInterceptor, SetMetadata } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { classToPlain } from 'class-transformer';
import { ApiResponse } from '../models/apiResponse';
export class TransformInterceptor implements NestInterceptor {
intercept(
context: ExecutionContext,
next: CallHandler<ApiResponse | any>,
): Observable<ApiResponse | any> {
return next.handle().pipe(
map(data => {
const http = context.switchToHttp();
const res = http.getResponse();
if(data instanceof ApiResponse) {
if(data.status !== undefined) {
res.status(data.status);
}
}
return classToPlain(data);
}),
);
}
}