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

@kaminooni/env

v2.0.1

Published

Type-save env variables access

Downloads

3

Readme

@kaminooni/env

Simple, declarative, and type-safe access to process.env variables.

Usage

import { env } from '@kaminooni/env'

enum Environment {
  Production = 'production',
  Development = 'development'
}

const config = {
  environment: env('NODE_ENV').enum(Environment),
  debug: env('DEBUG', false).boolean(),
  port: env('PORT', 3000).range(0, 65536),
  app: {
    name: env('APP_NAME', 'My App').string(),
    secret: env('APP_SECRET').string(),
    apiVersion: env('API_VERSION', 'v3').whitelist(['v1', 'v2', 'v3']),
    emails: env('EMAILS', ['[email protected]']).json<string[]>(),
    retries: env('RETRIES', 5).number(),
  },
}

env(...) function

env(key: string, defaultValue?: string) => VariableBuilder

This function takes Env variable name, reads this variable from process.env and returns the instance of VariableBuilder

VariableBuilder

VariableBuilder helps you to validate, transform, and cast the env variables to a type of your choice. All values are considered required, unless the default value is provided. If you try to access the non-existent variable, the builder will throw an error.

string() / toString()

Converts env variable to string

env('APP_NAME').string()

boolean()

Converts env variable to boolean Returns true if env value equals 'true' (case-insensitive). Returns false otherwise.

env('DEBUG').boolean()

number()

Converts env variable to number.

NOTE: Empty string '' will be converted to 0.

env('MAX_CONNECTIONS').number()

whitelist(values: string[])

Verifies that value is in the white list. Throws an Error if it's not.

env('API_VERSION').whitelist(['v1', 'v2', 'v3'])

range(from: number, to: number)

Verifies that value is a number in the specified range. Both limits are inclusive. Throws an Error if value is not in the specified range.

env('SERVER_PORT', 3000).range(0, 65536)

enum()

Converts variable to the member of specified enum. Throws an Error if the value is not a member of the provided Enum.

enum Environment {
  Production = 'production',
  Development = 'development'
}

const ENVIRONMENT: Environment = env('NODE_ENV').enum(Environment)

json()

Converts env variable to JS object using JSON.parse() function

NOTE: This functions doesn't check that JSON comply with the specified type.

const emails: string[] = env('EMAILS', ['[email protected]']).json<string[]>()