Naveedtechlab's picture
fix: Use relative URLs instead of localhost for HF Space deployment
cbdb737
/**
* API client utilities for backend communication with JWT handling.
*/
const API_URL = process.env.NEXT_PUBLIC_API_URL || '';
export function getToken(): string | null {
if (typeof window === 'undefined') return null;
return localStorage.getItem('token');
}
export function setToken(token: string): void {
if (typeof window === 'undefined') return;
localStorage.setItem('token', token);
}
export function removeToken(): void {
if (typeof window === 'undefined') return;
localStorage.removeItem('token');
}
export async function apiCall(
endpoint: string,
method: string = 'GET',
body?: any,
requiresAuth: boolean = true
): Promise<any> {
const headers: HeadersInit = {
'Content-Type': 'application/json',
};
if (requiresAuth) {
const token = getToken();
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
}
const options: RequestInit = {
method,
headers,
};
if (body) {
options.body = JSON.stringify(body);
}
const response = await fetch(`${API_URL}${endpoint}`, options);
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'API request failed');
}
if (response.status === 204) {
return null;
}
return response.json();
}