# new Environment()
Extends ObjectAccumulator to merge configuration objects while resolving values from Node or browser environment variables on demand.
Environment accumulator that lazily reads from runtime sources.
Example
const Config = Environment.accumulate({ logging: { level: "info" } });
console.log(Config.logging.level);
console.log(String(Config.logging.level)); // => LOGGING__LEVEL key when serialized
Members
# protected factory
Defines how new instances of the Environment class should be created.
A factory function for creating Environment instances.
Methods
# expand(value) → {void}
Defines lazy properties that first consult runtime variables before falling back to seeded values.
Expands an object into the environment.
Parameters:
| Name | Type | Description |
|---|---|---|
value |
V
|
Object to expose through environment getters and setters. |
void
# fromEnv(k) → {unknown}
Handles browser and Node.js environments by normalizing keys and parsing values.
Retrieves a value from the runtime environment.
Parameters:
| Name | Type | Description |
|---|---|---|
k |
string
|
Key to resolve from the environment. |
Value resolved from the environment or undefined when absent.
unknown
# orThrow() → {this}
Accessing a property that resolves to undefined or an empty string when declared in the model throws an error.
Returns a proxy enforcing required environment variables.
Proxy of the environment enforcing required variables.
this
# parseEnvValue(val) → {unknown}
Interprets booleans and numbers while leaving other types unchanged.
Converts stringified environment values into native types.
Parameters:
| Name | Type | Description |
|---|---|---|
val |
unknown
|
Raw value retrieved from the environment. |
Parsed value converted to boolean, number, or left as-is.
unknown
# static accumulate(value) → {Environment}
Adds new properties, hiding raw descriptors to avoid leaking enumeration semantics.
Accumulates the given value into the environment.
Parameters:
| Name | Type | Description |
|---|---|---|
value |
V
|
Object to merge into the environment. |
Updated environment reference.
# static buildEnvProxy(current, path) → {any}
Allows chained property access to emit uppercase ENV identifiers while honoring existing runtime overrides.
Builds a proxy that composes environment keys for nested properties.
Parameters:
| Name | Type | Description |
|---|---|---|
current |
any
|
Seed model segment used when projecting nested structures. |
path |
Array.<string>
|
Accumulated path segments leading to the proxy. |
Proxy that resolves environment values or composes additional proxies for deeper paths.
any
# static get(key) → {unknown}
Delegates to the singleton instance to access stored configuration.
Retrieves a value using a dot-path key from the accumulated environment.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string
|
Key to resolve from the environment store. |
Stored value corresponding to the provided key.
unknown
# protected static instance(…args) → {E}
Ensures only one Environment instance is created, wrapping it in a proxy to compose ENV keys on demand.
Retrieves or creates the singleton instance of the Environment class.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
args |
Array.<unknown>
|
<repeatable> |
Arguments forwarded to the factory when instantiating the singleton. |
Singleton environment instance.
E
# static keys(toEnvopt) → {Array.<string>}
Gets all keys in the environment, with an option to format them for environment variables.
Retrieves the keys of the environment, optionally converting them to ENV format.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
toEnv |
boolean
|
<optional> |
true | Whether to convert the keys to ENV format. |
An array of keys from the environment.
Array.<string>