feat: add @tower/config package with env validation
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const envSchema = z.object({
|
||||
NODE_ENV: z.enum(['development', 'test', 'production']).default('development'),
|
||||
DATABASE_URL: z.string().url(),
|
||||
REDIS_URL: z.string().url(),
|
||||
API_PORT: z.coerce.number().default(3001),
|
||||
JWT_SECRET: z.string().min(32),
|
||||
MEILI_URL: z.string().url().default('http://localhost:7700'),
|
||||
MEILI_MASTER_KEY: z.string().default('tower_meili_dev_key'),
|
||||
LOG_LEVEL: z.enum(['trace', 'debug', 'info', 'warn', 'error']).default('info'),
|
||||
});
|
||||
|
||||
export type Env = z.infer<typeof envSchema>;
|
||||
|
||||
export function validateEnv(env: NodeJS.ProcessEnv = process.env): Env {
|
||||
const result = envSchema.safeParse(env);
|
||||
if (!result.success) {
|
||||
console.error('Invalid environment variables:', result.error.format());
|
||||
throw new Error('Invalid environment variables');
|
||||
}
|
||||
return result.data;
|
||||
}
|
||||
Reference in New Issue
Block a user