@snapstack/config
v0.1.4
Published
Environment variable validation
Downloads
3
Maintainers
Readme
@snapstack/config
A progressive, type-safe, lightweight configuration management library for TypeScript applications based on Zod.
Installation
npm install @snapstack/configQuick Start
import { parse, z } from '@snapstack/config';
// 1. BASIC USAGE - Define schema, get typed config
const config = parse({
schema: z.object({
PORT: z.number().default(3000),
DATABASE_URL: z.string(),
POOL_SIZE: z.number().default(10),
REDIS_URL: z.string(),
REDIS_TTL: z.number().default(3600)
})
});
// Load config from env vars and .env file
const settings = await config.load();
// Fully typed!
console.log(`Server on port ${settings.port}`);
Basic setup
const config = parse({
schema: z.object({
PORT: z.number().default(3000),
API_URL: z.string().url(),
DEBUG: z.boolean().default(false),
NODE_ENV: z.enum(["development", "production"]).default("development")
})
});
// with .env file
// PORT=3000
// API_URL="https://api.snapcaster.ca
// DEBUG=0
// Access like so
console.log(config.PORT);
console.log(config.DATABASE_URL)
// Watch for changes in development
if (config.NODE_ENV === 'development') {
config.watch((newConfig, changes) => {
console.log('Config changed:', changes);
});
}
// 3. ERROR HANDLING - Beautiful, actionable errors
try {
await config.load();
} catch (error) {
console.error(error.message);
// Configuration Error:
//
// ❌ DATABASE_URL: Missing required value
// ❌ PORT: Expected number, got string
process.exit(1);
}