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

@carnesen/p-env

v1.0.0

Published

A TypeScript library for parsing process.env type-safely

Downloads

33

Readme

build status badge

An isomorphic TypeScript library for parsing process environment variables type-safely

Usage

Install this package as a dependency in your library or application:

npm install @carnesen/p-env

Here's an example of how to use it in your code:

import { p } from '@carnesen/p-env';

class AppEnv extends p.env({
   APP: p.string({ default: 'my-app', optional: true }),
   DATE: p.date({ default: new Date() }),
   JSON: p.json({ default: [] }),
   MODE: p.stringOneOf({
      default: 'dev',
      values: ['dev', 'qa', 'prod'] as const,
   }),
   PORT: p.port({ default: 8080 }),
   SECRET: p.string({ default: 'abc123', secret: true }),
   STRICT: p.boolean({ default: false, optional: true }),
}) {}

// The `AppEnv` constructor parses `process.env` and assigns the
// parsed values to the new instance. Suppose STRICT is
// set to "1" (or "yes" or "true") in the process environment.

const appEnv = new AppEnv({ logger: console });
// APP="my-app" (default)
// DATE="2023-02-12T16:45:24.607Z"
// JSON=[] (default)
// MODE="dev" (default)
// PORT=8080 (default)
// SECRET=<redacted> (default)
// STRICT=true ("1")

// ^^ Parsed values for fields with `secret: true` are redacted in logs and
// error messages

// ^^ A log with (default) means a default value was used. Otherwise the logged
// line has ("<raw value>") where <raw value> is the string value from the
// process environment.

console.log(appEnv);
// AppEnv {
//   APP: 'my-app',
//   DATE: 2023-02-12T16:45:24.607Z,
//   JSON: [],
//   MODE: 'dev'
//   PORT: 8080,
//   SECRET: 'abc123',
//   STRICT: true
// }

default and optional

Every environment variable in the schema must have a default value. A environment value always takes precedence over the default value. If an environment value is not provided, two factors determine whether the default value is used:

| optional | mode | use default? | | :------: | :---------: | :----------: | | true | any | yes | | any | development | yes | | false | production | no |

When NODE_ENV is "production" and no environment value is provided for a non-optional field, the parser throws a PEnvError.

API

This primary documentation for this package's API is its rich, strict types along with their associated TypeDoc strings viewable in your IDE by hovering over a symbol. All exports are named. The primary export is the p namespace object import { p } from "@carnesen/p-env".

env

p.env: Takes a single argument defining your environment object's schema and returns an anonymous class whose constructor parses process.env and assigns the parsed values to the new instance. We recommend extending the anonymous class as a named one e.g. class MyEnv extends p.env({...}) {}, but you're welcome to use the anonymous one directly as e.g. const MyClass = p.env({...}). The class constructor takes an optional config object argument new MyEnv({ logger, loader }). If logger.log is provided, the parsed values are logged. If logger.error is provided, parse/validation errors are logged. Use the loader property to define a custom process.env loader. This is mostly useful for unit testing. config can also/instead be passed as the second argument of p.env. If a configs is provided to p.env and to the class constructor, the class constructor one takes precedence.

Environment variable factories

A p-env schema declares a name (e.g. PORT) and a type for each environment variable. The environment variable factories p.boolean, p.string, etc. take a config object with a mandatory property default and optional properties optional and secret. The meaning of optional is described above. If secret is true, the value shows as "" in logs and error messages. Some of the environment variable types have other optional properties too.

boolean

p.boolean: Factory for boolean-valued environment variables. "1", "true", "yes" (and their upper-case/white-spaced variations) parse to true. "0", "false", and "no" and their upper-case/white-spaced variations) parse to false.

number

There are three factories for number valued environment variables

p.number: Parses the environment value as a number. NaN is not allowed value. Has optional configuration properties maximum (default +Infinity) and minimum (default -Infinity) defining the allowed range for the number value.

p.integer: Same as p.number but the parsed number must be an integer. Equivalent to p.number with { integer: true }.

p.port: Same as p.integer with minimum: 0 and maximum: 65535. Optional config properties maximum and minimum further restrict the allowed range.

string

p.string: Factory for string-valued environment variables. The string parser returns the environment value as-is.

stringArray

p.stringArray: Factory for string[]-valued environment variables. The stringArray parser splits the environment value on ,.

stringOneOf

p.stringOneOf: Factory for environment variables that must be one of the allowed values provided.

Custom types

To create your own custom environment variable type, extend PEnvVar using the built-in classes (PEnvBoolean etc.) as your guide.

More information

If you encounter any bugs or have any questions or feature requests, please don't hesitate to file an issue or submit a pull request on this project's repository on GitHub.

Related

  • @carnesen/cli: A library for building command-line interfaces in Node.js and the browser

License

MIT © Chris Arnesen