LiveDisplayX / src / lib / api / displays.ts
displays.ts
Raw
import { Display } from "@/lib/types/types";
import { useAuth } from "@clerk/nextjs";

export type UpdateDisplayData = Partial<Display>;

export const fetcher = async (
  url: string,
  getToken: () => Promise<string | null>,
  options?: RequestInit
) => {
  const token = await getToken();

  const res = await fetch(url, {
    ...options,
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${token}`,
      ...options?.headers,
    },
  });

  if (!res.ok) {
    const error = new Error("An error occurred while fetching the data.");
    const info = await res.json().catch(() => ({}));
    (error as any).info = info;
    (error as any).status = res.status;
    throw error;
  }

  if (res.status === 204) {
    return null;
  }

  return res.json();
};