Skip to main content

@versionzero/configurator / index / Configurator

Class: Configurator

Defined in: configurator.js:44

The Configurator class coordinates configuration.

A Schema is provided as input to define a valid configuration.

The SchemaResolver is used to compile the input Schema into a CompiledSchema.

ConfigurationSource implementations produce configuration assignments that correspond to the CompiledSchema. These assignments are resolved by priority, and then processed using the CompiledSchema to produce a validated configuration object.

Constructors

Constructor

new Configurator(options?): Configurator

Defined in: configurator.js:58

Create a new Configurator

If sources are not provided, the default set of sources will be used (see getDefaultSources()). Sources are processed in order of their sequence number (see ConfigurationSource.DefaultSequence).

Parameters

ParameterTypeDescription
options?ConfiguratorOptions-

Returns

Configurator

Properties

PropertyType
_schemaCompiledSchema | Schema
_resolverSchemaResolver

Accessors

sources

Get Signature

get sources(): ConfigurationSource[]

Defined in: configurator.js:170

List of configuration sources registered with this configurator

Returns

ConfigurationSource[]


schema

Get Signature

get schema(): CompiledSchema | Schema

Defined in: configurator.js:180

Schema definition being used by this Configurator

Returns

CompiledSchema | Schema


resolver

Get Signature

get resolver(): SchemaResolver

Defined in: configurator.js:188

Resolver being used by this Configurator

Returns

SchemaResolver

Methods

getDefaultSources()

static getDefaultSources(options?): ConfigurationSource[]

Defined in: configurator.js:141

Get the default set of configuration sources in priority order.

Default sources and their sequence numbers: 300 - ObjectSource(defaults) - application defaults from context.defaults 400 - EnvironmentSource - environment variables 600 - CommandLineSource - command line arguments 900 - JsonFileSource(config) - user configuration file 1000 - ObjectSource(overrides) - highest priority overrides from context.overrides

To add custom sources between default sources, create a source with an appropriate sequence number. For example, to add a secrets source between environment (400) and command line (600), use sequence: 500.

Parameters

ParameterTypeDescription
options?{ configContextName?: string; }-
options.configContextName?stringcontext name for configuration file source

Returns

ConfigurationSource[]

Examples

// Simple usage: add a custom source with default priority
const configurator = new Configurator({schema});
configurator.registerConfigurationSource(new MySecretsSource({sequence: 500}));
// Advanced usage: full control over source list
const sources = Configurator.getDefaultSources();
sources.push(new MySecretsSource({sequence: 500}));
const configurator = new Configurator({schema, sources});

registerConfigurationSource()

registerConfigurationSource(source): Configurator

Defined in: configurator.js:158

Register a configuration source with this Configurator

Parameters

ParameterTypeDescription
sourceConfigurationSource-

Returns

Configurator


configure()

configure(context, options?): Promise<any>

Defined in: configurator.js:208

Main entry point. Load configuration assignments from all defined sources, and use the highest priority assignments to build a configuration object based on the defined schema.

Parameters

ParameterTypeDescription
contextanyconfiguration context
options?ConfigureOptionsadvanced configuration options

Returns

Promise<any>

  • configuration results

loadSourceAssignments()

loadSourceAssignments(schema, context, strict?): Promise<Map<string, any>>

Defined in: configurator.js:242

Iterate over all sources and build a prioritized map of requested assignments

Parameters

ParameterTypeDefault valueDescription
schemaCompiledSchemaundefinedthe schema each ConfigurationSource should use to understand valid assignments
contextanyundefinedconfiguration context
strict?booleantruewhether to allow accept unexpected configuration inputs

Returns

Promise<Map<string, any>>


createHelpSchema()

static createHelpSchema(): Schema

Defined in: configurator.js:328

Factory for building a schema to handle help requests (see CommandLineSource)

Configurator uses the "configuratorSchema" metadata to identify properties that need special treatment (in this case: "help")

Returns

Schema


createConfigSchema()

static createConfigSchema(): Schema

Defined in: configurator.js:357

Factory for building a schema to load config files. The "context" option causes any value assigned to be copied to the context. If you use the default sources, this setting will get propagated to the sources that handle config files.

Configurator uses the "configuratorSchema" metadata to identify properties that need special treatment (in this case: "config")

Returns

Schema


createDumpSchema()

static createDumpSchema(): Schema

Defined in: configurator.js:376

Factory for building a schema to handle configuration dump requests.

Configurator uses the "configuratorSchema" metadata to identify properties that need special treatment (in this case: "dump")

Returns

Schema


createSetPropertyValueSchema()

static createSetPropertyValueSchema(): Schema

Defined in: configurator.js:396

Factory for building a schema to handle path-based property assignment requests.

Configurator uses the "configurationSchema" metadata to identify properties that need special treatment (in this case: "setPropertyValue")

Returns

Schema


dump()

dump(schema, config, destination, options?): Promise<void>

Defined in: configurator.js:432

Dump formatted configuration to stdout or file

TODO - support writing other formats, in particular .env files and .zsh completion scripts. TODO - flag to omit value if it corresponded to the default?

Parameters

ParameterTypeDescription
schemaCompiledSchema-
configanyconfiguration object to dump
destinationstringpath to write, or "-" for stdout
options?DumpOptions-

Returns

Promise<void>