diff --git a/src/api/aisClient.ts b/src/api/aisClient.ts index 116903b..b6d4184 100644 --- a/src/api/aisClient.ts +++ b/src/api/aisClient.ts @@ -1,13 +1,21 @@ import axios from 'axios'; import type { VesselData } from '../types'; -// Using MarineTraffic API as an example - you'll need to add your API key -const API_KEY = import.meta.env.VITE_MARINE_TRAFFIC_API_KEY; const API_BASE_URL = 'https://services.marinetraffic.com/api/vesselmasterdata/v3'; +// Get API key at request time to ensure window.env is populated +const getApiKey = (): string | undefined => { + if (typeof window !== 'undefined' && window.env?.MARINE_TRAFFIC_API_KEY) { + return window.env.MARINE_TRAFFIC_API_KEY; + } + return import.meta.env.VITE_MARINE_TRAFFIC_API_KEY; +}; + export async function getVesselData(imo: string): Promise { + const apiKey = getApiKey(); // Lazy evaluate at request time + // For development, return mock data if no API key is present - if (!API_KEY) { + if (!apiKey) { console.warn('No API key found - using mock data'); return getMockVesselData(imo); } @@ -16,7 +24,7 @@ export async function getVesselData(imo: string): Promise { const response = await axios.get(API_BASE_URL, { params: { imo, - apikey: API_KEY, + apikey: apiKey, } }); diff --git a/src/api/checkoutClient.ts b/src/api/checkoutClient.ts index 4a83b55..2785187 100644 --- a/src/api/checkoutClient.ts +++ b/src/api/checkoutClient.ts @@ -2,6 +2,8 @@ import axios from 'axios'; import { logger } from '../utils/logger'; // Get API base URL from runtime config (window.env) or build-time config +// IMPORTANT: Call this function at REQUEST TIME, not at module load time, +// to ensure window.env is populated by env-config.js const getApiBaseUrl = (): string => { // Check window.env first (runtime config from env.sh) if (typeof window !== 'undefined' && window.env?.API_BASE_URL) { @@ -12,8 +14,6 @@ const getApiBaseUrl = (): string => { return import.meta.env.VITE_API_BASE_URL || 'http://localhost:3001'; }; -const API_BASE_URL = getApiBaseUrl(); - export interface CreateCheckoutSessionParams { tons: number; portfolioId: number; @@ -54,10 +54,12 @@ export async function createCheckoutSession( params: CreateCheckoutSessionParams ): Promise { try { + const apiBaseUrl = getApiBaseUrl(); // Lazy evaluate at request time logger.info('Creating checkout session:', params); + logger.info('Using API base URL:', apiBaseUrl); const response = await axios.post( - `${API_BASE_URL}/api/checkout/create-session`, + `${apiBaseUrl}/api/checkout/create-session`, params ); @@ -79,10 +81,11 @@ export async function createCheckoutSession( */ export async function getOrderDetails(sessionId: string): Promise { try { + const apiBaseUrl = getApiBaseUrl(); // Lazy evaluate at request time logger.info('Fetching order details for session:', sessionId); const response = await axios.get( - `${API_BASE_URL}/api/checkout/session/${sessionId}` + `${apiBaseUrl}/api/checkout/session/${sessionId}` ); logger.info('Order details retrieved:', response.data.order.id); @@ -102,7 +105,8 @@ export async function getOrderDetails(sessionId: string): Promise */ export async function checkBackendHealth(): Promise { try { - const response = await axios.get(`${API_BASE_URL}/health`); + const apiBaseUrl = getApiBaseUrl(); // Lazy evaluate at request time + const response = await axios.get(`${apiBaseUrl}/health`); return response.data.status === 'ok'; } catch (error) { logger.error('Backend health check failed:', error);