mirror of
https://github.com/infinilabs/coco-app.git
synced 2025-12-16 19:47:43 +01:00
* chore: adjust oauth callback * chore: debug profile info * chore: modify logs logic and display * chore: add logout * chore: add userinfo * chore: remove advanced settings
124 lines
2.6 KiB
TypeScript
124 lines
2.6 KiB
TypeScript
import { fetch } from "@tauri-apps/plugin-http";
|
|
|
|
import { clientEnv } from "@/utils/env";
|
|
import { useLogStore } from "@/stores/logStore";
|
|
|
|
interface FetchRequestConfig {
|
|
url: string;
|
|
method?: "GET" | "POST" | "PUT" | "DELETE";
|
|
headers?: Record<string, string>;
|
|
body?: any;
|
|
timeout?: number;
|
|
parseAs?: "json" | "text" | "binary";
|
|
baseURL?: string;
|
|
}
|
|
|
|
interface FetchResponse<T = any> {
|
|
data: T;
|
|
status: number;
|
|
statusText: string;
|
|
headers: Headers;
|
|
}
|
|
|
|
const timeoutPromise = (ms: number) => {
|
|
return new Promise<never>((_, reject) =>
|
|
setTimeout(() => reject(new Error(`Request timed out after ${ms} ms`)), ms)
|
|
);
|
|
};
|
|
|
|
export const tauriFetch = async <T = any>({
|
|
url,
|
|
method = "GET",
|
|
headers = {},
|
|
body,
|
|
timeout = 30,
|
|
parseAs = "json",
|
|
baseURL = clientEnv.COCO_SERVER_URL
|
|
}: FetchRequestConfig): Promise<FetchResponse<T>> => {
|
|
const addLog = useLogStore.getState().addLog;
|
|
|
|
try {
|
|
const appStore = JSON.parse(localStorage.getItem("app-store") || "{}")
|
|
const endpoint_http = appStore?.state?.endpoint_http
|
|
baseURL = endpoint_http || clientEnv.COCO_SERVER_URL
|
|
console.log("baseURL", baseURL)
|
|
|
|
const authStore = JSON.parse(localStorage.getItem("auth-store") || "{}")
|
|
const auth = authStore?.state?.auth
|
|
console.log("auth", auth)
|
|
|
|
|
|
url = baseURL + url;
|
|
|
|
if (method !== "GET") {
|
|
headers["Content-Type"] = "application/json";
|
|
}
|
|
|
|
headers["X-API-TOKEN"] = headers["X-API-TOKEN"] || auth?.token || "";
|
|
|
|
// debug API
|
|
const requestInfo = {
|
|
url,
|
|
method,
|
|
headers,
|
|
body,
|
|
timeout,
|
|
parseAs,
|
|
};
|
|
|
|
const fetchPromise = fetch(url, {
|
|
method,
|
|
headers,
|
|
body,
|
|
});
|
|
|
|
const response = await Promise.race([
|
|
fetchPromise,
|
|
timeoutPromise(timeout * 1000),
|
|
]);
|
|
|
|
const statusText = response.ok ? "OK" : "Error";
|
|
|
|
let data: any;
|
|
if (parseAs === "json") {
|
|
data = await response.json();
|
|
} else if (parseAs === "text") {
|
|
data = await response.text();
|
|
} else {
|
|
data = await response.arrayBuffer();
|
|
}
|
|
|
|
// debug API
|
|
const log = {
|
|
request: requestInfo,
|
|
response: {
|
|
data,
|
|
status: response.status,
|
|
statusText,
|
|
headers: response.headers,
|
|
},
|
|
};
|
|
addLog(log);
|
|
|
|
return log.response;
|
|
} catch (error) {
|
|
console.error("Request failed:", error);
|
|
|
|
// debug API
|
|
const log = {
|
|
request: {
|
|
url,
|
|
method,
|
|
headers,
|
|
body,
|
|
timeout,
|
|
parseAs,
|
|
},
|
|
error,
|
|
};
|
|
addLog(log);
|
|
|
|
throw error;
|
|
}
|
|
};
|