Collect and parse configuration from different sources
# .env
STR=value
NUM=42
BOOL=true
import { DotEnvLoader } from "config-loaders";
const loader = new DotEnvLoader();
const value = loader.getValue();
/* value is:
{
STR: "value",
NUM: 42,
BOOL: true,
}
*/
import { EnvLoader } from "config-loaders";
const loader = new EnvLoader({
filterNames: ["VAR1", "VAR2"],
});
const value = loader.getValue();
/*
value contains only VAR1 and VAR2 variables
(if they exist in the `process.env`)
*/
import {
DotEnvLoader,
EnvLoader,
LoadersPack,
ValueLoader,
} from "config-loaders";
const loader = new LoadersPack([
new DotEnvLoader(),
new EnvLoader(),
new ValueLoader({ value: { TEST: true } }),
]);
const value = loader.getValue();
/*
value is values from .env file overriden by values
from process.env and { TEST: true } object.
*/
npm install config-loaders
Class representing a loader.
The process of obtaining a value is divided into three stages:
- Loading
- Parsing
- Transformation
The derived class must implement methods of stages 1 and 2.
Stage 3 method returns unchanged value by default and can be omitted.
The main method for getting the loader's value. Should return a serializable object.
Should implement the logic for obtaining raw value.
Should implement the logic for parsing the raw value.
May optionally describe the logic for converting a parsed value to its final form.
Base class for implementing file loaders. Includes logic for search the file by traversing directory tree up.
Name of the file to be loaded.
default:
"config"
Directory where the file search starts.
default:
process.cwd()
Try to find the file in upper directories.
default:
true
;
Directory where the file search stops.
default:
path.parse(fileDir).root
Encoding of the file.
default:
"utf-8"
Don't throw an error if the file isn't found.
default:
false
Loader that utilizes dotenv library for parsing files contain environment variables settings. Checks if there are expandable variables and expand them using dotenv-expand library. The default parser also converts string representations of primitive types to their proper types.
Requires dotenv
package to be installed.
Requires dotenv-expand
package to be installed in the case
of expandable variables processing.
FileLoader options with default fileName .env
:
Whether to expand variables.
default:
true
;
Parser options. See the list here.
default:
{}
Loader that reads process.env
object.
The default parser converts string representations of primitive types
to their proper types.
Parser options. See the list here.
default:
{}
Filter properties in the final object by its names.
default:
false
Loader that takes an array of loaders and merges the values loaded using them.
Loaders list
Loader for loading .yaml files.
See FileLoader options for options list.
Default filename is config.yaml
.
Loader that takes a static value or a function returning the value.
Value itself or a factory function returning the value.
Licensed under the MIT license. Copyright (c) 2023-present Vladislav Alexeev