|
| 1 | + |
| 2 | +interface IEvnConfig { |
| 3 | + [key: string]: string | number | undefined; |
| 4 | + nodeEnv: string; |
| 5 | + port: number; |
| 6 | + developmentUrl?: string; |
| 7 | + jwtSecret: string; |
| 8 | + databaseUrl: string; |
| 9 | + databasePort: number; |
| 10 | + databaseName: string; |
| 11 | + databaseUser: string; |
| 12 | + databasePassword: string; |
| 13 | + googleClientId?: string; |
| 14 | + googleClientSecret?: string; |
| 15 | + gitHubClientId?: string; |
| 16 | + gitHubClientSecret?: string; |
| 17 | + discordClientId?: string; |
| 18 | + discordClientSecret?: string; |
| 19 | + cloudflareR2BucketName: string; |
| 20 | + cloudflareR2AccessKey: string; |
| 21 | + cloudflareR2SecretKey: string; |
| 22 | + cloudflareR2AccessId: string; |
| 23 | + cloudflareR2Domain: string; |
| 24 | + destinationBucketName?: string; |
| 25 | + destinationAccountId?: string; |
| 26 | + destinationAccessKey?: string; |
| 27 | + destinationSecretKey?: string; |
| 28 | +} |
| 29 | + |
| 30 | +const envConfig: IEvnConfig = { |
| 31 | + nodeEnv: process.env.NODE_ENV || 'development', |
| 32 | + port: parseInt(process.env.APP_PORT || '3000', 10), |
| 33 | + developmentUrl: process.env.DEVELOPMENT_URL || '', |
| 34 | + jwtSecret: process.env.JWT_SECRET || '', |
| 35 | + databaseUrl: process.env.DATABASE_URL || '', |
| 36 | + databasePort: parseInt(process.env.DATABASE_PORT || '5432', 10), |
| 37 | + databaseName: process.env.DATABASE_NAME || '', |
| 38 | + databaseUser: process.env.PG_USER || '', |
| 39 | + databasePassword: process.env.PG_PASSWORD || '', |
| 40 | + googleClientId: process.env.GOOGLE_CLIENT_ID || '', |
| 41 | + googleClientSecret: process.env.GOOGLE_CLIENT_SECRET || '', |
| 42 | + gitHubClientId: process.env.GITHUB_CLIENT_ID || '', |
| 43 | + gitHubClientSecret: process.env.GITHUB_CLIENT_SECRET || '', |
| 44 | + discordClientId: process.env.DISCORD_CLIENT_ID || '', |
| 45 | + discordClientSecret: process.env.DISCORD_CLIENT_SECRET || '', |
| 46 | + cloudflareR2BucketName: process.env.R2_BUCKET_NAME || '', |
| 47 | + cloudflareR2AccessKey: process.env.R2_ACCESS_KEY || '', |
| 48 | + cloudflareR2SecretKey: process.env.R2_SECRET_KEY || '', |
| 49 | + cloudflareR2AccessId: process.env.R2_ACCOUNT_ID || '', |
| 50 | + cloudflareR2Domain: process.env.R2_DOMAIN || '', |
| 51 | + destinationBucketName: process.env.DESTINATION_BUCKET_NAME || '', |
| 52 | + destinationAccountId: process.env.DESTINATION_ACCOUNT_ID || '', |
| 53 | + destinationAccessKey: process.env.DESTINATION_ACCESS_KEY || '', |
| 54 | + destinationSecretKey: process.env.DESTINATION_SECRET_KEY || '' |
| 55 | +}; |
| 56 | + |
| 57 | +console.table(envConfig); |
| 58 | + |
| 59 | +export const checkEnvConfig = () => { |
| 60 | + // Define only the required fields |
| 61 | + const requiredFields: string[] = [ |
| 62 | + 'nodeEnv', |
| 63 | + 'port', |
| 64 | + 'jwtSecret', |
| 65 | + 'databaseUrl', |
| 66 | + 'databaseName', |
| 67 | + 'databaseUser', |
| 68 | + 'databasePassword' |
| 69 | + ]; |
| 70 | + |
| 71 | + for (const field of requiredFields) { |
| 72 | + if (!envConfig[field] || envConfig[field] === '') { |
| 73 | + throw new Error(`Missing required environment variable: ${field.toUpperCase()}`); |
| 74 | + } |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +try { |
| 79 | + checkEnvConfig(); |
| 80 | + console.log('All required environment variables are correctly set.'); |
| 81 | +} catch (error) { |
| 82 | + if (error instanceof Error) { |
| 83 | + console.error(error.message); |
| 84 | + } else { |
| 85 | + console.error('An unknown error occurred.'); |
| 86 | + } |
| 87 | + process.exit(1); // Exit if missing required environment variable |
| 88 | +} |
| 89 | + |
| 90 | +export default envConfig; |
0 commit comments