Hi,
I’m doing my first baby steps with Reason and getting really excited!
My first playground is using it in an AWS Lambda project. Found bs-aws-lambda which looked really promising to quickly get going with type safety. The challenge I’m facing now, seems to be related to how [@bs.optional]
in AwsLambda.APIGatewayProxy.Event.t handles fields with null
, though I’d be surprised if this isn’t my fault…
TLDR; I’m trying to gracefully extract the query parameter name
, and create a greeting based on whether that parameter exists or not.
# greeter.re
open AwsLambda.APIGatewayProxy;
open Belt;
let sayHello = (event: Event.t): string => {
let nameParam =
event
->Event.queryStringParametersGet
->Option.flatMap(dict => dict->Js.Dict.get("name"));
switch (nameParam) {
| Some(name) => "hello there " ++ name ++ "!"
| None => "no idea what your name is :/"
};
};
# handler.js
const greeter = require('./greeter.bs')
module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify({
message: greeter.sayHello(event)
})
};
};
event
example with query parameter in place (simplified for brevity):
{
headers: { .. },
path: '/hello',
queryStringParameters: { name: 'Santa' },
body: null
}
Everything works just as expected with the above. The challenge is when no query parameters are passed in at all in the incoming HTTP request;
{
headers: { .. },
path: '/hello',
queryStringParameters: null,
body: null
}
That results in the following error:
Cannot read property 'name' of null
In other words, the Js.Dict.get()
in greeter.re
is trying to operate on a null
value rather than an actual dictionary
Any thoughts what I should rather do to avoid this explosion?