# new Environment()
This class extends ObjectAccumulator to merge configuration objects while resolving values from Node or browser environment variables on demand.
An 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}
This method 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
|
The object to expose through environment getters and setters. |
void
# fromEnv(k) → {unknown}
This method 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
|
The key to resolve from the environment. |
The value that is resolved from the environment, or undefined if it is absent.
unknown
# orThrow() → {EnvironmentInstance.<any>}
Accessing a property that resolves to undefined or an empty string when declared in the model will throw an error.
Returns a proxy that enforces required environment variables.
A proxy of the environment that enforces required variables.
EnvironmentInstance.<any>
# parseEnvValue(val) → {unknown}
This method interprets booleans and numbers, while leaving other types unchanged.
Converts stringified environment values into native types.
Parameters:
| Name | Type | Description |
|---|---|---|
val |
unknown
|
The raw value that is retrieved from the environment. |
The parsed value, converted to a boolean or number, or left as-is.
unknown
# static accumulate(value) → {AccumulatedEnvironment.<any>}
This method adds new properties and hides raw descriptors to avoid leaking enumeration semantics.
Accumulates the given value into the environment.
Parameters:
| Name | Type | Description |
|---|---|---|
value |
V
|
The object to merge into the environment. |
The updated environment reference.
AccumulatedEnvironment.<any>
# static buildEnvProxy(current, path) → {any}
This 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
|
The seed model segment to use when projecting nested structures. |
path |
Array.<string>
|
The accumulated path segments that lead to the proxy. |
A proxy that resolves environment values or composes additional proxies for deeper paths.
any
# static get(key) → {unknown}
This method 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
|
The key to resolve from the environment store. |
The stored value that corresponds to the provided key.
unknown
# protected static instance(…args) → {E}
This method ensures that only one Environment instance is created, and wraps 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 that are forwarded to the factory when instantiating the singleton. |
The singleton environment instance.
E
# static keys(toEnvopt) → {Array.<string>}
This method 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>