I have been trying and searched online, but was not able to find a response. Is it possible to achieve the following using Serverless Framework:
I want to use the get.handler that has the code to the following definition for both getting one item and getting all the items. So:
api.example.com/items/ I retrieve all the itemsapi.example.com/items/1234 I retrieve item with id = 1234 - get_items:
handler: project/items/get.handler
events:
- http:
path: items/{itemId}
method: get
So far in the get.handler I check event.pathParameters? event.pathParameters.itemId : null if the specific item exists and call some getItem(itemdId) function and if it does not exits I call a getAll() function.
If I pass the item id in the path it works, but when I make a request for api.example.com/items/ I get the following error:
not a valid key=value pair (missing equal-sign) in Authorization header. This means something is wrong in my path and I have to pass the item id to the path parameters.
My question is: Is there a way I can use multiple paths in the - http: area, or what would be a recommended way to solve this issue (just create two separate handlers) ?
There are two ways to easily accomplish what you're looking for.
Firstly, a lambda function can be triggered by multiple events. You can add another http event to the array of handlers like so:
get_items:
handler: project/items/get.handler
events:
- http:
path: items/{itemId}
method: get
- http:
path: items/
method: get
Alternatively, you could use the {proxy+} argument. You can read more about the various proxy methods here