import { getToken } from "./auth"

const API_URL = process.env.NEXT_PUBLIC_API_URL

export async function apiFetch(path: string, options: RequestInit = {}) {
  const token = getToken()

  return fetch(`${API_URL}${path}`, {
    ...options,
    keepalive: true,
    headers: {
      "Content-Type": "application/json",
      "Connection": "keep-alive",
      ...(token && { Authorization: `Bearer ${token}` }),
      ...options.headers,
    },
  })
}