type EnvSchema = Record<string, { default?: string; required?: boolean }>;
export function loadEnv<S extends EnvSchema>(schema: S): { [K in keyof S]: string } {
const out = {} as { [K in keyof S]: string };
const missing: string[] = [];
for (const key in schema) {
const v = process.env[key] ?? schema[key]!.default;
if (v === undefined) {
if (schema[key]!.required ?? true) missing.push(key);
} else {
out[key] = v;
}
}
if (missing.length) {
console.error('Missing required env vars:', missing.join(', '));
process.exit(1);
}
return out;
}
export const env = loadEnv({
DATABASE_URL: {},
STRIPE_SECRET: {},
PORT: { default: '3000' },
DEBUG: { default: 'false', required: false },
});
Create a free account and build your private vault. Share publicly whenever you want.