🥰The KISS way of configuring your application.
- 👏️No BS.
- 👏No bloat.
- 👏No unnecessary complexity.
- 👏One line and your application is ready.
- Dead simple
- Super extensible
- Configure in Yaml
(pre-packaged)
, JSON(pre-packaged)
, TOML(plugin available)
, Properties,... ✔. - Configure from local file
(pre-packaged)
, S3(plugin available)
, HTTP, Etcd, redis, MySQL,... ✔. - Replace tokens from environment variables
(pre-packaged)
, vault, etcd, DB,... ✔.
- Configure in Yaml
- Super tiny, just one dependency
- Plugin based architecture
- Written in typescript with type definitions
- No unnecessary bloat
Because something simple that just works is always desired over something that is complex and difficult to setup. Exposing 100's of options is not an option... pun intended. The pre-packaged implementation includes support for:
- Loading config file from local disk
- YAML and JSON based config
- Replacing tokens from environment variables for different deployment environments.
And yes, you don't need (you shouldn't) have different configuration files for different environments. Definition should be same. Only values should change.
We'll consider following yaml config file as an example:
port: ${PORT:-8001}
logLevel: ${LOG_LEVEL:-info}
logDirectory: ${LOG_DIRECTORY:-/var/log}
The tokens are contained within ${
and }
pair. They represent variables whose value can come from, for example, environment variables. Text after :-
is the default value that is used when corresponding token value is not found from, for example, environment variable. This part is optional.
import {konfig} from 'konfig';
const config = await konfig(); // loads from default `config.yaml` from current directory
import {konfig} from 'konfig';
const config = await konfig('./config.yaml');
import {konfig, parsers} from 'konfig';
const config = await konfig('./config.json', {parser: parsers.JsonParser})
4. Load from S3 using konfig-s3-loader plugin
// npm install --save konfig-s3-loader
import {konfig, contracts} from 'konfig';
import {S3Provider} from 'konfig-s3-loader';
const s3Provider = (): contracts.Loader => S3Provider({
s3Config: {
accessKeyId: '<AWS_S3_ACCESS_KEY>',
secretAccessKey: '<AWS_S3_ACCESS_KEY_SECRET>',
region: '<AWS_S3_REGION>'
}
});
const config = await konfig('s3://<bucket>/<config-file-path>', {loader: s3Provider});
More details on plugin page
5. Using TOML config file instead of YAML using konfig-toml-parser plugin
import {konfig} from 'konfig';
import {TomlParser} from 'konfig-toml-parser';
const config = await konfig('config.toml', {parser: TomlParser});
More details on plugin page
import {konfig} from 'konfig';
import {S3Provider} from 'konfig-s3-loader';
import {TomlParser} from 'konfig-toml-parser';
const s3Provider = (): contracts.Loader => S3Provider({
s3Config: {
accessKeyId: '<AWS_S3_ACCESS_KEY>',
secretAccessKey: '<AWS_S3_ACCESS_KEY_SECRET>',
region: '<AWS_S3_REGION>'
}
});
const config = await konfig('s3://<bucket>/<config-file-path>', {loader: s3Provider, parser: TomlParser});
Konfig
expects 2 parameters, both optional.
- A
uri
, or astring
like object, which represents the location of config file. - An
object
containing following components fields (all optional):- A
loader
component of typecontracts.Loader
orLoaderProvider
: This component is responsible forloading
config file from givenuri
into memory in string format. - A
tokenizer
component of typecontracts.Tokenizer
orTokenizerProvider
: This component is responsible fortokenizing
config file content into tokens. If no tokens are present, this should behave as a no-op. - A
tokenSubstitutor
component of typecontracts.TokenSubstitutor
orTokenSubstitutorProvider
: This component is responsible forsubstituting
tokens as passed by thetokenizer
. For example, the pre-packagedenvironment variable tokenSubstitutor
substitutes these tokens by looking up environment variables. - A
parser
component of typecontracts.Parser
orParserProvider
: This component is responsible forparsing
the string content into a json object which is what is returned by konfig module. This is the final phase in the pipeline.
- A
- The
Provider
variants can be used when corresponding component needs some initialization. This is similar to aconstructor
in a class.S3Provider
, for example, is an example of this variant. It returns an instance ofcontracts.Loader
after initialization.
We are always looking for contributors of all skill levels! Code quality, tests, plugins, refactorings, documentation, examples, there's so much to contribute to. Contributions are always welcome😊
Working on your first Pull Request? You can learn how from this free series How to Contribute to an Open Source Project on GitHub
This project is open source and available under the MIT License.