2026-01-21 23:09:33 +08:00
|
|
|
const BASE_URL = import.meta.env.VITE_SERVER_BASE_URL || "http://localhost:3001";
|
|
|
|
|
|
|
|
|
|
export async function apiFetch(path, options = {}) {
|
|
|
|
|
const headers = new Headers(options.headers || {});
|
|
|
|
|
if (!headers.has("Accept")) headers.set("Accept", "application/json");
|
|
|
|
|
|
|
|
|
|
if (!(options.body instanceof FormData) && options.body != null) {
|
|
|
|
|
headers.set("Content-Type", "application/json");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lazy import to avoid circular deps
|
|
|
|
|
const { getToken } = await import("./extStorage.js");
|
|
|
|
|
const token = await getToken();
|
|
|
|
|
if (token) headers.set("Authorization", `Bearer ${token}`);
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${BASE_URL}${path}`, { ...options, headers });
|
|
|
|
|
|
|
|
|
|
const contentType = res.headers.get("content-type") || "";
|
|
|
|
|
const isJson = contentType.includes("application/json");
|
|
|
|
|
const payload = isJson ? await res.json().catch(() => null) : await res.text().catch(() => "");
|
|
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
const message = payload?.message || `HTTP ${res.status}`;
|
|
|
|
|
const err = new Error(message);
|
|
|
|
|
err.status = res.status;
|
|
|
|
|
err.payload = payload;
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
|
}
|