Skip to content
/ uniconfig Public

Yet another one config processor. Weird. Slow. Our own.

License

Notifications You must be signed in to change notification settings

qiwi/uniconfig

Repository files navigation

uniconfig

CI Known Vulnerabilities Maintainability Test Coverage

Yet another one config processor. Weird. Slow. Our own.

Install

    npm i @qiwi/uniconfig
    yarn add @qiwi/uniconfig

Concept

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 🚀

Features

  • Declarative definitions
  • Multiple source composition
  • Modular design
  • Ease extensibility

Basic usage

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'

Examples

Plugins

Fetchers
Parsers
Template engines
Etc

API

Migration from 1.x to 2.x

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"
    }
  }
}

factory

Produces IConfig instance.

import {factory} from '@qiwi/uniconfig-core'
...

const config = factory({pipeline: 'env'}) // IConfig

addPipe

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

removePipe('uppercase')
context.pipe.get('uppercase') // undefined

rollupPlugin

import {addPipe, context} from '@qiwi/uniconfig-core'
import envPlugin from '@qiwi/uniconfig-plugin-env'

rollupPlugin(envPlugin)
context.pipe.get('env') // <IPipe>

rollbackPlugin

rollbackPlugin(envPlugin)
context.pipe.get('env') // undefined