Using the Reader Monad to Gather Config
Learn to expand our application for gathering config using the Reader monad
We'll cover the following...
Overview
In this chapter, we’ll consider some ways in which we can expand our basic, but functional, application. Some ideas will be implemented in their entirety, and others will remain superficial sketches. Let’s again consider the very unfortunate lack of configuration injection in some of our functions. When constructing parameters for retrieving or adding data to DynamoDB, we did this:
{TableName: process.env.DATABASE_NAME,// ...}
Generating config
First, we need a function to generate config and some additional example configuration. We might use the hotel with ID 1, which we’ve been using in our tests and is a test object we don’t want to see in production. We need to check which environment we’re in. This is our type:
export type Config = {tableName: string,environment: string,testHotelAllowed: boolean,};
Besides the two environment variables, our config generates a boolean to verify whether test hotels are allowed. Another approach would be to add a list of test hotels to the configuration and check those in our validation.
Now add a config file in ...