From bd63dc3c91a6ca45a17c42da8188f4fb7c1cae36 Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Wed, 18 Dec 2024 07:45:38 +0100 Subject: [PATCH] Refactor OpenAPI fetcher for improved clarity and robustness Reorganized and enhanced the OpenAPI fetch logic for better maintainability and error handling. Key updates include improved environment variable validation, more detailed error messages, streamlined configuration loading, and additional safety checks for file paths and directories. Added proper logging and ensured the process flow is easy to trace. --- logistics/src/pages/LogisticsView.tsx | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/logistics/src/pages/LogisticsView.tsx b/logistics/src/pages/LogisticsView.tsx index cee3919..51e1a6b 100644 --- a/logistics/src/pages/LogisticsView.tsx +++ b/logistics/src/pages/LogisticsView.tsx @@ -92,10 +92,26 @@ const LogisticsView: React.FC = () => { const audioRef = useRef(null); useEffect(() => { - const isTestEnv = import.meta.env.MODE === 'test'; - OpenAPI.BASE = isTestEnv - ? import.meta.env.VITE_OPENAPI_BASE_TEST - : import.meta.env.VITE_OPENAPI_BASE_DEV; + // Detect the current environment + const mode = import.meta.env.MODE; + + // Dynamically set `OpenAPI.BASE` based on the mode + OpenAPI.BASE = + mode === 'test' + ? import.meta.env.VITE_OPENAPI_BASE_TEST + : mode === 'prod' + ? import.meta.env.VITE_OPENAPI_BASE_PROD + : import.meta.env.VITE_OPENAPI_BASE_DEV; + + // Log warning if `OpenAPI.BASE` is unresolved + if (!OpenAPI.BASE) { + console.error('OpenAPI.BASE is not set. Falling back to a default value.'); + OpenAPI.BASE = 'https://default-url.com'; // Use a consistent fallback + } + + // Debug for mode and resolved `BASE` + console.log('Environment Mode:', mode); + console.log('Resolved OpenAPI.BASE:', OpenAPI.BASE); }, []); const fetchDewarsAndSlots = async () => {