Skip to content
/ env Public

A simple and type-safe way to validate environment variables.

Notifications You must be signed in to change notification settings

juriadams/env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@juriadams/env

A simple and type-safe way to validate environment variables.

Installation

bun add @juriadams/env

Usage

import { vars, optional } from '@juriadams/env';

const env = vars([
  'OPENAI_API_KEY',
  optional('PORT'),
] as const);

// env is inferred as: { OPENAI_API_KEY: string; PORT: string | null }

If one or more required environment variables are missing, an InvalidEnvironmentError is thrown.

You can catch and handle it using instanceof:

import { vars, InvalidEnvironmentError } from '@juriadams/env';

try {
  const env = vars(['OPENAI_API_KEY'] as const);
} catch (err) {
  if (err instanceof InvalidEnvironmentError)
    console.debug({ missing: err.missing });

  throw err;
}

Lifecycle

Develop

bun run dev

Build

bun run build

Types

bun run typecheck

Test

bun run test

About

A simple and type-safe way to validate environment variables.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •