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.
This commit is contained in:
GotthardG 2024-12-18 07:35:25 +01:00
parent 19bb05fc48
commit 2360e0718d

View File

@ -10,7 +10,6 @@ import { OpenAPI, SpreadsheetService } from '../../openapi';
import type { Body_upload_file_upload_post } from '../../openapi/models/Body_upload_file_upload_post';
import SpreadsheetTable from './SpreadsheetTable';
import Modal from './Modal';
import * as ExcelJS from 'exceljs';
interface UploadDialogProps {
open: boolean;
@ -27,10 +26,26 @@ const UploadDialog: React.FC<UploadDialogProps> = ({ open, onClose, selectedShip
const fileInputRef = useRef<HTMLInputElement>(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 downloadUrl = `${OpenAPI.BASE}/download-template`;