config-stack
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

config-stack

keep your configurations organized and stacked

Why Config Stack ?

When building a modern application, you don’t want to worry about configuration file formats; you want to focus on building awesome software. config-stack is here to help with that.

Features

  • Find, load, and parse a configuration file in JSON, TOML, YAML, HJSON, JSON5.
  • Provide a mechanism to set default values for your different configuration options.
  • Working with environment variables (This enables 12 factor applications)
  • Working with different deployment environments (development, qa, staging, production, etc.).
  • Find, load, and parse a command line
  • Support both typescript and javascript
  • inject configuration using typescript decorator
  • Type safety
  • Transform configuration value to a specified type
  • zero-dependency module

Example

  • typescript - load from file
  import * as configStack from 'config-stack'
  configStack.loadFile('./config.toml','test')
  // or to default environment
  configStack.loadFile('/config.toml')
  configStack.getString('key') // or any other function
  • javascript - load from file
  const configStack = require('config-stack')
  configStack.loadFile('./config.toml','test')
  // or to default environment
  configStack.loadFile('/config.toml')
  configStack.getString('key') // or any other function
  • typescript - bind all environment variables
  import * as configStack from 'config-stack'
  configStack.automaticEnv(); // ENV will be in lowercase "env"
  console.log(configStack.getString('env'));
  • javascript - bind all environment variables
  const configStack = require('config-stack')
  configStack.automaticEnv(); // ENV will be in lowercase "env"
  console.log(configStack.getString('env'));
  • typescript - bind specific environment variable
  import * as configStack from 'config-stack'
  configStack.bindEnv('env','development'); 
  console.log(configStack.getString('env','development'));
  • javascript - bind specific environment variable
  const configStack = require('config-stack')
  configStack.bindEnv('env','development'); 
  console.log(configStack.getString('env','development'));
  • typescript - set target environment
  import * as configStack from 'config-stack'
  configStack.set('key',2,'production')
  configStack.set('key',1,'test')
  configStack.setTragetEnv('test')
  // or from default environment
  console.log(configStack.get('key')); // print 2
  • javascript - set target environment
  const configStack = require('config-stack')
  configStack.set('key',2,'production')
  configStack.set('key',1,'test')
  configStack.setTragetEnv('test')
  // or from default environment
  console.log(configStack.get('key')); // print 2
  • typescript - bind object
  import * as configStack from 'config-stack'
  const obj = {
     nested: {
       key: "1"
    }
  };
  console.log(configStack.getObject('nested')) // {key: "1"}
  console.log(configStack.getNumber('nested.key')) // 1 
  console.log(configStack.getString('nested.key')) // '1' 
  • javascript - bind object
  const configStack = require('config-stack')
  const obj = {
     nested: {
       key: "1"
    }
  };
  console.log(configStack.getObject('nested')) // {key: "1"}
  console.log(configStack.getNumber('nested.key')) // 1 
  console.log(configStack.getString('nested.key')) // '1' 
  • typescript - working with command line
  import * as configStack from 'config-stack'
  const flag = configStack.Flag('usage','environment<test|qa|etc..>');
  flag.set(
    {
     name: 'name',
        usage: 'user name',
    
    }
  );
  flag.set(
    {
     name: 'birthday',
        usage: 'user birthday',
        alias: 'b'
    
    }
  );
  flag.set(
    {
     name: 'student',
        usage: 'is user student',
        boolean: true,
        default: false
    
    }
  );
  flag.parse();
  configStack.bind(flag);
  console.log(`user name ${configStack.getString('name')}`);
  console.log(`user birthday ${configStack.getDate('birthday')}`);
  console.log(`user student ${configStack.getBoolean('student')}`);
  • javascript - working with command line
  const configStack = require('config-stack')
  const flag = configStack.Flag('usage','environment<test|qa|etc..>');
  flag.set(
    {
     name: 'name',
        usage: 'user name',
    
    }
  );
  flag.set(
    {
     name: 'birthday',
        usage: 'user birthday',
        alias: 'b'
    
    }
  );
  flag.set(
    {
     name: 'student',
        usage: 'is user student',
        boolean: true,
        default: false
    
    }
  );
  flag.parse();
  configStack.bind(flag);
  console.log(`user name ${configStack.getString('name')}`);
  console.log(`user birthday ${configStack.getDate('birthday')}`);
  console.log(`user student ${configStack.getBoolean('student')}`);
./example.js --help 
./example.js -b 01-12-2003 --name john --student
  • typescript - working with custom binder
import * as configStack from 'config-stack'
class CustomBinder implements configStack.Binder {
  bind(set: configStack.SetFunction){
   set('key','value','test')
   set('key','value')
  }
}
bind(new Custominder())
  • javascript - working with custom binder
const configStack = require('config-stack')
class CustomBinder {
  bind(set){
   set('key','value','test')
   set('key','value')
  }
}
configStack.bind(new Custominder())
  • typescript - decorators
import * as configStack from 'config-stack'
class TestClass {
 @configStack.GetString('name')
 public name: string;
 
 @configStack.Get('any')
 public _any: any;
 
 @configStack.GetNumber('age')
 public age: number;
 
 @configStack.GetBoolean('valid')
 public isValid: boolean;
 
 @configStack.GetRegex('regex')
 public regex: RegExp;
 
 @configStack.GetDate('date')
 public date: Date;
 
 @configStack.GetObject('obj')
 public Object: Object;
 
 @configStack.GetArray('any_array')
 public array: any[];
 
 @configStack.GetBooleanArray('boolean_array')
 public boolean_array: boolean[];
 
 @configStack.GetStringArray('string_array')
 public string_array: string[];
 
 @configStack.GetNumberArray('number_array')
 public number_array: number[];
}
 

Installation

Install using npm:

npm install config-stack

Documentation

Roadmap

  • support remote config like etcd
  • laze automatic env
  • load config-stack schema
  • environment variables prefix

Contributing

I welcome any contributions, features, enhancements, and bug-fixes. File an issue on GitHub and submit a pull request.

License

Type config-stack is 100% free and open-source, under the MIT license. Use it however you want.

Package Sidebar

Install

npm i config-stack

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

53.3 kB

Total Files

21

Last publish

Collaborators

  • hajyahya