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

readtheroom

v2.0.0

Published

Environment variable readers and defaults configuration for TypeScript

Downloads

35

Readme

readtheroom

banner

A small, versatile TypeScript library for declaratively handling configuration settings from environment variables with:

  • Type safety
  • Seamless support for import.meta.env
  • Compatibility with frontend build imports
  • Zero dependencies 😎

readtheroom gives you helpers to read, validate, set environment-specific defaults, and even convert types in a single const statement that you can easily export from a single file.

Getting Started

npm install readtheroom

Once installed, import it like so:

import { Env, read } from 'readtheroom'

Examples

In the comments below, "present" means a non-empty string, and "not present" means undefined or an empty string.

read

import { read } from 'readtheroom'

//
// Reads from process.env.DB_NAME
// Throws if not present.
//
export const dbName = read('DB_NAME')

//
// Reads from process.env.DB_USER
// If not present, uses 'default_user' as its value.
//
export const dbUser = read('DB_USER', 'default_user');


//
// Reads from process.env.MAX_CONNECTIONS
// If present, converts the string value into a number.
// If not present, throws.
//
export const maxConnections = read('MAX_CONNECTIONS', Number);

//
// Same as above, but with a default value.
// Note that you must provide the default value as a string,
// so that your converter function retains a consistent input type.
//
export const maxConnections2 = read('MAX_CONNECTIONS', '4', Number);

Env

Env allows you to:

  1. Validate your currently running NODE_ENV.
  2. Choose different config values based on that NODE_ENV.
import { read, Env } from 'readtheroom'

//
// Reads process.env.NODE_ENV
// If present, validates that it is either 'development' or 'production'
// If not present, defaults to 'development' (this is the default default)
//
export const env = Env(['test', 'development', 'production'])

//
// Same as above, except defaults to 'test' instead of 'development'
//
export const env2 = Env(['test', 'development', 'production'], 'test')


//
// Reads process.env.DATABASE_URL *unless* in test environment.
// Wraps the default value in a thunk so `read` doesn't throw unnecessarily.
//
export const dbConfig = env.branch(
  () => read('DATABASE_URL'),
  {
    test: 'postgresql://postgres:[email protected]:54322/postgres'
    //
    // Could also be this if you wanted:
    //
    // test: () => read('TEST_DATABASE_URL')
  }
)