From 0331a59da336eeebee6e17bb45b94247893feaab Mon Sep 17 00:00:00 2001 From: ValueOn AG Date: Mon, 25 May 2026 17:35:52 +0200 Subject: [PATCH] config fix --- config/config.ts | 188 ++++------------------------------------------- config/index.ts | 13 +--- 2 files changed, 17 insertions(+), 184 deletions(-) diff --git a/config/config.ts b/config/config.ts index 21a7472..0e5a800 100644 --- a/config/config.ts +++ b/config/config.ts @@ -1,178 +1,22 @@ /** - * Simple Configuration Service - * Centralized access to environment variables with fallbacks + * Configuration — reads mandatory env vars set by .env (copied from config/env-*.env by CI). + * + * NO silent fallbacks for critical values. + * If VITE_API_BASE_URL is missing the app fails loudly at startup. + * + * Vite replaces import.meta.env.VITE_* statically at build time. + * Dynamic access via import.meta.env[key] does NOT work in production builds. + * Therefore each variable must be accessed with its literal property name. */ -// API Configuration -export const getApiBaseUrl = (): string => { - return import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000'; -}; +const _apiBaseUrl: string = import.meta.env.VITE_API_BASE_URL; -export const getApiTimeout = (): number => { - return parseInt(import.meta.env.VITE_API_TIMEOUT || '10000'); -}; +if (!_apiBaseUrl) { + throw new Error( + 'Missing required env variable: VITE_API_BASE_URL. Ensure .env is present (cp config/env-.env .env).' + ); +} -// App Configuration -export const getAppName = (): string => { - return import.meta.env.VITE_APP_NAME || 'PowerOn'; -}; +export const getApiBaseUrl = (): string => _apiBaseUrl; -export const getAppVersion = (): string => { - return import.meta.env.VITE_APP_VERSION || '0.0.0'; -}; - -export const getAppEnvironment = (): string => { - return import.meta.env.VITE_APP_ENVIRONMENT || 'dev'; -}; - -// Environment Detection -export const isDevelopment = (): boolean => { - return import.meta.env.MODE === 'development' || getAppEnvironment() === 'dev'; -}; - -export const isProduction = (): boolean => { - return import.meta.env.MODE === 'production' || getAppEnvironment() === 'prod'; -}; - -export const isIntegration = (): boolean => { - return getAppEnvironment() === 'int'; -}; - -// Debug Configuration -export const isDebugMode = (): boolean => { - return import.meta.env.VITE_DEBUG === 'true'; -}; - -export const getLogLevel = (): string => { - return import.meta.env.VITE_LOG_LEVEL || 'info'; -}; - -export const isConsoleLogsEnabled = (): boolean => { - return import.meta.env.VITE_ENABLE_CONSOLE_LOGS === 'true'; -}; - -// Microsoft Authentication -export const getMicrosoftClientId = (): string | undefined => { - return import.meta.env.VITE_MICROSOFT_CLIENT_ID; -}; - -export const getMicrosoftTenantId = (): string | undefined => { - return import.meta.env.VITE_MICROSOFT_TENANT_ID; -}; - -export const getEntraClientSecret = (): string | undefined => { - return import.meta.env.VITE_ENTRA_CLIENT_SECRET; -}; - -export const getEntraAuthority = (): string | undefined => { - return import.meta.env.VITE_ENTRA_AUTHORITY; -}; - -export const getEntraRedirectPath = (): string | undefined => { - return import.meta.env.VITE_ENTRA_REDIRECT_PATH; -}; - -export const getEntraRedirectUri = (): string | undefined => { - return import.meta.env.VITE_ENTRA_REDIRECT_URI; -}; - -// Feature Flags (if needed in the future) -export const isFeatureEnabled = (feature: string): boolean => { - const envKey = `VITE_ENABLE_${feature.toUpperCase()}`; - return import.meta.env[envKey] === 'true'; -}; - -// Analytics and Monitoring -export const isAnalyticsEnabled = (): boolean => { - return import.meta.env.VITE_ENABLE_ANALYTICS === 'true'; -}; - -export const isErrorReportingEnabled = (): boolean => { - return import.meta.env.VITE_ENABLE_ERROR_REPORTING === 'true'; -}; - -export const isPerformanceMonitoringEnabled = (): boolean => { - return import.meta.env.VITE_ENABLE_PERFORMANCE_MONITORING === 'true'; -}; - -// Development Server (for dev environment) -export const getDevServerPort = (): number => { - return parseInt(import.meta.env.VITE_DEV_SERVER_PORT || '5176'); -}; - -export const getDevServerHost = (): string => { - return import.meta.env.VITE_DEV_SERVER_HOST || 'localhost'; -}; - -export const isDevServerHttps = (): boolean => { - return import.meta.env.VITE_DEV_SERVER_HTTPS === 'true'; -}; - -// Security Configuration -export const isHttpsEnabled = (): boolean => { - return import.meta.env.VITE_ENABLE_HTTPS === 'true'; -}; - -export const isCspEnabled = (): boolean => { - return import.meta.env.VITE_ENABLE_CSP === 'true'; -}; - -// Test Configuration -export const isMockDataEnabled = (): boolean => { - return import.meta.env.VITE_ENABLE_MOCK_DATA === 'true'; -}; - -export const isTestMode = (): boolean => { - return import.meta.env.VITE_ENABLE_TEST_MODE === 'true'; -}; - -// Convenience object for easy destructuring -export const config = { - // API - getApiBaseUrl, - getApiTimeout, - - // App - getAppName, - getAppVersion, - getAppEnvironment, - - // Environment - isDevelopment, - isProduction, - isIntegration, - - // Debug - isDebugMode, - getLogLevel, - isConsoleLogsEnabled, - - // Microsoft Auth - getMicrosoftClientId, - getMicrosoftTenantId, - getEntraClientSecret, - getEntraAuthority, - getEntraRedirectPath, - getEntraRedirectUri, - - // Features - isFeatureEnabled, - - // Analytics - isAnalyticsEnabled, - isErrorReportingEnabled, - isPerformanceMonitoringEnabled, - - // Dev Server - getDevServerPort, - getDevServerHost, - isDevServerHttps, - - // Security - isHttpsEnabled, - isCspEnabled, - - // Test - isMockDataEnabled, - isTestMode, -}; +export const getAppName = (): string => import.meta.env.VITE_APP_NAME || 'PowerOn'; diff --git a/config/index.ts b/config/index.ts index ac50c32..29a7147 100644 --- a/config/index.ts +++ b/config/index.ts @@ -1,12 +1 @@ -// Export simple configuration service -export * from './config'; - -// Re-export commonly used functions -export { - getApiBaseUrl, - getAppName, - isDevelopment, - isProduction, - isDebugMode, - config -} from './config'; +export { getApiBaseUrl, getAppName } from './config';