繁体   English   中英

无法确定 AWS 的事件类型 Lambda 无服务器 function

[英]Unable to determine event type for AWS Lambda serverless function

我有一个非常简单的处理程序,我用它来熟悉无服务器 lambda 函数。

const example: APIGatewayProxyHandler = async event => {
  console.log(JSON.stringify(event, null, 2));
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Success',
    }),
  };
};

这是我的 serverless.yml 文件:

service: lambda-example
frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x

plugins:
  - serverless-offline

functions:
  example:
    handler: lib/handler.example
    events:
      - http:
          path: example
          method: get
          integration: lambda

当我点击 API 时,它工作得很好......

{
  statusCode: 200,
  body: "{"message":"Success"}"
}

我正在使用这个 URL 来触发事件http://localhost:3000/dev/example?url=https://google.com/并且当端点被击中时我正在控制台记录事件本身......

{
  "body": {},
  "method": "GET",
  "principalId": "offlineContext_authorizer_principalId",
  "stage": "dev",
  "cognitoPoolClaims": {
    "sub": ""
  },
  "enhancedAuthContext": {
    "principalId": "offlineContext_authorizer_principalId"
  },
  "headers": {
    "Host": "localhost:3000",
    "Connection": "keep-alive",
    "Cache-Control": "max-age=0",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-User": "?1",
    "Sec-Fetch-Dest": "document",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.9,la;q=0.8"
  },
  "query": {
    "url": "https://google.com/"
  },
  "path": {},
  "identity": {
    "accountId": "offlineContext_accountId",
    "apiKey": "offlineContext_apiKey",
    "apiKeyId": "offlineContext_apiKeyId",
    "caller": "offlineContext_caller",
    "cognitoAuthenticationProvider": "offlineContext_cognitoAuthenticationProvider",
    "cognitoAuthenticationType": "offlineContext_cognitoAuthenticationType",
    "sourceIp": "127.0.0.1",
    "user": "offlineContext_user",
    "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36",
    "userArn": "offlineContext_userArn"
  },
  "stageVariables": {},
  "requestPath": "/example"
}

我有兴趣访问这个 object 的“查询”属性,但是当我尝试时 TypeScript 对我很生气......

console.log(event.query);

===

Property 'query' does not exist on type 'APIGatewayProxyEvent'.

查询属性非常清楚地存在于事件 object 中,如我的控制台日志中所示,因此我假设我错误地输入了这个 function,但我似乎找不到正确的类型。 我在这里做错了什么?

附加信息(package.json):

"dependencies": {
  "axios": "^0.21.0",
  "cheerio": "^1.0.0-rc.3"
},
"devDependencies": {
  "@types/aws-lambda": "^8.10.64",
  "@types/cheerio": "^0.22.22",
  "@types/node": "^14.14.2",
  "@typescript-eslint/eslint-plugin": "^4.5.0",
  "@typescript-eslint/parser": "^4.5.0",
  "eslint": "^7.12.0",
  "eslint-config-prettier": "^6.14.0",
  "husky": "^4.3.0",
  "prettier": "^2.1.2",
  "prettier-plugin-organize-imports": "^1.1.1",
  "serverless-dotenv-plugin": "^3.1.0",
  "serverless-offline": "^6.8.0",
  "typescript": "^4.0.3"
}

原来问题是

integration: lambda

除了我的 serverless.yml 文件。 当我删除此行时, APIGatewayProxyHandler接口按预期工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM