无服务器:如何使用无服务器库集成网关响应?

Serverless: How to integrate gateway response using serverless library?

如何使用无服务器框架集成网关响应?

我无法在 serverless.com as well as in plugin serverless-aws-documentation

中找到文档

好问题。我相信你可以在 s-function.json 文件中这样做:

"endpoints": [
{
  "path": "/test",
  "method": "GET",
  "authorizationType": "none",
  "apiKeyRequired": false,
  "requestParameters": {},
  "requestTemplates": {},
  "responses": {
    "400": {
      "statusCode": "400"
    },
    "default": {
      "statusCode": "200",
      "responseParameters": {},
      "responseModels": {"text/html": "Empty"},
      "responseTemplates": {"text/html": "$input.json('$')"},
      "text/html": ""
    }
  }
}
]

来源:

https://github.com/serverless/serverless/issues/587 https://github.com/serverless/serverless/issues/463

serverless.yml 的资源部分添加如下

resources:
  Resources:
    Unauthorized:
      Type: "AWS::ApiGateway::GatewayResponse"
      Properties:
        ResponseParameters:
          gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
          gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
        ResponseTemplates:
          "application/json": _${file(gateway-responses/responses.js):unauthorized}
        ResponseType: UNAUTHORIZED
        RestApiId: _${file(serverless.env.yml):_${self:provider.stage}.REST_API_ID}
        StatusCode: '401'
    Forbidden:
      Type: "AWS::ApiGateway::GatewayResponse"
      Properties:
        ResponseParameters:
          gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
          gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
        ResponseTemplates:
          "application/json": _${file(gateway-responses/responses.js):forbidden}
        ResponseType: ACCESS_DENIED
        RestApiId: _${file(serverless.env.yml):_${self:provider.stage}.REST_API_ID}
        StatusCode: '403'