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

@6cat/env

v0.3.2

Published

Create javascript objects from environment variables. If you are using TypeScript, the result is fully typed as well.

Downloads

256

Readme

@6cat/env

Create javascript objects from environment variables. If you are using TypeScript, the result is fully typed as well.

Warning While environment variables names can theoretically contain any character besides =, there are many tools that do not support anything besides A-Za-z0-9_ and it's therefore recommended that you limit yourself to those characters.

Usage

import { parseEnvironment, string, number } from "@6cat/env";

const env = parseEnvironment({
  auth: {
    bearerToken: string({ default: "" }),
    requestLimit: number,
  },
});

console.log(env.auth.bearerToken);

This would read and log the value from environment variables named AUTH_BEARER_TOKEN, AUTH_BEARERTOKEN, auth_bearerToken, or auth.bearerToken (this last form is discouraged).

parseEnvironment

Read environment variables

env: Environment to use (default: process.env)

profiles: Optional profiles

If specified, environment variables that start with these values will override any other keys. The first array value has the highest priority. Profile variables can use the optional prefix _ or %.

For example, this allows for configuring multiple environments:

let env = parseEnvironment({ endpoint: string }, { profiles: ["dev"] });
ENDPOINT=http://service.prod
_DEV_ENDPOINT=http://service.dev
_TEST_ENDPOINT=http://localhost:2000

argv: Whether command line arguments will be parsed as well

If true, in addition to environment variables, command line arguments will be parsed as well:

node app.js --service.name=foo -Dservice.version=v1
let env = parseEnvironment(
  { service: { name: string, version: string } },
  { argv: true }
);

If a string is configured, it will be prefixed to any argument provided. (note that this will effectively disable profile support for command line arguments)

node action.js --name=foo
let env = parseEnvironment({ input: { name: string } }, { argv: "input" });

Types

The following types are included in the package.

array

Read an array

Array values are comma separated by default:

EXAMPLE_ARRAY=value,other value

If you want to split values into multiple environment variables, you can use indexed entries:

EXAMPLE_ARRAY_0=value
EXAMPLE_ARRAY_1=other value
exampleArray[2]=alternative syntax

You can create arrays of complex objects, but it that case you can only use indexed entries:

let env = parseEnvironment({ values: array({ url: string }) });
values_0_url=https://foo
values_1_url=https://bar

boolean

Read a boolean

Similar to YAML, supported formats are true, y, yes, and on.

default: A default value in case no environment variables is defined (default: undefined)

required: Whether the value is required, in which case an error will be thrown if no value is provided (default: false)

number

Read a number

default: A default value in case no environment variables is defined (default: undefined)

required: Whether the value is required, in which case an error will be thrown if no value is provided (default: false)

record

Read a record

Strings will always be converted to lowercase when used as keys, to avoid surprises when switching keys between upper and lowercase.

For example,

const env = parseEnvironment({ values: record(string, string) });
console.log(env.values["test"]);

Will read environment variables VALUES_TEST and values.test.

string

Read a string

default: A default value in case no environment variables is defined (default: undefined)

required: Whether the value is required, in which case an error will be thrown if no value is provided (default: false)

trim: Whether whitespace will be trimmed from the value (default: true)

trailingSlash: Whether a trailing slash is automatically added or removed from the value (default: no change)

Creating custom types

The easiest way to add support for a new type is by using the Parser.forValue method. For example, if you want to parse URLs:

import { parseEnvironment, Parser } from "@6cat/env";

function url() {
  return Parser.forValue((value, ctx) => (value ? new URL(value) : undefined));
}

const env = parseEnvironment({
  endpoint: url,
});

If no environment variables matches, a value of undefined will be passed to the parser, allowing you to set a default value.