@machb/config / Settings
Class: Settings
Defined in: Settings.ts:33
Settings implementation that uses a provided map of properties.
This class implements the Configurator interface, allowing retrieval of configuration properties as optional or required values, as well as handling array properties. Settings
Implements
Example
const settings = new Settings({
"apiUrl": "https://api.example.com",
"retryAttempts": "3",
"features": ["feature1", "feature2"]
});
const apiUrl = settings.required("apiUrl"); // "https://api.example.com"
const retryAttempts = settings.optional("retryAttempts", "5"); // "3"
const features = settings.requiredArray("features"); // ["feature1", "feature2"]
const nonExistent = settings.optional("nonExistent", "default"); // "default"
const missingArray = settings.getArrayProp("missingArray"); // undefined
const missingArrayWithDefault = settings.getArrayProp("missingArray", ["default1", "default2"]); // ["default1", "default2"]
Remarks
This implementation is straightforward and relies on the provided properties map. It does not perform any environment variable or file-based lookups. The getArrayProp method treats single string values as one-element arrays. Use this class when you have a predefined set of configuration properties and want to access them in a type-safe manner.
Throws
Error if a required property is missing.
Implements
Constructors
Constructor
new Settings(
props):Settings
Defined in: Settings.ts:40
Create a new Settings instance.
Parameters
props
A map of property names to their string or string array values.
Returns
Settings
Methods
apply()
apply(
props):void
Defined in: Settings.ts:46
Parameters
props
Returns
void
getArrayProp()
getArrayProp(
name,defaultValue?):string[] |undefined
Defined in: Settings.ts:115
Retrieve a configuration property and return it as an array of strings, or a default array.
Current behavior:
- If a value is found, it is returned as a single-element string array: [value].
- Otherwise, the provided defaultValue is returned (may be undefined).
Parameters
name
string
defaultValue?
string[]
Returns
string[] | undefined
Implementation of
optional()
optional(
name,defaultValue?):string|undefined
Defined in: Settings.ts:60
Retrieve a configuration property as an optional string. If the property is not found, return the provided default value or undefined.
Parameters
name
string
defaultValue?
string
Returns
string | undefined
The property value as a string, or the default value if not found.
Remarks
This method does not throw an error if the property is missing; it simply returns the default value or undefined.
Implementation of
prop()
prop(
name,value):void
Defined in: Settings.ts:42
Parameters
name
string
value
any
Returns
void
required()
required(
name):string
Defined in: Settings.ts:77
Retrieve a required configuration property as a string. If the property is not found, this method throws an Error.
Parameters
name
string
Returns
string
The property value as a string.
Throws
Error if the property cannot be resolved.
Remarks
This method ensures that the requested property is present; otherwise, it raises an error. Use this method when the presence of the property is critical for application functionality.
Throws
Error if the property cannot be resolved.
Implementation of
requiredArray()
requiredArray(
name):string[]
Defined in: Settings.ts:98
Retrieve a required configuration property as an array of strings. If the property is not found, this method throws an Error.
Parameters
name
string
The property name to retrieve.
Returns
string[]
An array of strings representing the property value.
Throws
Error if the property cannot be resolved.
Remarks
This method treats a single string value as a one-element array. Use this method when the property is expected to have multiple values.
Throws
Error if the property cannot be resolved.