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

envious-type

v1.1.0

Published

A library for working with environment variables.

Downloads

4

Readme

envious-type

A library for working with environment variables.

Basic usage

import envUtils from 'envious-type';

// Get the env utilities
const { env, validateEnv } = envUtils({ lazyValidation: true });

// Define your configs and validate that all values are ok.
const config = validateEnv({
    // Get a string. Must be defined in your environment.
    someApiUrl: env.SOME_API_URL.get(),

    // Get an integer. Separate defaults defined for when NODE_ENV is dev, test or prod.
    myApiPort: env.MY_API_PORT.integer().get({ dev: 8080, test: 18080, prod: 80 }),
});

// Use config with type safety, no explicit casting needed!
const port: number = config.myApiPort;

Translating different types

const myVar = env.MY_VAR
    .string()    // Treat as string.       Define MY_VAR=foo    [DEFAULT]
    .integer()   // Treat as integer.      Define MY_VAR=123
    .float()     // Treat as float.        Define MY_VAR=1.23
    .boolean()   // Treat as boolean.      Define MY_VAR=true or MY_VAR=false
    .array()     // Treat as string array. Define MY_VAR=val1,val2,val3
    .dict()      // Treat as dictionary.   Define key1:val1,key1:val2
    .optional()  // Treat as optional.     Can omit definition without default with no errors.
    .get()       // Return the type-casted and translated value!

// Example of custom translator 
const customVal = env.CUSTOM_VALUE.custom(value => new Person(value)).get(),

Handling configuration errors

By default errors are thrown immediately as they occur.

const { env } = envUtils();

// If MY_VAL is not defined in the env, an error is thrown immediately.
const myVal = env.MY_VAL.get(); 

Strongly suggested: If you'd like to collect all the errors in one go, you can combine the option lazyValidation with the validateEnv utility:

const { env, validateEnv } = envUtils({ lazyValidation: true });

// ✅ Safe
const config = validateEnv({
    // Returns an error if MY_VAL is not defined, but this is caught by validateEnv
    val: env.MY_VAL.get() 
})

// ❌ UNSAFE with lazyValidation=true!
const config = {
    // Can return an error, and the value is typed as a string. Might go unnoticed.
    val: env.MY_VAL.get() 
};

Defining environment names and defaults

By default, envUtils uses the value of NODE_ENV env variable to determine the current environment name. Do this if you want to override the environment name:

const { env } = envUtils({ envName: 'my-env' });

The defaults are applied for different environment names. By default the supported environment names for are dev, prod and test. These are autocompleted when defining default values.

If you prefer development, production and test, it's possible like this:

import envUtils, { LongEnvNames } from 'envious-type';

const { env } = envUtils<LongEnvNames>();

// ✅ OK
const value = env.MY_VAL.get({ development: 'foo' })

// ❌ TypeScript compilation error, 'dev' does not belong to LongEnvNames
const value = env.MY_VAL.get({ dev: 'foo' })

You can also provide any string-based type besides LongEnvNames and ShortEnvNames too, if you need it.

No matter what the type of the environment names is, you can always use a wildcard to set a default to all environments. This can be overridden for specific environemnts too

const value = env.MY_VAL.get({
    '*': 'foo', // Applied for prod, test and any others
    dev: 'bar'  // Applied for dev
});

Custom env object (browser usage)

If you are not using Node.js at runtime, you don't have access to the process.env object. In this case you need to define what object envUtils can get the environment from. Example:

// Define your environment on the window global, for example
const { env } = envUtils({ envObject: window._env_ }); 

Environment variable prefix

If all the environment variables you use have a prefix, you can use the envPrefix option.

const { env } = envUtils({ envPrefix: 'MY_APP_' }); 

const port = env.PORT.get(); // Return value from variable MY_APP_PORT