首页 > 解决方案 > 使用无服务器从 Swagger UI 中隐藏 OPTIONS 端点

问题描述

我为无服务器 YAML 文件中的每个端点设置了 cors: true 。但是,我不想在 swagger UI 中显示相同的“OPTIONS”端点。我正在使用无服务器框架,下面是示例端点的代码:

sample name:
    handler: path/to/the/handler/
    events:
      - http:
          path: v1/sample
          method: get
          cors: true
          private: true
          documentation:
            summary: "summary of the endpoint"
            methodResponses:
              - statusCode: "200"
                responseBody:
                  description: "response body"
                responseModels:
                  "application/json": "response model"

尽管启用了 CORS,但我希望 OPTIONS 在 Swagger UI 上不可见。

标签: node.jstypescriptswagger-uiserverless

解决方案


从技术上讲,OPTIONS方法端点是 CORS 正常运行所必需的,因为它是检查 CORS 可行性的预检请求中使用的方法。

AWS Swagger 导出确实OPTIONS以相当侵入性的方式包含端点。如果您不想在 Swagger UI 中OPTIONS使用它,我建议您在从 API Gateway 导出 Swagger 文件后以编程方式从 Swagger 文件中过滤出端点,然后再将其用于 Swagger UI(取决于您构建 Swagger UI 的方式)。


推荐阅读