Yet another one config processor. Weird. Slow. Our own.
npm i @qiwi/uniconfig
yarn add @qiwi/uniconfig
Config is just a piece of data
with getters. The data
is obtained from ISource
in some way and processed by registered IPipe
handlers.
These operations form the pipeline
.
Btw, here's the next step in config evolution: topoconfig 🚀
- Declarative definitions
- Multiple source composition
- Modular design
- Ease extensibility
import * as path from 'path'
import { Config, rollupPlugin } from '@qiwi/uniconfig-core'
import envPlugin from '@qiwi/uniconfig-plugin-env'
import jsonPlugin from '@qiwi/uniconfig-plugin-json'
import filePlugin from '@qiwi/uniconfig-plugin-api-file'
import datatreePlugin from '@qiwi/uniconfig-plugin-datatree'
rollupPlugin(envPlugin)
rollupPlugin(jsonPlugin)
rollupPlugin(filePlugin)
rollupPlugin(datatreePlugin)
const target = path.resolve(__dirname, '../../config/default.json')
/* default.json
{
"data": {
"mode": "$env:ENVIRONMENT_PROFILE_NAME",
"server": {
"port": 8080
},
"consul": {
"host": "$env:CONSUL_AGENT_HOST",
"port": "$env:CONSUL_AGENT_PORT",
"token": "***"
}
},
"sources": {
"env": {
"pipeline": "env"
}
}
}
*/
export default new Config({
data: target,
mode: 'sync',
pipeline: 'file>json>datatree'
})
config.get('consul.host') // '10.10.10.10'
- uniconfig-plugin-api-http
- uniconfig-plugin-api-file
- uniconfig-plugin-env
- uniconfig-plugin-ip
- uniconfig-plugin-pkg
- uniconfig-plugin-root
- uniconfig-plugin-global
- uniconfig-plugin-path
- uniconfig-plugin-argv
Update sources
definitions. Replace api
and parser
fields with pipeline
, and target
with data
.
const before = {
"sources": {
"fromFile": {
"target": "<some path>",
"api": "file",
"parser": "json"
}
}
}
const after = {
"sources": {
"fromFile": {
"data": "<some path>",
"pipeline": "file>json"
}
}
}
Produces IConfig
instance.
import {factory} from '@qiwi/uniconfig-core'
...
const config = factory({pipeline: 'env'}) // IConfig
import {transform} from 'lodash'
import {addPipe, context} from '@qiwi/uniconfig-core'
const formatToUpper = data => transform(
data,
(result, value, key) => result[key.toUpperCase()] = ('' + value).toUpperCase(),
{}
)
const pipe = {
handleSync(data) {
return formatToUpper(data)
},
handle(data) {
return Promise.resolve(formatToUpper(data))
}
}
addPipe('uppercase', pipe)
context.pipe.get('uppercase') // <IPipe>
removePipe('uppercase')
context.pipe.get('uppercase') // undefined
import {addPipe, context} from '@qiwi/uniconfig-core'
import envPlugin from '@qiwi/uniconfig-plugin-env'
rollupPlugin(envPlugin)
context.pipe.get('env') // <IPipe>
rollbackPlugin(envPlugin)
context.pipe.get('env') // undefined