Files
rxminder/utils/env.ts
William Valentin b59160eb10 feat: implement unified configuration as single source of truth
- Make unified config lazy-loaded to avoid initialization issues
- Replace direct config exports with getter functions
- Rewrite utils/env.ts to use unified config instead of scattered access
- Add show-config.js helper script for configuration management
- Type-safe configuration access throughout the app
- Smart defaults for all environments with environment overrides
- Eliminates scattered process.env and import.meta.env access
2025-09-08 21:23:44 -07:00

154 lines
3.2 KiB
TypeScript

/**
* Environment Utilities - Single Source of Truth
*
* This module provides environment utilities that use the unified config
* as the single source of truth for all environment variables.
*
* Use this instead of directly accessing process.env or import.meta.env
*/
import {
getAppConfig,
getDatabaseConfig,
getAuthConfig,
getEmailConfig,
getOAuthConfig,
getFeatureFlags,
getLoggingConfig,
getSecurityConfig,
unifiedConfig,
} from '../config/unified.config';
/**
* Get the current environment
*/
export function getEnvironment(): string {
return getAppConfig().environment;
}
/**
* Check if we're running in a browser environment
*/
export function isBrowser(): boolean {
return typeof window !== 'undefined';
}
/**
* Check if we're running in a Node.js environment
*/
export function isNode(): boolean {
return typeof process !== 'undefined' && Boolean(process.versions?.node);
}
/**
* Check if we're running in a test environment
*/
export function isTest(): boolean {
return getAppConfig().environment === 'test';
}
/**
* Check if we're running in development
*/
export function isDevelopment(): boolean {
return getAppConfig().environment === 'development';
}
/**
* Check if we're running in production
*/
export function isProduction(): boolean {
return getAppConfig().environment === 'production';
}
/**
* Check if we're running in staging
*/
export function isStaging(): boolean {
return getAppConfig().environment === 'staging';
}
/**
* Get application configuration
*/
export { getAppConfig };
/**
* Get database configuration
*/
export { getDatabaseConfig };
/**
* Get authentication configuration
*/
export { getAuthConfig };
/**
* Get email configuration
*/
export { getEmailConfig };
/**
* Get OAuth configuration
*/
export { getOAuthConfig };
/**
* Get feature flags
*/
export { getFeatureFlags };
/**
* Get logging configuration
*/
export { getLoggingConfig };
/**
* Get security configuration
*/
export { getSecurityConfig };
/**
* Get the full unified configuration
* Use this sparingly - prefer the specific getters above
*/
export function getUnifiedConfig() {
return unifiedConfig;
}
/**
* Legacy compatibility - get a specific environment variable
* @deprecated Use the unified config getters instead
*/
export function getEnvVar(key: string, fallback?: string): string | undefined {
console.warn(
`getEnvVar('${key}') is deprecated. Use unified config instead.`
);
// Try to map common environment variables to unified config
switch (key) {
case 'NODE_ENV':
return getAppConfig().environment;
case 'APP_NAME':
case 'VITE_APP_NAME':
return getAppConfig().name;
case 'APP_BASE_URL':
return getAppConfig().baseUrl;
case 'VITE_COUCHDB_URL':
case 'COUCHDB_URL':
return getDatabaseConfig().url;
case 'VITE_COUCHDB_USER':
case 'COUCHDB_USER':
return getDatabaseConfig().username;
case 'VITE_COUCHDB_PASSWORD':
case 'COUCHDB_PASSWORD':
return getDatabaseConfig().password;
case 'JWT_SECRET':
return getAuthConfig().jwtSecret;
case 'LOG_LEVEL':
return getLoggingConfig().level;
default:
return fallback;
}
}