feat: properly handle data fetching state on view bucket page

This commit is contained in:
2024-10-13 22:49:20 +00:00
parent 611258d0db
commit c1619276c0
3 changed files with 70 additions and 27 deletions
+9 -11
View File
@@ -30,23 +30,21 @@ const api = {
headers: { ...headers, ...(options?.headers || {}) },
});
if (!res.ok) {
const json = await res.json().catch(() => {});
const message = json?.message || res.statusText;
throw new Error(message);
}
const isJson = res.headers
.get("Content-Type")
?.includes("application/json");
const data = isJson ? await res.json() : await res.text();
if (isJson) {
const json = (await res.json()) as T;
return json;
if (!res.ok) {
const message = isJson
? data?.message
: typeof data === "string"
? data
: res.statusText;
throw new Error(message);
}
const text = await res.text();
return text as unknown as T;
return data as unknown as T;
},
async get<T = any>(url: string, options?: Partial<FetchOptions>) {