Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SMConfig

Environment and configuration utilities

Hierarchy

  • SMConfig

Index

Constructors

Properties

Accessors

Methods

Constructors

constructor

  • Initializes the class, determining the environment, then loading the configuration for the environment and storing it in the object.

    The config parameter can be an object with the configuration values, or a string representing a JSON/YAML/Hjson file to load, another instance of SMConfig, or an array mixing those. If multiple objects/files are passed, values are merged from left to right, so subsequent values overwrite property assignments of previous values. File type is determined by the extension: json, yml, yaml, hjson.

    When using YAML files, you can also represent additional JavaScript typtes that are not allowed by JSON and Hjson:

    • RegExp: !!js/regexp /pattern/gim
    • Functions: !!js/function 'function () {...}'
    • Undefined: !!js/undefined ''

    The configuration object should have the following structure:

    {
        // Default configuration, for all environments
        default: {
            key1: 'value1',
            key2: 'value2'
        },
    
        // Each subsequent key is the name of the environment;
        // this can be anything you want
        dev: {
            // Custom environments inherit all keys from the default
            // environment, but can be overwritten here
            key1: 'override',
            newkey: 'helloworld'
        },
        production: {
            key1: 'override'
        },
        otherenvironment: {},
    
        // The hostnames object contains a list of hostnames that are
        // mapped to a specific environment.
        hostnames: {
            // Name of the environment, then list of hostnames
            dev: [
                'laptop.localdomain'
            ],
            production: [
                // Can use * as wildcard
                '*.example.com',
                // Can also use RegExp objects
                /(.*?)\.example\.com$/i
            ]
        }
    }

    Configuration values can always be overridden at runtime by passing environmental variables to the application. All values can be specified in a string with key-value pairs, in an environmental variable called SMCONFIG (name can be changed with the options.envVarName setting). You can also append _1, _2, etc, to pass multiple environmental variables. Multiple key-value pairs can be passed in the same variable, separated by a space. If the value contains a space, quotes can be used to escape it. It's possible to load environmental variables from a ".env" file by specifying the file path in the SMCONFIG_FILE (or options.envVarName + '_FILE') variable. This can be used with Docker secrets too, when the path is in /run/secrets. Values passed via environmental variables are strings, but numeric ones (those representing a number) are converted to numbers.

    Values in the hostnames array can be RegExp objects or strings. Strings are parsed using SMHelper.strIs, so the * token can be used as wildcard.

    The environment is determined by, in order:

    1. The value passed to the env parameter
    2. The NODE_ENV environmental variable
    3. The environment that is configured for the hostname
    4. Fallback to the default environment

    Parameters

    • config: SMConfigConfig

      Configuration params or filename(s) to load

    • Optional env: string

      Force a specific environment

    • Optional options: SMConfigOptions

      Dictionary with options

    Returns SMConfig

Properties

Protected _config

_config: Dictionary

Contains the configuration for the current environment

Protected _environment

_environment: string

Holds the name of the environment, as determined during initialization

Accessors

all

environment

  • get environment(): string

Methods

get

  • get(key: string): any
  • Return value for key from configuration

    Parameters

    • key: string

      Configuration key to retrieve

    Returns any

    Value for the key

Generated using TypeDoc