npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan HefnerΒ  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

@jondotsoy/envconfig

v1.4.3

Published

Envconfig is a helper zero-dependency module to load environment values with formatter πŸ˜‰.

Downloads

9

Readme

envconfig

Envconfig is a helper zero-dependency module to load environment values with formatter πŸ˜‰.

const e = envconfig({ env: process.env })

const port: number = e('PORT', 'number') ?? 3000;
const ssl: boolean = e('SSL', 'boolean') ?? false;

Features:

  • Support to Deno
  • Default load config from process.env
  • Formatter value using option [type]: e('ENV', { type: Types }) => Types | undefined or e('ENV', Types) => Types | undefined
    • Formatters supported:
      • string formatter (Default): Ex. e('ENV') => string | undefined
      • number formatter: Ex. e('ENV', { type: 'number' }) => number | undefined
      • bigint formatter: Ex. e('ENV', { type: 'bitint' }) => bitint | undefined
      • boolean formatter: Ex. e('ENV', { type: 'boolean' }) => boolean | undefined
      • Custom formatter: Ex. e('ENV', { type: v => new Date(v) }) => Date | undefined
  • Typescript support
    • Samples:
      • e('ENV', 'number') => number | undefined
      • e('ENV', v => new Date(v)) => Date | undefined
  • Assert values
    • Samples:
      • e('ENV', { type: 'number', required: true }) => number
      • e('ENV', { type: v => new Date(v), required: true }) => Date

How to use

Install dependency @jondotsoy/envconfig

# With npm
$ npm i @jondotsoy/envconfig

Usage with Deno

Import the module using the next url https://unpkg.com/@jondotsoy/envconfig@latest/index.ts

import envconfig from 'https://unpkg.com/@jondotsoy/envconfig@latest/index.ts';

const e = envconfig({ env: Deno.env.toObject() });

Usage

Require and configure envconfig.

// my_configs.ts
import envconfig from '@jondotsoy/envconfig';

const e = envconfig();

Use your instance e in your code. For example, using PORT env if exists or assign a default value.

const port: number = e('PORT', 'number') ?? 3000;

Options envconfig([{ env?, prefix?, sufix? }]): e(key: string)

env

  • Optional
  • Type:
declare type env = { [k: string]: string }
  • Default: process.env

Specify origins data, the method e() is using this value to read and transform values.

const e = envconfig({ env: { PORT: '6000' } });

const port: number = e('PORT', 'number', true);

prefix

  • Optional
  • Type:
declare type prefix = string

Used to simplify the reading values that contain prefixes on all values required. Its util to complex strategies.

const e = envconfig({
    env: { AWS_ACCESS_KEY_ID: 'a123', AWS_SECRET_ACCESS_KEY: 'abcdeaw12343****' },
    prefix: 'AWS_',
});

const accessKeyId: string = e('ACCESS_KEY_ID', 'string', true);
const secretAccessKey: string = e('SECRET_ACCESS_KEY', 'string', true);

sufix

  • Optional
  • Type:
declare type sufix = string

Used to simplify the reading the values that can have a suffix. It has preferer the key with the suffix if not exists the key with the suffix this to find a key without the suffix.

Demo 1

const e = envconfig({
    env: { API_KEY: 'abc123', API_KEY_STAGING: 'def456' },
    sufix: '_STAGING',
});

const apiKey: string = e('API_KEY', 'string', true);

assert(apiKey).to.be.equal('def456');

Demo 2

const e = envconfig({
    env: { API_KEY: 'abc123', API_KEY_PRODUCTION: 'def456' },
    sufix: '_STAGING',
});

const apiKey: string = e('API_KEY', 'string', true);

assert(apiKey).to.be.equal('abc123');

Options e(key: string[, { type?: Types, required?: boolean }])

  • Other Syntax: e(key: string[, type: Types[, required: boolean]])

key

  • required
  • Type:
declare type key = string

It's a string that is a referrer to the key in the object env.

const key = 'PORT'
const env = { PORT: '1234' }

const e = envconfig({ env })

const port = e(key) // '1234'

type

  • Optional
  • Type:
declare type Types = 'number' | 'boolean' | 'string' | 'bigint' | ((v: string) => any);

This param is optional, a string or a function. This is used to indicate how is interpreted the value found. If the value found is undefined that value will not be interpreted.

If is equal to string, the value found is transformed to String.

const port = e('PORT', 'string')

assert.ok(typeof port === 'string')

If is equal to number, the value found is transformed to Number.

const port = e('PORT', 'number')

assert.ok(typeof port === 'number')

If is equal to boolean, the value found is transformed to Boolean.

const verbose = e('VERBOSE', 'boolean')

assert.ok(typeof verbose === 'boolean')

If is equal to bigint, the value found is transformed to BigInt.

const memmax = e('MEMMAX', 'bigint')

assert.ok(typeof memmax === 'bigint')

If is a function, it is used to transform value, only if is not undefined the value found.

Demo 1

const toDate = (v) => new Date(v)

const openService = e('OPEN_SERVICE', toDate)

assert.ok(openService instanceof Date)

Demo 2

const base64ToBuffer = (v) => Buffer.from(v, 'base64')

const keySecret = e('KEY_SECRET', base64ToBuffer)

assert.ok(keySecret instanceof Buffer)

required

  • Optional
  • Type:
declare type required = boolean

if is equal to true the value found will have be not undefined else it throw a error.