envey
v2.4.0
Published
Type-safe configuration schemas for Node.js
Downloads
23
Maintainers
Readme
envey
Envey is a library designed to simplify the process of managing and validating environment variables in Node.js applications. It provides a fully type-safe solution for defining and parsing configuration schemas, leveraging the power of Zod's excellent type system.
Installation
pnpm i -E zod envey
Usage
import { z } from 'zod'
import { createConfig } from 'envey'
const result = createConfig(
z,
{
databaseUrl: {
env: 'DATABASE_URL',
format: z.string(),
},
port: {
env: 'PORT',
format: z.coerce.number().int().positive().max(65535),
},
},
{ validate: true },
)
if (!result.success) {
console.error(result.error.issues)
// Handle error
}
const { config } = result
// ^? {
// readonly databaseUrl: string;
// readonly port: number;
// }
Supports schema type inference, similar to Zod's infer:
const schema = {
logLevel: {
env: 'LOG_LEVEL',
format: z.enum([
'fatal',
'error',
'warn',
'info',
'debug',
'trace',
'silent',
]),
},
} satisfies EnveySchema
type Config = InferEnveyConfig<typeof schema>
// ^? {
// readonly logLevel: "fatal" | "error" | "warn" | "info" | "debug" | "trace" | "silent"
// }