Reputation: 475
I have a very simple handler that I'm using to familiarize myself with serverless lambda functions.
const example: APIGatewayProxyHandler = async event => {
console.log(JSON.stringify(event, null, 2));
return {
statusCode: 200,
body: JSON.stringify({
message: 'Success',
}),
};
};
Here is my serverless.yml file:
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
When I hit the API it works just fine...
{
statusCode: 200,
body: "{"message":"Success"}"
}
I'm using this URL to trigger the event http://localhost:3000/dev/example?url=https://google.com/
and I'm console logging the event itself when the endpoint is hit...
{
"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"
}
I'm interested in accessing the 'query' attribute of this object but TypeScript gets mad at me when I try...
console.log(event.query);
===
Property 'query' does not exist on type 'APIGatewayProxyEvent'.
The query attribute very clearly exists on the event object as shown in my console log so I'm assuming I'm incorrectly typing this function, but I can't seem to find the proper type. What am I doing wrong here?
Additional Info (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"
}
Upvotes: 0
Views: 863
Reputation: 475
It turns out the problem was the
integration: lambda
addition to my serverless.yml file. When I removed this line the APIGatewayProxyHandler
interface worked as expected.
Upvotes: 1