Refactor Mailgun config to use getEnvVar and improve checks
- Replace getEnv with getEnvVar for environment variable access - Update MailgunConfig types to allow undefined values - Enhance isMailgunConfigured to check for undefined and empty values - Update isDevelopmentMode to check production status - Improve test mocks for environment variable handling
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
// Mailgun Configuration
|
||||
// This file handles Mailgun credentials and configuration
|
||||
|
||||
import { getEnv } from '../utils/env';
|
||||
import { getEnvVar, isProduction } from '../utils/env';
|
||||
|
||||
export interface MailgunConfig {
|
||||
apiKey: string;
|
||||
domain: string;
|
||||
apiKey: string | undefined;
|
||||
domain: string | undefined;
|
||||
baseUrl: string;
|
||||
fromName: string;
|
||||
fromEmail: string;
|
||||
fromEmail: string | undefined;
|
||||
}
|
||||
|
||||
// Default configuration for development
|
||||
@@ -22,14 +22,12 @@ const defaultConfig: MailgunConfig = {
|
||||
|
||||
// Load configuration from environment variables or use defaults
|
||||
export const getMailgunConfig = (): MailgunConfig => {
|
||||
const env = getEnv();
|
||||
|
||||
return {
|
||||
apiKey: env.VITE_MAILGUN_API_KEY || defaultConfig.apiKey,
|
||||
domain: env.VITE_MAILGUN_DOMAIN || defaultConfig.domain,
|
||||
baseUrl: env.VITE_MAILGUN_BASE_URL || defaultConfig.baseUrl,
|
||||
fromName: env.VITE_MAILGUN_FROM_NAME || defaultConfig.fromName,
|
||||
fromEmail: env.VITE_MAILGUN_FROM_EMAIL || defaultConfig.fromEmail,
|
||||
apiKey: getEnvVar('VITE_MAILGUN_API_KEY'),
|
||||
domain: getEnvVar('VITE_MAILGUN_DOMAIN'),
|
||||
baseUrl: getEnvVar('VITE_MAILGUN_BASE_URL', defaultConfig.baseUrl),
|
||||
fromName: getEnvVar('VITE_MAILGUN_FROM_NAME', defaultConfig.fromName),
|
||||
fromEmail: getEnvVar('VITE_MAILGUN_FROM_EMAIL'),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -37,13 +35,20 @@ export const getMailgunConfig = (): MailgunConfig => {
|
||||
export const isMailgunConfigured = (): boolean => {
|
||||
const config = getMailgunConfig();
|
||||
return (
|
||||
config.apiKey !== 'demo-key' &&
|
||||
config.domain !== 'demo.mailgun.org' &&
|
||||
config.apiKey.length > 0
|
||||
config.apiKey !== defaultConfig.apiKey &&
|
||||
config.domain !== defaultConfig.domain &&
|
||||
config.fromEmail !== defaultConfig.fromEmail &&
|
||||
config.apiKey !== undefined &&
|
||||
config.apiKey !== '' &&
|
||||
config.apiKey.trim().length > 0 &&
|
||||
config.domain !== undefined &&
|
||||
config.domain !== '' &&
|
||||
config.fromEmail !== undefined &&
|
||||
config.fromEmail !== ''
|
||||
);
|
||||
};
|
||||
|
||||
// Development mode check
|
||||
export const isDevelopmentMode = (): boolean => {
|
||||
return !isMailgunConfigured();
|
||||
return !isProduction() && !isMailgunConfigured();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user