问题描述
我正在使用拦截器来转换我的响应.我想在里面设置 HttpStatus
但我现在使用的代码不起作用.
I'm using an interceptor to transform my response. I want to set the HttpStatus
inside but the code I'm using now doesn't work.
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);
}),
);
}
}
推荐答案
更新答案
从nest 6.1.0版本开始,可以在拦截器中设置状态码;它不会再被覆盖(参见这个 PR):
context.switchToHttp()
.getResponse()
.status(205);
过时的答案
不可能从拦截器设置状态代码(请参阅此问题) 因为:
- 有时响应状态代码依赖于异常,并且在拦截器之后执行异常过滤器,
- 全局响应控制器的逻辑是在通过网络发送最终结果之前执行的最后一步(这就是默认状态代码出现的地方).
因此您的状态代码将被默认代码 200/201 或异常过滤器覆盖.
So your status code will be overridden by the default code 200/201 or an exception filter.
作为一种(hacky)解决方法,您可以使用异常过滤器在拦截器中设置状态代码:
As a (hacky) workaround, you can use exception filters to set the status code in interceptors:
1) 创建您自己的异常作为 HttpException
的包装器:
1) Create your own exception as a wrapper around HttpException
:
export class StatusException extends HttpException {
constructor(data, status: HttpStatus) {
super(data, status);
}
}
2) 创建一个设置响应代码并返回数据的异常过滤器:
2) Create an exception filter that sets the response code and returns the data:
@Catch(StatusException)
export class StatusFilter implements ExceptionFilter {
catch(exception: StatusException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const status = exception.getStatus();
console.log(`Setting status to ${status}`);
response.status(status).json(exception.message);
}
}
3) 而不是设置响应在拦截器中抛出相应的异常:
3) Instead of setting the response throw the according exception in your interceptor:
@Injectable()
export class StatusInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next): Observable<any> {
return next.handle().pipe(
map((data: any) => {
if (data.text === 'created') {
throw new StatusException(data, HttpStatus.CREATED);
} else {
throw new StatusException(data, HttpStatus.ACCEPTED);
}
}),
);
}
}
4) 在您的控制器中使用它:
4) Use it in your controller:
@UseFilters(StatusFilter)
@UseInterceptors(StatusInterceptor)
@Controller()
export class AppController {
@Get(':param')
async get(@Param('param') param) {
return { text: param };
}
}
或者,您可以在控制器中注入 @Res()
并直接控制响应代码(但也会丢失拦截器、异常过滤器等)
Alternatively, you can inject @Res()
in your controller and directly control the response code (but also losing interceptors, exception filters, etc.)
这篇关于NestJS 在拦截器中设置 HttpStatus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!