Skip to content

NodeJS module for strictly configuration from environment variables.

License

Notifications You must be signed in to change notification settings

CheerlessCloud/twconf

Repository files navigation

twconf

Commitizen friendly Conventional Commits codestyle PRs Welcome

Travis dependencies Status devDependencies Status Coverage Status

Attention: this is beta version. Use it in production with caution.

NodeJS module for strictly configuration from environment variables.

import TwConf from 'twconf';

const conf = new TwConf({
  'database.mongodb.uri': {
    comment: 'MongoDB connection URI',
    simple: 'mongodb://localhost/test1',
    type: new TwConf.Types.StringType({ allowed: [ /mongodb\:/ ] }),
    required: true,
    preTransforms: [ /* function(value: string).<any> */ ],
    postTransforms: [ /* function(value: any).<any> */ ],
    validators: [
      value => value !== 'mongodb://localhost/test',
    ],
  },
});

conf.get('database.mongodb.hostname'); // get config value
conf.toString(); // get JSON version of config

String-defined type

new TwConf({
  nodeEnv: {
    type: 'string',
    allowed: ['development', 'test', 'production'],
    default: 'development',
  },
});

Object-defined type

new TwConf({
  nodeEnv: {
    type: {
      name: 'string',
      allowed: ['development', 'test', 'production'],
    },
    default: 'development',
  },
});

Available types:

  • Boolean
    • no options
  • Float
    • min: number - minimal value for field
    • max: number - maximum value for field
    • precision: number - count of number after dot
  • Int
    • min: number - minimal value for field
    • max: number - maximum value for field
  • IpAddress
    • version: number (4|6) - IP address standard version
  • Number
    • min: number - minimal value for field
    • max: number - maximum value for field
  • String
    • minLength: number - mininal length of string
    • maxLength: number - maximum length of string
    • allowed: Array.<(string|RegExp)> - allowed values of this field

Roadmap

  • Add a typification by string
  • Add a typification by object with options and name
  • Increase the percentage of code coverage
  • Fix incorrect throwing invalid values
  • Add typescript definitions
  • Migrate tests to Ava