Skip to content

Latest commit

 

History

History
65 lines (44 loc) · 921 Bytes

File metadata and controls

65 lines (44 loc) · 921 Bytes

@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