/**
 * Fetcher utility for SWR
 * Handles API requests with proper error handling
 */

interface FetchError extends Error {
  info?: unknown;
  status?: number;
}

export async function fetcher<T>(url: string): Promise<T> {
  const response = await fetch(url);

  if (!response.ok) {
    const error: FetchError = new Error('An error occurred while fetching the data.');
    // Attach extra info to the error object
    error.info = await response.json();
    error.status = response.status;
    throw error;
  }

  return response.json();
}
