import CONFIG from "./config.js";

type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";

async function apiRequest<T = any>(
  endpoint: string,
  method: HttpMethod = "GET",
  data: Record<string, any> | null = null
): Promise<T> {
  const response = await fetch(`${CONFIG.API_BASE_URL}/${endpoint}`, {
    method,
    headers: {
      "Content-Type": "application/json",
    },
    body: data ? JSON.stringify(data) : null,
  });

  return response.json();
}

export default apiRequest;
