6

neon-env - npm

 1 year ago
source link: https://www.npmjs.com/package/neon-env
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

neon-env

Basically typed-env, but with support for choices and maintained.

A typed environment variable parser with support for choices, custom parsers, and more.

Installation

Node.js ^14.8, v16 or newer is required. See issue #1.

npm i neon-env

Usage

import { createEnv } from 'neon-env';

const env = createEnv({
  PORT: { type: 'number', default: 80 }
});

env.PORT // number

Features

  • Strongly typed
  • Supports custom parsers
  • Supports optional environment variables
  • Supports limiting the possible values (see Choices)
  • Supports passing custom environments (see Options)

Choices

Make sure you add as const after the choices array for maximum type safety.

Warning If you notice that it's just typed as string, this likely means you forgot to add as const.

import { createEnv } from 'neon-env';

const env = createEnv({
  NODE_ENV: {
    type: 'string',
    choices: ['development', 'production'] as const
  }
});

env.NODE_ENV // 'development' | 'production'

Parser

You can pass a parser function to return your own custom type

import { createEnv } from 'neon-env';

const env = createEnv({
  HOMEPAGE: { parser: url => new URL(url) }
});

env.HOMEPAGE // URL

Options

If you want to use a custom env, pass env in the options parameter, otherwise it will load from process.env

interface Options {
  env?: Record<string, string> | NodeJS.ProcessEnv;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK