0.5.2 • Published 1 month ago

@fp8/simple-config v0.5.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Simple Config Reader

Read the config file from ./[etc|config]/${FP8_ENV}/ directory. The file can be [app|config].[json|yaml]. Only .json and .yaml extension are supported. Any other file extension found or passed will not be loaded.

Note: Package under @fp8 contains Farport Software's specific view on how the code of project is to be organized.

Usage

Assuming that you have a following configuration file saved in your ./config/app.yaml:

name: database-config
username: user-123
password: password-234

Define the following class:

class ConfigData {
    name: string;
    username: string;
    password: string;
}

Load the config using ConfigStore:

const store = new ConfigStore(ConfigData);
const config = store.data; // would be of type ConfigData

console.log('username: ', config.username);
// output user-123

Config Model

The configuration model's properties can be decorated with class-validator to ensure that config data loaded is indeed thie expected format. ConfigStore raises EntityCreationError if validation fails.

It is also possible to skip the definition of the config model by returning a generic IJson and obtain the config value using the ConfigStore.get method:

name: test-app
db:
    username: user-123
    password: password-234
const store = new ConfigStore<IJson>();

console.log('username: ', store.get('db.username'));
// output user-123

Config Options

The IConfigStoreOptions allow customization on how the config should be loaded.

env

The config file are loaded from ./[etc|config]/${FP8_ENV}/ directory. This env option overrides value from FP8_ENV and search the config from ./[etc|config]/${env}/ directory.

configFileName

The primary configuration file name. This is normally app.[json|yaml] or config.[json|yaml] but can be specified via this option.

loadAll

If this option is set, all the files from the config directory are loaded. The logic is:

  1. Find the primary config file
  2. Load all the supported file from the config directory
  3. Append the name of the config file without extension to the config data

Assuming that you have the following files in the config directory:

app.yaml:

name: yaml-config

device.json:

{ "id": "id-abc" }

The expected loaded config data would be:

class ConfigData {
    app: { name: string };
    device: { id: string };
}

entries

This option allows additinal entries to be added to the config data. This is useful if you wish to add secret from vaults to config upon application startup.

Templating

Templating support is done via mustache.

name: url configurator
domain: example.com
url: "https://{{domain}}/info"
env: "ENV.FP8_ENV"

Notice env file can be used as part of templating by prefixing the variable name with ENV..

## Validation

The config store will always validate the loaded config if following syntax is used:

const store = new ConfigStore(ConfigData);

However, if following syntax is used, the validation will be skipped:

const store = new ConfigStore<ConfigData>();

Upon validation error, the EntityCreationError is throw. To see what fields fails the validation, use EntityCreationError.fields property.