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

1.7.2 • Public • Published

Config Dug

Build status coverage codecov TypeScript 3.7.2

Config Dug

Config loader with support for AWS Secrets Manager.

Usage

Installation

yarn npm
yarn add config-dug npm install config-dug

Create your config files

config-dug looks in several places for your config, including config files in your project, environment variables and AWS Secrets Manager. config-dug allows you to write your config files in either TypeScript or JavaScript. You are expected to export a default object from your config file:

// config.default.ts
export default {
  API_ENDPOINT: 'https://api.kanye.rest/',
};
// config.default.js
module.exports = {
  API_ENDPOINT: 'https://api.kanye.rest/',
};

Environment specific config files are loaded based on the value of the APP_ENV or NODE_ENV environment variables. If APP_ENV is present it will take precedence over NODE_ENV.

Settings from these different sources are merged together into a single config object in the following order:

  1. config.default.{ts|js}
  2. config.${APP_ENV|NODE_ENV}.{ts|js}
  3. config.${APP_ENV|NODE_ENV}.local.{ts|js}
  4. config.local.{ts|js}
  5. AWS Secrets Manager
  6. Environment variables

By default your config files need to be placed in the root directory of your project. If you want to keep config files in a different directory see Customizing Config Loading.

Import config

Import config-dug anywhere in your code where you want to access your config. All of your settings are available on the imported object:

// app.ts
import config from 'config-dug';

console.log(config.API_ENDPOINT);
// app.js
const config = require('config-dug').default;

console.log(config.API_ENDPOINT);
// https://api.kanye.rest/

⚠️ You must use require('config-dug').default in JavaScript files. If you exclude .default Config Dug will not work.

Using AWS Secrets Manager

In order to use AWS Secrets Manager you have to add a AWS_SECRETS_MANAGER_NAME or awsSecretsManagerName setting to your config that specifies the names of the secrets to look up:

// config.default.ts
export default {
  AWS_SECRETS_MANAGER_NAME: 'production/myapp/config',
  API_ENDPOINT: 'https://api.kanye.rest/',
};

If you need to read from multiple secret buckets, AWS_SECRETS_MANAGER_NAMES takes a comma separated list to allow connection to multiple secrets in AWS Secrets Manager. Each secret from the list is evaluated in order mean that if a specific key appears in two secrets the value will be overwritten by the last secret in the list.

// config.default.ts
export default {
  AWS_SECRETS_MANAGER_NAMES: 'production/myapp/config,production/myapp/another-config',
  API_ENDPOINT: 'https://api.kanye.rest/',
};

In addition to specifying the secret name you can also provide a region using the AWS_SECRETS_MANAGER_REGION or awsSecretsManagerRegion setting. The connection timeout in milliseconds can also be specified using the AWS_SECRETS_MANAGER_TIMEOUT or awsSecretsManagerTimeout setting:

// config.default.ts
export default {
  AWS_SECRETS_MANAGER_NAME: 'production/myapp/config',
  AWS_SECRETS_MANAGER_REGION: 'us-west-2',
  AWS_SECRETS_MANAGER_TIMEOUT: 2000
  API_ENDPOINT: 'https://api.somecompany.com'
};

The default region is us-east-1 and the default connection timeout is 5000ms.

Config Dug will warn if it detects invalid config values. Invalid values include:

  • undefined
  • null
  • the string 'undefined'
  • an empty string

This package uses the aws-sdk internally. Refer to their documentation for information about authentication, configuring a default region and configuring access control for AWS Secrets Manager.

Advanced

Customizing Config Loading

If you want to load config files from a directory other than the project root you can import the loadConfig function and use it directly.

import { loadConfig } from 'config-dug';

loadConfig('config');

This will import your config files from the config directory. The path you specify must be relative to your project root.

Debugging

config-dug uses the debug library. To print debug messages for config-dug set DEBUG=config-dug.

Contributing

Running Tests

  1. Fork this repo
  2. Clone the forked repo
  3. Install dependencies: npm install OR npm i
  4. Run tests: npm run test

Publishing

  1. Update the version in package.json
  2. Add a CHANGELOG entry
  3. Commit your changes
  4. Run npm pack to see what will be published then delete the .tgz file that was created
  5. Run npm publish
  6. Create a release on GitHub. Use the version as the tag and release name. For example for version 1.0.0 the tag and release name would be v1.0.0.

Credits

This project was inspired by config3 and config4.

Package Sidebar

Install

npm i config-dug

Weekly Downloads

9,044

Version

1.7.2

License

MIT

Unpacked Size

