import { config } from 'dotenv';
config();
export interface EnvironmentConfig {
webhookUrl: string;
port: number;
logLevel: string;
}
export class EnvironmentError extends Error {
constructor(message: string) {
super(message);
this.name = 'EnvironmentError';
}
}
export function validateEnvironment(): EnvironmentConfig {
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
if (!webhookUrl) {
throw new EnvironmentError(
'DISCORD_WEBHOOK_URL is required in environment variables'
);
}
// Validate webhook URL format
const webhookPattern = /^https:\/\/discord\.com\/api\/webhooks\/\d+\/[\w-]+$/;
if (!webhookPattern.test(webhookUrl)) {
throw new EnvironmentError(
'DISCORD_WEBHOOK_URL must be a valid Discord webhook URL'
);
}
return {
webhookUrl,
port: parseInt(process.env.PORT ?? '3000', 10),
logLevel: process.env.LOG_LEVEL ?? 'info',
};
}