shadowsilence's picture
Clean re-upload from local project
37d26f1 verified
import { auth } from "./firebase";
const RAW_API_BASE_URL = String(import.meta.env.VITE_API_BASE_URL || "").trim();
const API_BASE_URL = RAW_API_BASE_URL
? RAW_API_BASE_URL.replace(/\/+$/, "")
: "";
async function getAuthHeader() {
const user = auth.currentUser;
if (!user) {
throw new Error("Please sign in first.");
}
const token = await user.getIdToken();
return { Authorization: `Bearer ${token}` };
}
export async function apiGet<T>(path: string): Promise<T> {
const headers = await getAuthHeader();
const response = await fetch(`${API_BASE_URL}${path}`, {
method: "GET",
headers,
cache: "no-store",
});
if (!response.ok) {
const body = await response.json().catch(() => ({}));
throw new Error(body.detail || `Request failed (${response.status})`);
}
return response.json() as Promise<T>;
}
export async function apiPostForm<T>(
path: string,
formData: FormData,
): Promise<T> {
const headers = await getAuthHeader();
const response = await fetch(`${API_BASE_URL}${path}`, {
method: "POST",
headers,
body: formData,
});
if (!response.ok) {
const body = await response.json().catch(() => ({}));
throw new Error(body.detail || `Request failed (${response.status})`);
}
return response.json() as Promise<T>;
}
export async function apiPatchForm<T>(
path: string,
formData: FormData,
): Promise<T> {
const headers = await getAuthHeader();
const response = await fetch(`${API_BASE_URL}${path}`, {
method: "PATCH",
headers,
body: formData,
});
if (!response.ok) {
const body = await response.json().catch(() => ({}));
throw new Error(body.detail || `Request failed (${response.status})`);
}
return response.json() as Promise<T>;
}
export async function apiPost<T>(path: string, payload: any): Promise<T> {
const headers = await getAuthHeader();
const response = await fetch(`${API_BASE_URL}${path}`, {
method: "POST",
headers: {
...headers,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (!response.ok) {
const body = await response.json().catch(() => ({}));
throw new Error(body.detail || `Request failed (${response.status})`);
}
return response.json() as Promise<T>;
}
export async function apiDelete(path: string): Promise<void> {
const headers = await getAuthHeader();
const response = await fetch(`${API_BASE_URL}${path}`, {
method: "DELETE",
headers,
});
if (!response.ok) {
const body = await response.json().catch(() => ({}));
throw new Error(body.detail || `Request failed (${response.status})`);
}
}