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.getmethod allows you to specify which type you expec

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

In this example, when calling the

getmethod, 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.

#typescript #javascript #js #nodejs #node

Node.JS - FoalTS framework - Configuration & environment variables
1.45 GEEK