Something went wrong. Try again.
A lightweight fork of Quenti, the popular open-source alternative to Quizlet, with a reduced scope in mind.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940const API_URL = import.meta.env.VITE_API_URL ?? "http://localhost:3001";
export class ApiError extends Error { constructor( public status: number, message: string, ) { super(message); }}
export const api = async <T>( path: string, init?: RequestInit,): Promise<T> => { const res = await fetch(`${API_URL}${path}`, { ...init, credentials: "include", headers: { "Content-Type": "application/json", ...init?.headers, }, });
if (!res.ok) { const body = await res.json().catch(() => ({ error: res.statusText })); throw new ApiError(res.status, body.error ?? res.statusText); }
if (res.status === 204) return undefined as T; return res.json() as Promise<T>;};
export const apiGet = <T>(path: string) => api<T>(path);export const apiPost = <T>(path: string, body?: unknown) => api<T>(path, { method: "POST", body: body ? JSON.stringify(body) : undefined });export const apiPatch = <T>(path: string, body?: unknown) => api<T>(path, { method: "PATCH", body: body ? JSON.stringify(body) : undefined });export const apiDelete = <T>(path: string) => api<T>(path, { method: "DELETE" });