

This article presents some improvements introduced in version 2 of FoalTS:
.env
files (.env
, .env.test
, etc)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.undefined
."1"
), the function converts and returns it.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
..env
files supportVersion 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)
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"
}
}
}
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
Create your free account to unlock your custom reading experience.