@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
| Parameter | Type | Description |
|---|---|---|
options? | ConfiguratorOptions | - |
Returns
Configurator
Properties
| Property | Type |
|---|---|
_schema | CompiledSchema | Schema |
_resolver | SchemaResolver |
Accessors
sources
Get Signature
get sources():
ConfigurationSource[]
Defined in: configurator.js:170
List of configuration sources registered with this configurator
Returns
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()
staticgetDefaultSources(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
| Parameter | Type | Description |
|---|---|---|
options? | { configContextName?: string; } | - |
options.configContextName? | string | context name for configuration file source |
Returns
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
| Parameter | Type | Description |
|---|---|---|
source | ConfigurationSource | - |
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
| Parameter | Type | Description |
|---|---|---|
context | any | configuration context |
options? | ConfigureOptions | advanced 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
| Parameter | Type | Default value | Description |
|---|---|---|---|
schema | CompiledSchema | undefined | the schema each ConfigurationSource should use to understand valid assignments |
context | any | undefined | configuration context |
strict? | boolean | true | whether to allow accept unexpected configuration inputs |
Returns
Promise<Map<string, any>>
createHelpSchema()
staticcreateHelpSchema():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()
staticcreateConfigSchema():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()
staticcreateDumpSchema():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()
staticcreateSetPropertyValueSchema():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
| Parameter | Type | Description |
|---|---|---|
schema | CompiledSchema | - |
config | any | configuration object to dump |
destination | string | path to write, or "-" for stdout |
options? | DumpOptions | - |
Returns
Promise<void>