https://github.com/alt-javascript/config
An extensible wrapper of the popular config package, supporting placeholder resolution (or variable expansion); encrypted values (via jasypt); default (or fallback) values; and asynchronous url fetching.
https://github.com/alt-javascript/config
config enrypted fetch javascript nodejs placeholders properties url variable-expansion
Last synced: 10 months ago
JSON representation
An extensible wrapper of the popular config package, supporting placeholder resolution (or variable expansion); encrypted values (via jasypt); default (or fallback) values; and asynchronous url fetching.
- Host: GitHub
- URL: https://github.com/alt-javascript/config
- Owner: alt-javascript
- License: mit
- Created: 2021-07-11T19:04:22.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-04-19T23:39:07.000Z (over 3 years ago)
- Last Synced: 2025-02-19T09:18:21.826Z (11 months ago)
- Topics: config, enrypted, fetch, javascript, nodejs, placeholders, properties, url, variable-expansion
- Language: JavaScript
- Homepage:
- Size: 841 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
README
An Extensible Wrapper for Node Config
=====================================
[](https://nodei.co/npm/@alt-javascript/config/)


[release notes](https://github.com/@alt-javascript/config/blob/main/History.md)
Introduction
--------------------------------
An extensible wrapper of the popular config package, supporting:
- placeholder resolution (or variable expansion),
- encrypted values (via jasypt)
- default (or fallback) values,
- and optional asynchronous url fetching.
Usage
-------------------------
To use the module, substitute the named {config} module export, in place of the popular
[config](https://www.npmjs.com/package/config) default – note, we use named exports, because the module
exports other useful classes as well.
```javascript
import { config } from '@alt-javascript/config' ;
config.get('key');
config.get('nested.key');
config.get('unknown','use this instead'); // this does not throw an error
```
Config values that include the common `${placeholder}` syntax, will resolve the inline
placeholders, so the `config.get('placeholder')'` path below will return `start.one.two.end`.
Config values that start with the prefix `enc.` will be decrypted with the
[jasypt](https://www.npmjs.com/package/jasypt) package port, with the passphrase being
sourced from the `process.env.NODE_CONFIG_PASSPHRASE` environment variable.
Optionally, config values that start with the prefix `url.` can be fetched and resolved asynchronously with the `fetch`
function, and HTTP options can be specified as in the example config file. To avoid bundling `node-fetch`, you need to
provide it by using `@alt-javascript/boot` to boot it into the global root context, where the package will detect it.
```javascript
import { boot } from '@alt-javascript/boot';
import { config } from '@alt-javascript/config';
import fetch from 'node-fetch';
boot({config,fetch})
const webdata = await config.fetch('pathToUrlPrefixedValue');
```
> :warning: While we have implemented asynchronous fetch from "the network", we discourage it.
>
> It's mostly a design flex.
>
> Configuration should be static and immutable contextual information your system needs on application bootstrap, and
> configuration as a service increases the complexity of your deployment architecture, making it difficult to configure
> different, and ephemeral deployment options, including local development and testing.
>
> Don't say we didn't warn you.
`local-development.json`
```json
{
"key": "value",
"one" : "one",
"placeholder": "start.${one}.${nested.two}.end",
"placeholderEncrypted": "start.${nested.encrypted}.end",
"nested" : {
"key" : "value",
"two" : "two",
"placeholder": "start.${one}.${nested.two}.end",
"encrypted" : "enc.pxQ6z9s/LRpGB+4ddJ8bsq8RqELmhVU2",
"encryptedWithSecret" : "enc./emLGkD3cbfqoSPijGZ0jh1p1SYIHQeJ"
},
"jsonplaceholder": {
"todos": "url.https://jsonplaceholder.typicode.com/todos/1"
},
"fetchWithOpts" : {
"url": "url.https://jsonplaceholder.typicode.com/todos/1",
"authorization": "Basic dXNlcjpwYXNz",
"method": "get",
"body": {},
"headers": {"Content-Type": "application/json"}
}
}
```
### Browser
The module is also able to be used directly in the browser, in combination with the config module.
You can either import the LoggerFactory globally as an IIFE (Immediately Invoked Function Expression),
as follows:
```html
var config = ConfigFactory.getConfig({
logging : {
format : 'json',
level : {
'/' : 'info',
'/MyPage': 'info'
}
}
"http://127+0+0+1:8080" : {
logging : {
format : 'json',
level : {
'/' : 'info',
'/MyPage' : 'debug'
}
}
}
})
var logger = LoggerFactory.getLogger('/MyPage',config);
logger.debug('Hello World');
```
Or import the ES6 module bundle from a module, as follows:
```javascript
import { ConfigFactory } from 'https://cdn.jsdelivr.net/npm/@alt-javascript/logger/dist/alt-javascript-config-esm.js'
//...as above
```
Encrypted config is not supported in the browser (by default), as it is
inherently unsecure (there is no safe way to hide the salt).
Additionally, config sections can be prefixed with the window location to allow
site or environment specific configuration in the browser (periods must be replaced with
a plus sign - so mysite.com => mysite+com).
Testability
-------------------------
Testing config is hard, and testability is a first class concern at @alt-javascript so the config wrapper,
and the module exports an EphemeralConfig that can source config paths from a plain old javascript
object as follows, allowing you to assert varying configurations easily
```javascript
import {
EphemeralConfig, ValueResolvingConfig, PlaceHolderResolver, PlaceHolderSelector
} from '@alt-javascript/config';
const ephemeralConfig = new EphemeralConfig({
key: 'value',
nested: {
key: 'value',
},
});
const placeHolderResolver = new PlaceHolderResolver(new PlaceHolderSelector());
const config = new ValueResolvingConfig(ephemeralConfig,placeHolderResolver );
```
License
-----------------------------
May be freely distributed under the [MIT license](https://raw.githubusercontent.com/alt-javascript/config/main/LICENSE).
Copyright (c) 2021 Craig Parravicini