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

问题描述

我有一个非常简单的处理程序,用于熟悉无服务器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,"userArn": "offlineContext_userArn"
  },"stageVariables": {},"requestPath": "/example"
}

我有兴趣访问此对象的'query'属性,但是当我尝试使用TypeScript时,我会发怒...

console.log(event.query);

===

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

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

其他信息(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界面按预期工作。