17.2 kB

Total Files

9

Last publish

Collaborators

  • neo-ife-ojikutu
  • neo-will-parker
  • neo-paula-grangeiro
  • gaulford
  • danielafekhume-neo
  • jashish_m
  • neo-lior-ben-shahar
  • neo-diego-nunes
  • tim-neo2
  • antonio.canabrava
  • neo.eduardoyuidy
  • neoricardogcolombo
  • kirstyrobertson
  • neo-oleksandr-yanchenko
  • neo-barry-li
  • desmond-maloney
  • harmeetsalech
  • neo-elgiz-abbasov
  • nikita-syrotenko
  • neo-bclausi
  • dustinn1235
  • neo-prerakkumar-patel
  • neo-peter-spangler
  • neo-rivan-mota
  • neo-daniel-brennan
  • neo-matt-felice
  • neo-rameez-virji
  • matt-armstrong
  • rajanatneo
  • josephma
  • neo-fredy
  • neo-zhaoquan-zhang
  • alejandrogarbi
  • ronellgarcia
  • gui.martins
  • edward.nazarov
  • annakumova
  • leah-is-offline
  • neo-shruti-goyal
  • felipe.minetto
  • neo-kevin-hillard
  • neo-ahmed-jama
  • olgavozniuk
  • tunde-neo
  • neo-shubham-patel
  • biancaballena
  • neo-sacha-cuna
  • jadonduby
  • msalama
  • ani-samajpati-neo
  • ishmeet.rayat
  • neo-bryce-brandford
  • neo-raheel-junaid
  • neo-ricardo-gomez
  • neo-lawrence-liu
  • edwardkruger
  • neobot1
  • neo-dakota-chatt
  • lucasjohannson
  • neo-travis-friesen
  • mike.himbeault-neo
  • raul.ribeiro
  • neo-shraddha
  • anhtranneo
  • lucachamecki
  • amievishwa
  • neo-matias-wengiel
  • ini-a
  • neo-ahmed-seifelnasr
  • rhullyam
  • das-boss
  • kennethcolina
  • feminachacko
  • marvindeleon
  • neo-miguel-castro
  • neo-dione-silva
  • hanna.tkachenko
  • nico-connor
  • roman.mnatsakanian
  • riley-pickerl
  • thomas-llewellyn-neo
  • briancruz
  • amjad-mahfoud-neo
  • neoauderesantos
  • nitin.madan
  • mohpor
  • cghenneneo
  • punit.patel
  • brayden_sc_neo
  • anna_benemanskaia
  • hrishikeshkale
  • kyle-elyk-neo
  • michelepiperni
  • neo-youre_pena
  • neo-michal-kowal
  • niconeiman
  • neodiegobeltran
  • stburke
  • marcelo.matos
  • matheusicaro
  • divanshu
  • maiahneo
  • kimberlytranneo
  • rjberry
  • neo-carlos-pieren
  • mohamedalhammoud
  • neo-hasanpreetsingh
  • irakitnykh
  • jcavalcanti
  • stephanie-wang-neo
  • kaelanneofinancial
  • vinicius-barcelos
  • benx-neo
  • mhowitt-neo
  • malcolm-neo
  • neo-seol-kim
  • brentcam
  • teena_
  • deankim
  • herculesjr
  • matthew.belford
  • rishbar
  • neo-baonguyen
  • caio.andrade
  • scott.wiedemann
  • thomawstrahn
  • luan.le
  • dennisbarthel
  • lavanya.mohan
  • leosbelsotomayor
  • dhruvang
  • neo-rodrigoraval
  • joeljwood
  • nahom-neo
  • stefan.amyotte
  • timothy.couch.neofinancial
  • scottkayeneo
  • omolara
  • ar-ies
  • cjnewman
  • vidalrmrz
  • rnetto46
  • iansu
  • darrenpicard25
  • maxgoo
  • stschmaltz
  • usernameseb
  • eric.zaporzan.neo
  • lucasparreira
  • lewishorwood
  • craigsiemens
  • diegobmy
  • joelsimpson
  • jooh-lee
  • kasrababaei
  • mattlabarre
  • u2v22
  • johnclendvoy
  • demaestro
  • sudeep.sidhu
  • daylan.law
  • neo-stephen
  • neogibson
  • beraki
  • vrochamaaia
  • d_fenniak
  • mato-gallardo
  • adamtranquilla
  • nehasingh1801
  • lucaslongarini
  • erozonachi
  • faddoul
  • lovekesh1
  • jeffrey-dugas-neo
  • pichatelli
  • npoltz-neo