// Configuration with authentication support for private HF Spaces export function getApiUrl(): string { // Check for explicit URL first if (process.env.NEXT_PUBLIC_API_URL) { return process.env.NEXT_PUBLIC_API_URL; } // Default for local development if (typeof window === 'undefined') { return 'http://localhost:8000'; } // Auto-detect based on current location const currentHost = window.location.hostname; if (currentHost === 'localhost' || currentHost === '127.0.0.1') { return 'http://localhost:8000'; } // For production, use the backend URL return process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000'; } export function getAuthHeaders(): HeadersInit { const headers: HeadersInit = { 'Content-Type': 'application/json', }; // Add HF token if available (for private spaces) if (process.env.NEXT_PUBLIC_HF_TOKEN) { headers['Authorization'] = `Bearer ${process.env.NEXT_PUBLIC_HF_TOKEN}`; } // Add custom API key if configured if (process.env.NEXT_PUBLIC_API_KEY) { headers['X-API-Key'] = process.env.NEXT_PUBLIC_API_KEY; } return headers; } export async function fetchWithAuth(url: string, options: RequestInit = {}): Promise { const authHeaders = getAuthHeaders(); return fetch(url, { ...options, headers: { ...authHeaders, ...options.headers, }, }); }