Skip to main content
Version: 3.10.0

core.config

Preface

The core.config object provides methods for reading configuration and for creating new options. It allows to create namespaces, so each namespaced config is isolated from the other namespaces.

info

Here is described the config API partially. For further information about the whole API, please read the @mocks-server/config docs.

API

caution

When the core is passed to a plugin as a parameter, the config object is a config namespace instead of the root config instance. The config namespace is labeled with the plugin id, so each plugin owns its own namespace in the global configuration object.

addNamespace()

core.config.addNamespace(name): Add namespace to the root. Returns a namespace instance.

  • name (String): Name for the namespace.

addOption()

core.config.addOption(optionProperties): Equivalent to the addOption method in namespaces, but it add the option to the root. Returns an option instance.

  • optionProperties (Object): Properties defining the option. See the addOption method in namespaces for further info.

addOptions()

core.config.addOptions(optionsProperties): Add many options. Returns an array of option instances.

  • optionsProperties (Array): Array of optionProperties.

namespace()

core.config.namespace(name): Returns the namespace instance in the root config with name equal to name.

option()

core.config.option(optionName): Returns the option instances in the root config with name equal to optionName.

set()

core.config.set(configuration): Set configuration properties to each correspondent namespace and options.

  • configuration (Object): Object with programmatic configuration. Levels in the object correspond to namespaces names, and last level keys correspond to option names.

validate()

core.config.validate(configuration, options): Allows to pre-validate a configuration before setting it, for example. It returns an object with valid and errors properties. See AJV docs for further info.

  • configuration (Object): Object with configuration. Levels in the object correspond to namespaces names, and last level keys correspond to option names.
  • options (Object): Object with extra options for validation:
    • allowAdditionalProperties (Boolean): Default false. If true, additional properties in the configuration would not produce validation errors.

getValidationSchema()

getValidationSchema(options): Returns a validation schema compatible with AJV for validating the configuration of all nested namespaces.

  • options (Object): Object with extra options for validation:
    • allowAdditionalProperties (Boolean): Default false. If true, the validation schema will allow additional properties.

namespaces

core.config.namespaces: Getter returning array with all root namespaces.

options

core.config.options: Getter returning array with all root options.

value

core.config.value: Getter returning the current values from all namespaces and options as an object. Levels in the object correspond to namespaces names, and last level keys correspond to option names. It can be also used as setter as an alias of the set method, with default options.

loadedFile

core.config.loadedFile: Getter returning the file path of the loaded configuration file. It returns null if no configuration file was loaded.

Namespace instance API

info

A configNamespace instance is returned when using the root core.config.namespace(name) method, or when executing configNamespace.namespace(name) in a namespace. So, any amount of config nested namespace levels can be created.

addNamespace()

configNamespace.addNamespace(name): Add another namespace to the current namespace. Returns a namespace instance.

  • name (String): Name for the namespace.

addOption()

configNamespace.addOption(optionProperties): Adds an option to the namespace. Returns an option instance.

  • optionProperties (Object): Properties defining the option.
    • name (String): Name for the option.
    • description (String): Optional. Used in help, traces, etc.
    • type (String). One of string, boolean, number, array or object. Used to apply type validation when loading configuration and in option.value setter.
    • itemsType (String). Can be defined only when type is array. It must be one of string, boolean, number or object.
    • default - Optional. Default value. Its type depends on the type option.
    • extraData - (Object). Optional. Useful to store any extra data you want in the option. Mocks Server supported extraData options are:
      • scaffold - (Object).
        • omit - (Boolean). Determines whether the option should be included in the config scaffold or not. Default is false.
        • commented - (Boolean). Determines whether the option should be commented in the config scaffold or not. Default is false.
        • value - (Any). Provides a specific value to be set for this option when the config scaffold is created.

addOptions()

configNamespace.addOptions(optionsProperties): Add many options. Returns an array of option instances.

  • optionsProperties (Array): Array of optionProperties.

namespace()

configNamespace.namespace(name): Returns the namespace instance in this namespace with name equal to name.

option()

configNamespace.option(optionName): Returns the option instances in this namespace with name equal to optionName.

set()

configNamespace.set(configuration): Set configuration properties to each correspondent child namespace and options.

  • configuration (Object): Object with configuration. Levels in the object correspond to child namespaces names, and last level keys correspond to option names.

namespaces

configNamespace.namespaces: Getter returning an array with children namespaces.

options

configNamespace.options: Getter returning an array object with children options.

value

configNamespace.value: Getter returning the current values from all child namespaces and options as an object. Levels in the object correspond to namespaces names, and last level keys correspond to option names. It can be also used as setter as an alias of the set method, with default options.

name

configNamespace.name: Getter returning the namespace name.

root

configNamespace.root: Getter returning the root configuration instance. It is useful when trying to access to the root Mocks Server configuration from a plugin, but use it with caution because you will be accessing to all of the elements configuration, not only to the plugin's one.

Option instance API

info

An option instance is returned when using the config.addOption or config.option methods.

set()

option.set(value, [options]): Sets option value. Allows to pass options when setting the value.

  • value : <Any>. Depending on the option type, the value type may be different.
  • options : <Object>.
    • merge : <Boolean>. Determines if the value should be merged with the previous value in case it is an Object or an Array. Default is false.

onChange()

option.onChange(callback): Allows to add a listener that will be executed whenever the value changes. It only emit events after calling to the config.start method. It returns a function that removes the listener once executed.

  • callback(value) (Function): Callback to be executed whenever the option value changes. It receives the new value as first argument.

value

option.value: Getter or setter of the current value.

name

option.name: Getter returning the option name.

type

option.type: Getter returning the option type.

description

option.description: Getter returning the option description.

extraData

option.extraData: Getter returning the option extra data.

default

option.default: Getter returning the option default value.