Spaces:
Sleeping
Sleeping
File size: 1,419 Bytes
1643ce8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | const BASE =
(import.meta.env.VITE_API_BASE as string | undefined) || "/api";
export const API_BASE = BASE.replace(/\/$/, "");
async function parseError(res: Response): Promise<string> {
try {
const data = await res.json();
return data?.detail || res.statusText;
} catch {
return res.statusText;
}
}
export async function request<T>(
path: string,
options: RequestInit = {},
): Promise<T> {
const url = `${API_BASE}${path}`;
const res = await fetch(url, {
credentials: "same-origin",
...options,
});
if (!res.ok) {
throw new Error(await parseError(res));
}
if (res.status === 204) return null as T;
const contentType = res.headers.get("content-type") || "";
if (contentType.includes("application/json")) {
return (await res.json()) as T;
}
return (await res.text()) as T;
}
export async function postForm<T>(path: string, data: FormData): Promise<T> {
return request<T>(path, { method: "POST", body: data });
}
export async function postJson<T>(
path: string,
body: unknown,
): Promise<T> {
return request<T>(path, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
}
export async function putJson<T>(path: string, body: unknown): Promise<T> {
return request<T>(path, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
}
|