NestJS 在拦截器中设置 HttpStatus

NestJS set HttpStatus in interceptor

我正在使用拦截器来转换我的响应。我想在里面设置 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);
      }),
    );
  }
}

更新答案

从nest 6.1.0版本开始,可以在拦截器中设置状态码;它不会再被覆盖(参见this PR):

context.switchToHttp()
      .getResponse()
      .status(205);

过时的答案

不可能从拦截器设置状态代码(请参阅此 issue)因为:

  • sometimes response status codes are dependent on exceptions and exception filters are executed after interceptors,
  • global response controller's logic is the last step performed just before sending a final result through the network (that's the place where default status codes come in).

因此您的状态代码将被默认代码 200/201 或异常过滤器覆盖。


作为(hacky)解决方法,您可以使用异常过滤器在拦截器中设置状态代码:

1) 创建您自己的异常作为 HttpException:

的包装器
export class StatusException extends HttpException {
  constructor(data, status: HttpStatus) {
    super(data, status);
  }
}

2) 创建一个设置响应代码和 returns 数据的异常过滤器:

@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) 不要设置响应,而是在拦截器中抛出相应的异常:

@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) 在你的控制器中使用它:

@UseFilters(StatusFilter)
@UseInterceptors(StatusInterceptor)
@Controller()
export class AppController {
  @Get(':param')
  async get(@Param('param') param) {
    return { text: param };
  }
}

或者,您可以在您的控制器中注入 @Res() 并直接控制响应代码(但也会丢失拦截器、异常过滤器等)