'use client'; import { toast } from "@/components/ui/use-toast"; type FetchMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; interface FetchOptions { method?: FetchMethod; body?: any; cache?: RequestCache; headers?: HeadersInit; } export async function fetchClient( endpoint: string, options: FetchOptions = {} ): Promise { const { method = 'GET', body, headers = {}, ...rest } = options; const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001'; const url = `${apiUrl}/api${endpoint.startsWith('/') ? endpoint : `/${endpoint}`}`; const fetchOptions: RequestInit = { method, credentials: 'include', headers: { 'Content-Type': 'application/json', ...headers, }, ...rest, }; if (body && method !== 'GET') { fetchOptions.body = JSON.stringify(body); } try { const response = await fetch(url, fetchOptions); if (!response.ok) { const errorData = await response.json().catch(() => ({})); const errorMessage = errorData.message || errorData.error || 'An error occurred'; throw new Error(errorMessage); } if (response.status === 204) { return {} as T; } const data = await response.json(); return data; } catch (error) { console.error('API request failed:', error); // Only show toast if this is a client-side error (not during SSR) if (typeof window !== 'undefined') { const message = error instanceof Error ? error.message : 'Failed to connect to server'; toast({ title: 'Error', description: message, variant: 'destructive', }); } throw error; } }