envkey / envkey-node

EnvKey's official Node.js client library
https://www.envkey.com
MIT License
49 stars 9 forks source link

boolean env values? #9

Closed PatrickJS closed 6 years ago

PatrickJS commented 6 years ago

yo, we also manage some basic configuration in envkey like urls depending on the env or even booleans. Is there a way for envkey to parse these boolean keys so we won't have to write the code String(process.env.DISABLE_TRACKING) === 'true' or at least a way to hook into applyVarsToEnv so we can run the check ourself

I ended up doing this

const env = applyVarsToEnv(envkeyLoader.fetch({
  dotEnvFile: '.env'
}))

// ... client bundler
  {
    env: env
  }
// ...

function applyVarsToEnv(vars){
  var varsSet = {}
  for (k in vars) {
    var val = process.env[k] || vars[k]
    if (val === 'true') {
      val = true
    } else if (val === 'false') {
      val = false
    }
    if (process.env[k] && process.env[k] !== vars[k]) {
      console.log('ENVKEY: using local value for ' + k)
      varsSet[k] = val
    } else {
      process.env[k] = val
      varsSet[k] = val
    }
  }
  return varsSet
}
danenania commented 6 years ago

Yo! I could definitely see this being useful, but I think converting automatically would be a bit too magical (since environment variables are expected to be strings), and it could be a breaking change for people relying on the current behavior. I'd be fine with exposing it as an option though for custom loading -- something like:

import {load as envkeyLoad} from 'envkey/loader'

envkeyLoad({ parseBooleans: true })

It might also be cool to allow parsing numbers, dates, or even JSON objects/arrays like this.

PatrickJS commented 6 years ago

yeah that would be awesome as a flag 👍