AppConfiguration

node/appConfiguration. AppConfiguration

This is a service to manage applications configurations. It takes care of loading, activating, switching and merging configuration files.

Constructor

new AppConfiguration(environmentUtils, rootRequire, appNameopt, defaultConfigurationopt, optionsopt)

Source:
Tutorials:
Parameters:
Name Type Attributes Default Description
environmentUtils EnvironmentUtils

Required to read the environment variables and determine which configuration to use.

rootRequire RootRequireFn

Necessary to be able to require the configuration files with paths relative to the app root directory.

appName string <optional>
'app'

The name of the app using this service. It's also used as part of the name of the configuration files.

defaultConfiguration Object <optional>
{}

The default configuration the others will extend.

options Partial.<AppConfigurationOptions> <optional>
{}

Options to customize the service.

Members

activeConfiguration :string

Source:

The name of the active configuration.

Type:

canSwitch :boolean

Source:

Whether or not the active configuration can be switched.

Type:

configurations :Object.<string, Object>

Source:

A dictionary with all the loaded configurations. It uses the names of the configurations as keys.

Type:

options :AppConfigurationOptions

Source:

The service customizable options.

Type:

Methods

get(setting, asArrayopt) → {*}

Source:

Gets a setting or settings from the active configuration.

Example
// To get a single setting
  const value = appConfiguration.get('some-setting');

  // To get multiple values
  const { settingOne, settingTwo } = appConfiguration.get([
    'settingOne',
    'settingTwo',
  ]);

  // Use paths
  const subValue = appConfiguration.get('settingOne.subSetting');
Parameters:
Name Type Attributes Default Description
setting string | Array.<string>

A setting path or a list of them.

asArray boolean <optional>
false

When setting is an Array, if this is true, instead of returning an object, it will return an array of settings.

Returns:
Type
*

getConfig(nameopt) → (nullable) {Object}

Source:

Gets a configuration settings. If no name is specified, it will return the settings of the active configuration.

Parameters:
Name Type Attributes Default Description
name string <optional>
''

The name of the configuration.

Returns:
Type
Object

load(name, settings, switchToopt) → {Object}

Source:

Load a new configuration.

Parameters:
Name Type Attributes Default Description
name string

The configuration name.

settings Object

The configuration settings.

switchTo boolean <optional>
true

If the service should switch to the new configuration after adding it.

Throws:

If the configuration tries to extend a configuration that doesn't exist.

Type
Error
Returns:

The settings of the new configuration.

Type
Object

loadFromEnvironment() → {Object}

Source:

Checks if there's a configuration name on the environment variable and if there is, try to load the configuration file for it.

Returns:

The loaded configuration or an empty object if the variable was empty.

Type
Object

loadFromFile(name, switchToopt, checkSwitchFlagopt) → {Object}

Source:

Loads a configuration from a file.

Parameters:
Name Type Attributes Default Description
name string

The name of the configuration.

switchTo boolean <optional>
true

If the service should switch to the new configuration after adding it.

checkSwitchFlag boolean <optional>
true

If true, the service will update the value of allowConfigurationSwitch based on the loaded configuration setting.

Throws:

If the configuration file can't be loaded.

Type
Error
Returns:

The settings of the loaded configuration.

Type
Object

set(setting, valueopt)

Source:

Sets the value of a setting or settings from the active configuration. If both the current and the new value of a setting are objects, then instead of overwriting it, the method will merge them.

Example
// To set a single setting value
  appConfiguration.set('some-setting', 'some-setting-value');
  // To set the value of multiple settings
  appConfiguration.set({
    settingOne: 'valueOne',
    settingTwo: 'valueTwo',
  });
Parameters:
Name Type Attributes Description
setting string | Object.<string, any>

The name of the setting to update or a dictionary of settings and their values.

value * <optional>

The value of the setting. This is only used when setting is a string.

Throws:

If setting is not a dictionary and value is undefined.

Type
Error

setConfig(config, nameopt, mergeopt) → {Object}

Source:

Overwrites all the settings for a configuration. If the name is not specified, it will overwrite the active configuration.

Parameters:
Name Type Attributes Default Description
config Object

The new configuration settings.

name string <optional>
''

The name of the configuration.

merge boolean <optional>
true

Whether or not to merge the new settings with the existing ones.

Returns:

The updated configuration.

Type
Object

switch(name, forceopt) → {Object}

Source:

Switchs to a different configuration. If the configuration is not registered, it will try to load from a file.

Parameters:
Name Type Attributes Default Description
name string

The new of the configuration to switch to.

force boolean <optional>
false

A way to force the service to switch even if the allowConfigurationSwitch property if false.

Throws:

If force is false and the allowConfigurationSwitch property is false.

Type
Error
Returns:

The new active configuration.

Type
Object