@juriadams/env
v1.1.1
Published
A simple and type-safe way to validate environment variables.
Readme
@juriadams/env
A tiny, native, type-safe library for reading and validating environment variables, optimized for Workers, Bun, and Node.
Installation
bun add @juriadams/envUsage
import { vars, optional } from "@juriadams/env";
const env = vars([
"DB_URL",
"OPENAI_API_KEY",
optional("SENTRY_DSN"),
optional("PORT", "3000"),
]);
type Env = typeof env;
// {
// DB_URL: string;
// OPENAI_API_KEY: string;
// SENTRY_DSN: string | null;
// PORT: string | "3000";
// }Optional Variables
optional("PORT") marks a key as optional. A default value can be provided via optional("PORT", "3000"), the type of which is strictly inferred.
[!IMPORTANT]
If one or more expected environment variables are missing, aMissingEnvironmentVariablesErroris thrown.
Custom Environment
By default, vars reads from import.meta.env and process.env (in order). A custom environment to read from can be provided via vars([ ... ], { env: process.env }).
[!IMPORTANT]
If no environment can be resolved, anInvalidEnvironmentErroris thrown.
Lifecycle
Develop
bun devTest
bun testBuild
bun run buildTypecheck
bun typecheckLint / Format
bun lint
bun format