1.2.0 • Published 2 years ago

@cm-ayf/readenv v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@cm-ayf/readenv

type-friendly utility for reading environment variable.

Setup

npm install @cm-ayf/readenv

How to Use

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.

API Document

readenv(options: { [key: string]: Option })

Input:

  • options: Object. The return object will inherit its keys. Each key can be configured with Option.

Returns: Object. Inherits keys from options. Each value will always be string.

Throws: Error object that tells us all environment variables missing.

Option

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 with default.
  • option.from
    • type: string?
    • Environment variable name. Uses key if omitted.
  • option.parse
    • type: ((src: string) => any)?
    • Applied after environment variable was read before return. Returns string value if omitted.

Use Case

Examples are here;

twitter-v2

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);
1.2.0

2 years ago

1.1.0

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago