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

@americanairlines/simple-env

v1.0.5

Published

An intuitive, strongly typed, and scalable way to retrieve environment variables

Downloads

401

Readme

License: MIT Dependencies: 0 Build and Publish Total alerts Language grade: JavaScript codecov

simple-env

An intuitive, strongly typed, and scalable way to retrieve environment variables.

Installation

# Via npm
npm install @americanairlines/simple-env

# Via Yarn
yarn add @americanairlines/simple-env

Usage

Create a file to manage your environment variables (either added via arguments or a .env file loaded with dotenv):

// src/env.ts
import setEnv from '@americanairlines/simple-env';

export const env = setEnv({
  required: {
    nodeEnv: 'NODE_ENV',
    someRequiredSecret: 'SOME_REQUIRED_SECRET',
  },
  optional: {
    anOptionalSecret: 'AN_OPTIONAL_SECRET',
  },
});

Import env (or whatever you named your export) from your configuration file:

// src/index.ts
import env from './env';

const someRequiredSecret = env.someRequiredSecret;

Expected Behavior

| Env Var Type | State of Variable | Return Value/Behavior | | :-----------: | :---------------: | :------------------------------------- | | optional | set | ✅ Associated value returned as string | | optional | unset | ✅ undefined returned | | required | set | ✅ Associated value returned as string | | required | unset | 💥 Runtime error | | N/A - Unknown | ??? | 💥 Compilation error |

⚠️ Retrieving an unset and required env variable at the root of a file will throw an error and the app will fail to start.

Why use simple-env?

Autocomplete and Strongly Typed Keys are your new best friend! Using simple-env makes it easier for devs to utilize environment variables via autocomplete and requiring defined keys prevents typos and makes refactoring incredibly simple.

| Feature | simple-env | dotenv | env-var | | :--------------------------------- | :----------: | :------: | :-------: | | Zero Dependencies | ✅ | ✅ | ✅ | | JS/TS Support | ✅ | ✅ | ✅ | | Required vs Optional Specification | ✅ | ❌ | ✅ | | Autocomplete | ✅ | ❌ | ❌ | | Strongly Typed Keys | ✅ | ❌ | ❌ | | Single Location Refactor | ✅ | ❌ | ❌ | | Return Type Helpers | 🔜 | ❌ | ✅ | | Loads .env | 🔜 | ✅ | ❌ |

Let's see how some of the features above look in code:

// fileA.ts
const secret = process.env.SECRET;
// fileB.ts
const secret = process.env.SECRE;

// 👆 Brittle, susceptible to typos, weak types, and painful to refactor 😓

const env = setEnv({
  required: { secret: 'SOMETHING_SECRET' },
});

const secret = env.secret;
const secret = env.secre; // Property 'secre' does not exist on type '{ readonly secret: string; }'. Did you mean 'secret'? ts(2551)

// 👆 Compilation errors on typos, autocompletes as you type, and env var key can be modified without needing to refactor everywhere 👌

const env = setEnv({
  required: { requiredSecret: 'SOME_REQUIRED_SECRET' },
  optional: { optionalSecret: 'SOME_OPTIONAL_SECRET' },
});

env.requiredSecret.valueOf(); // No error
env.optionalSecret.valueOf(); // Object is possibly 'undefined'. ts(2532)

// 👆 Extremely strong typing - it knows what's required vs optional, which helps you catch bugs faster 🐞

Options

setEnv accepts multiple optional arguments:

Required Env Vars

// src/env.ts
import setEnv from '@americanairlines/simple-env';

export const env = setEnv({
  required: {
    nodeEnv: 'NODE_ENV',
    someRequiredSecret: 'SOME_REQUIRED_SECRET',
  },
});

Optional Env Vars

You can choose to only include optional env vars by passing in a single object:

// src/env.ts
import setEnv from '@americanairlines/simple-env';

export const env = setEnv({
  optional: {
    anOptionalSecret: 'AN_OPTIONAL_SECRET',
  },
});

Individual Assignment

If you want to set your env vars in multiple groups, make sure to destructure the optional env vars properly.

// src/env.ts
import setEnv from '@americanairlines/simple-env';

setEnv({
  required: {
    nodeEnv: 'NODE_ENV',
    someRequiredSecret: 'SOME_REQUIRED_SECRET',
  },
});

export const env = setEnv({
  optional: {
    anOptionalSecret: 'AN_OPTIONAL_SECRET',
  },
});

NOTE: if you choose to assign optional and required env vars individually, setEnv should only be done once for each or you will overwrite your previously defined values.

Contributing

Interested in contributing to the project? Check out our Contributing Guidelines.

Running Locally

  1. Install dependencies with npm i
  2. Run npm run dev to compile and re-compile on change
  3. Run npm link
  4. Navigate to another Node.js project and run npm link @americanairlines/simple-env

You can now use simple-env functionality within your project. On changing/adding functionality, the @americanairlines/simple-env package will update within your other project so you can test changes immediately.