stages-config 🔑
Easy and safe environment stages configuration for develop, staging, production...
stages-config
?
Why use The environment variables are great, I really like to use libraries like dotenv
to store my secret keys in different env files for each environment (develop, staging, production...) but this doesn't allow me to be sure that a key is correctly configured and we tend to save more than necessary in these files, which ends up making them a little unusable.
With stages-config
you have full control of your environment variables, you can configure non-critical keys in simple javascript per stage or with default values. And the possibility of error due to forgetting to set some variable in some environment is reduced to zero.
Installation
Install with npm or with yarn.
npm install @woonivers/stages-config --save
yarn add @woonivers/stages-config
Usage
Basically, you need to follow these steps:
- Define a
schema
. Each setting in the schema has some properties (format, env, default...).
const schema = {
jwt: {
format: 'string',
env: 'JWT_SECRET'
},
port: {
format: 'int'
},
firebase: {
key: {
format: 'string'
},
id: {
format: 'int'
},
url: {
format: 'url',
default: 'https://firebase.com'
}
}
}
- Setup your
stages
The values for the settings in the different environments you have (develop, staging, production...)
const stages = {
develop: {
port: 1040,
firebase: {
key: 'firebase_local',
id: 171721
}
},
production: {
port: 7777,
firebase: {
key: 'firebase_prduction',
id: '646464'
}
}
}
You should also use this package along with other tools that allow you to set environment variables for more sensitive data.
# .env
JWT_SECRET: mysupersecretindevelop
# .env.staging
JWT_SECRET: mysupersecretinstaging
# .env.production
JWT_SECRET: mysupersecretinproduction
- Provide your active stage. Select which of your stages is the active
const stage = {
env: 'STAGE',
default: 'develop'
}
You must define an environment variable with the active stage (develop, staging, production...).
# .env
STAGE: develop
# .env.staging
STAGE: staging
# .env.production
STAGE: production
- Export your config
import stageConfig from '@woonivers/stages-config'
const config = stageConfig({
schema,
stages,
stages
})
export default config
- Use it in your app!
import config from './config'
const firebaseId = config.get('firebase.id')
console.log(firebaseId) // 171721 (from stage values)
The value in firebaseId
will be set with a Precendence order
The schema
Each setting in the schema has the following possible properties:
format
)
Format (Specifies a format. When you want to get a setting, the format check is performed and if it fails an error is thrown.
"*"
(default): Any value is accepted"string"
"int"
"url"
"email"
env
)
Environment variable (Used for more sensitive variables, such as third party API KEYS services. If you provide a env
property, stages-config
will look for that setting in process.env
jwt: {
format: 'string',
env: 'JWT_SECRET'
},
Based in this piece of schema,
stages-config
will look inprocess.env.JWT_SECRET
for thejwt
setting when called.
default
)
Default value (If the value of a setting is the same for all stages and it is not sensitive data, or if you always want to have a fallback value, the property default
is useful for you
Precedence order
When merging configuration values from different sources, stages-config follows precedence rules. The order, from highest to lowest, is:
- Stages values
- Environment variables
- Default values
Inspiration
This package is heavy inspired in node-convict
.
License
MIT