type-friendly utility for reading environment variable.
npm install @cm-ayf/readenv
This package exports one function: readenv
:
// typescript
import readenv from 'readenv';
or
// javascript
const readenv = require('readenv');
readenv
takes one argument, which specifies how you need to read environment variable. For example:
const env = readenv({
TOKEN: {},
NODE_ENV: {
default: 'development'
},
apiKey: {
from: 'API_KEY'
}
});
Say you have dotenv.condig()
ed with .env
below:
# .env
TOKEN=token_keyboard_cat
API_KEY=api_key_keyboard_dog
then you will get:
const env = {
TOKEN: 'token_keyboard_cat',
NODE_ENV: 'development',
apiKey: 'api_key_keyboard_dog'
}
Additionally, this env
has static type like:
const env: {
TOKEN: string;
NODE_ENV: string;
apiKey: string;
};
so that you can use it type-safe.
If an environment variable was missing with key that has no default value, it throws error. The error will tell you all environment variables that were missing.
Input:
options
: Object. The return object will inherit its keys. Each key can be configured withOption
.
Returns: Object. Inherits keys from options
. Each value will always be string
.
Throws: Error
object that tells us all environment variables missing.
interface Option {
default?: any;
from?: string;
parse?(src): any
}
Fields:
option.default
- type:
any
- Default value for the key.
readenv()
never throws error about key withdefault
.
- type:
option.from
- type:
string?
- Environment variable name. Uses key if omitted.
- type:
option.parse
- type:
((src: string) => any)?
- Applied after environment variable was read before return. Returns string value if omitted.
- type:
Examples are here;
source code from twitter.ts.
works very well when new Twitter()
:
const env = readenv({
consumer_key: { from: 'CONSUMER_KEY' },
consumer_secret: { from: 'CONSUMER_SECRET' },
access_token_key: { from: 'ACCESS_TOKEN_KEY' },
access_token_secret: { from: 'ACCESS_TOKEN_SECRET' },
});
const twitter = new Twitter(env);