@jeetgr/env
v0.0.1
Published
Synchronous environment variable parser using @standard-schema/spec
Downloads
6
Maintainers
Readme
@jeetgr/env
A tiny utility to synchronously parse and validate environment variables using a Standard Schema-compatible schema.
🚀 Features
- ✅ Validates
process.envor a custom source - ✅ Works with any schema compatible with
@standard-schema/spec - ✅ Synchronous-only validation (fails fast if async)
- ✅ Returns a fully typed accessor function to read validated environment variables
📦 Installation
# npm
npm install @jeetgr/env
# yarn
yarn add @jeetgr/env
# pnpm
pnpm add @jeetgr/env
# bun
bun add @jeetgr/env🧪 Example
This example uses Zod, but you can use any validation library that supports @standard-schema/spec.
import { parseEnv } from '@jeetgr/env'
import * as z from 'zod'
const schema = z.object({
PORT: z.coerce.number().default(3000),
DATABASE_URL: z.string(),
})
const env = parseEnv({ schema })
console.log(env('PORT')) // => 3000 (or value from process.env)
console.log(env('DATABASE_URL')) // => value from process.envYou can also pass a custom source (e.g. for testing):
const env = parseEnv({
schema,
source: {
PORT: '8080',
DATABASE_URL: 'postgres://localhost/db',
},
})
console.log(env('PORT')) // => 8080
console.log(env('DATABASE_URL')) // => postgres://localhost/db📄 License
MIT © Jeet Gangwar
