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

jet-env

v1.1.4

Published

Simple, TypeScript zero-dependency environment validation/initialization tool.

Downloads

570

Readme

Simple, TypeScript first, zero-dependency environment variable validation/initialization tool.

Why jet-env

  • Small, quick, convenient zero-dependency.
  • You can learn this tool in 5 minutes.
  • Works client or server side.
  • Automatically constructs environment-variable names from object keys.
  • Sets up a completely type-safe object to hold all your environment variables.
  • Includes 4 build-in validator-functions: (bool, num, date and str)
  • transform() wrapper for validator-functions includes if a value needs to be modified first.
  • Easier to learn and smaller han envalid.

Overview

This library does not load environment variables, but it provides a single function that will loop through an object and assignment an environment variable to each one. If the environment variable is undefined or the incorrect type, then an error will be thrown.

The environment variable name will be constructed using the keys on the object. PascalCase names will automatically be converted to UPPER_SNAKE_CASE. So if your environment variable is NODE_ENV then the key should be NodeEnv on the object key. This will work for nested objects tool. The parent-object's key name will be prepended to whatever the child-properties's key is.

If you want to override the behavior for naming the environment variables, you can pass an array instead of a function. The first value in the array must be a string and will be used to pull the environment variable (no prepending is done). The second value must be a validator-function

If you want your environment variable value transformed before validation, there is a helper function export that your can wrap your validator function with. There are also 4 default validator-functions (bool, num, date and str) that come packaged by default.

Notes on validators:

  • An empty string will not satisfy the str function.
  • date will convert any valid date value (string or number) to a Date object and make sure it's valid.
  • For boolean types, there are several different variations which will satisfy the built-in bool function:
    • false/true, case doesn't matter
    • 0: false
    • 1: true
    • no: false case doesn't matter
    • yes: true case doesn't matter

Quick Glance

import jetEnv, { bool, date, num, transform } from 'jet-env';

const Env = jetEnv({
  NodeEnv: str, // NODE_ENV
  IsLocal: bool, // IS_LOCAL
  Port: num, // PORT
  BackEndUrl: str, // BACK_END_URL
  FrontEndUrl: str, // FRONT_ENV_URL
  BypassDbConn: transform(JSON.parse, (arg) => arg === true),
  S3BucketName: ['S3_BUCKET_NAME', str],
  S3BucketUrl: str, // S3_BUCKET_URL
  S3BucketExp: date, // S3_BUCKET_EXP
  Aws: {
    S3Credentials: {
      AccessKeyId: str, // AWS_S3_CREDENTIALS_ACCESS_KEY_ID
      SecretAccessKey: str, // AWS_S3_CREDENTIALS_SECRET_ACCESS_KEY 
    },
  },
});

Overriding Default Behavior

  • The default behavior for formatting variable names, fetching values, and handling error messages can all be overridden by an optional options parameter as the second argument to jetEnv. Here's a overview of the options object:
{
  getValue?: (property: string, key?: string) => unknown; // Fetch the value, default is "=> process.env[property]"
  variableNameFormatter?: (name: string) => string; // Set your own custom function for formatting environment-variable names from object keys.
  onError?: (property: string) => void; // Change what happens when a validator-function fails (currently throws an error)
}

That's It!

Happy Coding :)