Search⌘ K

Using the Reader Monad to Gather Config

Explore how to apply the Reader monad in your TypeScript projects to gather configuration and dependencies efficiently. This lesson guides you through enhancing a reservation application by injecting config, managing environment checks, and integrating Reader with other monads for improved code clarity and maintainability.

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:

TypeScript 3.3.4
{
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:

TypeScript 3.3.4
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 our ...