0.1.1 • Published 5 months ago

@gallolabs/config v0.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

Config

Advanced config:

  • from files (super-yaml, json) and envs (scoped or not)
  • Validated user config (with validate component with cast and defaults values)
  • Finalization fn to transform user config to config
  • watch changes and emit on change new config
  • watch alerts in case of no listen on watch events
  • watch changes included files
  • watch new files and unwatch old files if new config does not use same files
  • include files into config from envs (ex: APP_DB_PASSWORD='!include /run/secrets/db_password')
  • Super Json (like Yaml, for example { db: { password: { $include: '/run/secrets/db_password' }, username: { $env: 'USER' } } })
  • Command line arguments as config param
  • Refacto, refacto, refacto

Example:

import {loadConfig} from '@gallolabs/config'

const watchEventEmitter = new EventEmitter

watchEventEmitter.on('change:machin.truc', ({value/*, previousValue, config, previousConfig*/}) => {
    // Update service
    machinService.setTruc(value)
})

deepEqual(
    await loadConfig<Config, Config>({
        defaultFilename: __dirname + '/config.test.yml',
        envFilename: 'config',
        envPrefix: 'app',
        userProvidedConfigSchema: tsToJsSchema<Config>(),
        watchChanges: {
            onChange({config/*, previousConfig, patch*/}) {
                console.log('My new config', config)
            }
            eventEmitter: watchEventEmitter
        }
    }),
    {
        machin: {
            truc: {
                bidule: true
            }
        },
        envShell: 'hello world'
    }
)

const machinService = new MachinService(config.machin)