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

ts-app-env

v1.5.0

Published

A config library for TypeScript.

Downloads

1,160

Readme

ts-app-env

A config library for TypeScript.

This is a small library for encapsulating access to an application's config.

Currently just environment variables.

Goals

The main goals are:

  1. Declare all of an application's config in a single place.

    This helps understanding and maintaining an application's config vs. grepping for process.env.FOO calls spread throughout the codebase.

  2. Perform a simple "all of the config values are available" sanity check immediately on application boot.

    This prevents an application booting and then ~seconds/minutes later blowing up because a process.env.VERY_IMPORTANT_SETTING is not available.

Non-Goals

  • Loading config from disk.

    This library currently doesn't try to load YAML, JSON, TOML, etc. files from disk; it's generally assumed you're running in a Node/container environment where environment variables are the primary means of configuration.

    In theory the Environment type decouples ts-app-env from the actual Node/process/etc. environment, so you could provide other implementations.

    You can also use something like dotenv to load files from disk into process.env and then use ts-app-env from there.

Usage

First declare your config in a class via the string, number, etc. options:

import { string, number } from 'ts-app-env';

const AppEnv = {
  PORT: number(),

  SOME_URL: string(),
};

And then instantiate it:

import { newConfig } from 'ts-app-env';

export const env = newConfig(AppEnv, process.env);

newConfig will fail if any non-optional config parameters are not available.

Then in the rest of your application, you can import env:

import { env } from 'env.ts';

env.SOME_URL; // already ensured to be set

Configuration

The library supports both a convention of "property name == environment name" that allow succinct declaration:

const AppEnv = {
  PORT: number(),
}

As well as customization of each property via an options hash:

const AppEnv = {
  port: number({
    env: 'CUSTOM_ENV_NAME',
    default: 8080,
    optional: true,
  }),
}

Where:

  • env provides the environment name to look up; if not provided it defaults to the property name snake-cased (e.g. someThing will be looked up as SOME_THING)
  • default provides a default value to use if the environment value is not set
  • optional marks the option as optional, and will change the return type e.g. from port: number to port: number | undefined