@brickblock/strong-config
v0.10.0
Published
Safe config for node
Downloads
32
Keywords
Readme
@brickblock/strong-config
strong-config
is a library that does 4 things:
- Loads a file (
yaml
orjson
) into memory as an object - Optionally, decrypts a sops encrypted file
- Optionally, validates the config against a schema defined in json-schema
- Substitutes environment variables with the loaded and decrypted values from memory
Installation
The library should be installed as a dependency
:
yarn add @brickblock/strong-config
Conventions
strong-config
will read the config fromconfig/${NODE_ENV}.[yml|yaml|json]
- If the config file contains a
sops
key, it is assumed to be encrypted and it will be decrypted usingsops
.sops
must already be available on your$PATH
- If a
config/schema.[yml|yaml|json]
exists, the resulting config object is validated against the schema using json-schema - If the config file has any instances of
${ENV_VAR}
it will be substituted with the value defined in the environment.
Usage
NODE_ENV=production yarn start
/* config/production.json */
"logger": {
"name": "my-prod-logger"
"level": "INFO"
}
/* index.js */
const config = require('@brickblock/strong-config').load()
// config = { logger: { name: 'my-prod-logger', level: 'INFO' }}
const logger = bunyan.createLogger(config.logger)
Flow Types for Schemas
When given a valid json-schema, this package will
automatically generate a flow type of the schema to allow strongly-typing the
config
-object in consuming applications.
Caveats
- The consuming application must be executed with
NODE_ENV=development
to trigger the flow type generation - Schema must be a JSON file
Using the type
When using .load()
, flow will automatically pick up the auto-generated type
definition at @brickblock/strong-config/build/index.flow
and will use it to
strongly-type the config-object:
/* ./src/index.js */
// @flow
const config = require('@brickblock/strong-config').load()
// flow will warn that the `config` object doesn't have the key `nonExistentConfigKey`!
const ProblematicComponent = () => <div>{config.nonExistentConfigKey}</div>
Next Steps
Follow the Onboarding tutorial