Creating the Retrieve Lambda
Retrieve information about a reservation.
We'll cover the following...
Overview
Next, we’ll retrieve information about a reservation. To do that, we’ll probably have to pass in two bits of information into the Lambda, namely the userId
and hotelId
. But that might return multiple results if a user has stayed in the hotel before. So, we’ll also pass in the start date of the stay. Because we’re retrieving information but not actually changing anything, the correct HTTP method is GET.
Setup
Create a new directory called retrieve
under the Lambdas directory. Copy the webpack config, jest config, and tsconfig from the other Lambda. Those remain the same. The package.json
is very similar:
{"name": "leanpub-reservations-retrieve-reservation","version": "1.0.0","description": "Lambda for retrieving a reservation","scripts": {"clean": "rm -rf node_modules && rm -rf dist","webpack": "webpack","zip": "cd dist && zip -r retrieve.zip *","build": "npm i && webpack && npm run zip","test": "jest --coverage"},"dependencies": {"fp-ts": "^2.8.3","fp-ts-std": "^0.7.0"},"devDependencies": {"@types/jest": "^26.0.14"},"author": "Sam Van Overmeire","license": "ISC"}
There are ways to avoid (most of) this duplication. We can create a single webpack in the project’s root that conducts multiple builds. The tsconfig files can inherit config from a base config and we could do something similar for jest. That said, because we only have two Lambdas, we’ll leave everything as is, including this bit of duplication.
Writing index test in src
folder
We again create a src
folder with an index file. This time, we’ll start by writing an index test in the __tests__
directory, which only turns green when we’ve completed all our work for this Lambda:
import {handler} from "../src";describe('retrieve index test', () => {it('should return the asked-for reservation', async () => {const startDate = new Date(2020, 11, 10, 12, 0, 0);const queryStringParameters = {hotelId: '1',userId: '11',start: startDate.toISOString(),}const event = {queryStringParameters,}const result: any = await handler(event);expect(result.statusCode).toBe(200);const body = JSON.parse(result.body);expect(body.price).toBe(20);});});
Adding handler
function
Now, add an index.ts
with handler
under the src
folder to make it compile:
import {LambdaEvent} from "../../../util/domain/types";export const handler = async (event: LambdaEvent) => {return {statusCode: 200,}};
The ...