Hackernoon logoNode.JS - FoalTS framework - Configuration & environment variables by@loicpoullain

Node.JS - FoalTS framework - Configuration & environment variables

Loïc Poullain Hacker Noon profile picture

@loicpoullainLoïc Poullain

Fullstack developper. Creator of FoalTS.

This article presents some improvements introduced in version 2 of FoalTS:

  • Configuration and type safety
  • Configuration and
    .env
    files (
    .env
    ,
    .env.test
    , etc)
  • Available configuration file formats (JSON, YAML and JS)

Configuration and Type Safety

Starting from version 2, great attention is paid to type safety in the configuration. The

Config.get
method allows you to specify which type you expect.

const timeout = Config.get('custom.timeout', 'number');
// The TypeScript type returned by `get` is number|undefined.

In this example, when calling the

get
method, the framework will look at the configuration files to retrieve the desired value.

  • If the value is not defined, the function returns
    undefined
    .
  • If the value is a number, the function returns it.
  • If the value is a string that can be converted to a number (ex:
    "1"
    ), the function converts and returns it.
  • If the value is not a number and cannot be converted, then the function throws a
    ConfigTypeError
    with the details. Note that the config value is not logged to avoid leaking sensitive information.

If you wish to make the config parameter mandatory, you can do it by using the

getOrThrow
method. If no value is found, then a
ConfigNotFound
error is thrown.

const timeout = Config.getOrThrow('custom.timeout', 'number');
// The TypeScript type returned by `get` is number.

Supported types are

string
,
number
,
boolean
,
boolean|string
,
number|string
and
any
.

Multiple
.env
files support

Version 2 allows you to use different

.env
files depending on your environment.

If your configuration is as follows and

NODE_ENV
equals
production
, then the framework will look at
.env.production
to retrieve the value and if it does not exist (the file or the value), Foal will look at
.env
.

Example of configuration file (YAML)

settings:
  jwt:
    secret: env(JWT_SECRET)

Three config formats (JS, JSON, YAML)

JSON and YAML were already supported in version 1. Starting from version 2, JS is also allowed.

YAML example

settings:
  session:
    store: "@foal/typeorm"

JSON example

{
  "settings": {
    "session": {
      "store": "@foal/typeorm"
    }
  }
}

JS example

module.exports = {
  settings: {
    session: {
      store: "@foal/typeorm"
    }
  }
}

More Liberty in Naming Environment Variables

In version 1, the names of the environment variables were depending on the names of the configuration keys. For example, when using

Config.get('settings.mongodbUri')
, Foal was looking at
SETTINGS_MONGODB_URI
.

Starting from version 2, it is your responsibility to choose the environment variable that you want to use (if you use one). This gives more flexibility especially when a Cloud provider defines its own variable names.

YAML example

settings:
  mongodbUri: env(MONGODB_URI)

Previously published at https://foalts.org/blog/2021/03/02/whats-new-in-version-2-part-2

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.