如何在 NestJS 中安装 Express 中间件(express-openapi-validator)?

How to install Express middleware (express-openapi-validator) in NestJS?

我正在写一个NestJS application. Now I want to install the Express middleware express-openapi-validator

但是,我无法让它工作。有一个description for how to install the express-openapi-validator in express,但它总是导致错误。

例如

export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        consumer.apply(middleware({apiSpec "./bff-api.yaml"}))
            .forRoutes(OrganizationController)
    }
}

结果

error TS2345: Argument of type 'OpenApiRequestHandler[]' is not assignable to parameter of type 'Function | Type<any>'.
      Type 'OpenApiRequestHandler[]' is missing the following properties from type 'Type<any>': apply, call, bind, prototype, and 4 more.

如何在 NestJS 中安装这个中间件?

我现在已经开始工作了:

configure(consumer: MiddlewareConsumer) {
    middleware({
        apiSpec: `${__dirname}/../api-doc/bff-api.yaml`
    }).forEach(value => consumer.apply(value).forRoutes(OrganizationController))
}

我加了一个NestJS example to express-openapi-validator (static link for posterity).

AppModule 看起来基本相同,尽管您不需要迭代中间件:

@Module({
  imports: [PingModule],
  providers: [{ provide: APP_FILTER, useClass: OpenApiExceptionFilter }],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(
        ...OpenApiValidator.middleware({
          apiSpec: join(__dirname, './api.yaml'),
        }),
      )
      .forRoutes('*');
  }
}

我还添加了一个异常过滤器来将错误从 express-openapi-validator 转换为正确的响应;否则我总是会得到 500 错误。您还可以使用此方法将错误转换为自定义错误格式。

import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
import { Response } from 'express';
import { error } from 'express-openapi-validator';

@Catch(...Object.values(error))
export class OpenApiExceptionFilter implements ExceptionFilter {
  catch(error: ValidationError, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();

    response.status(error.status).json(error);
  }
}

interface ValidationError {
  status: number;
  message: string;
  errors: Array<{
    path: string;
    message: string;
    error_code?: string;
  }>;
  path?: string;
  name: string;
}