feat: 完成登录与栅格下载桌面端
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
export const API_BASE = import.meta.env.DEV ? "" : "http://127.0.0.1:8765";
|
||||
|
||||
export class ApiError extends Error {
|
||||
status: number;
|
||||
|
||||
constructor(status: number, message: string) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
|
||||
export async function api<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
let response: Response;
|
||||
try {
|
||||
const headers = new Headers(init?.headers);
|
||||
if (init?.body && !(init.body instanceof FormData) && !headers.has("Content-Type")) {
|
||||
headers.set("Content-Type", "application/json");
|
||||
}
|
||||
response = await fetch(`${API_BASE}${path}`, { ...init, headers });
|
||||
} catch {
|
||||
throw new ApiError(0, "连接失败");
|
||||
}
|
||||
const text = await response.text();
|
||||
const data = text ? JSON.parse(text) : null;
|
||||
if (!response.ok) {
|
||||
const detail = data?.detail;
|
||||
const message = typeof detail === "string" ? detail : "请求失败";
|
||||
throw new ApiError(response.status, message);
|
||||
}
|
||||
return data as T;
|
||||
}
|
||||
|
||||
export function health() {
|
||||
return api<{ ok: boolean }>("/api/health");
|
||||
}
|
||||
|
||||
export async function openLocal(path: string) {
|
||||
if (!("__TAURI_INTERNALS__" in window)) return false;
|
||||
const { openPath } = await import("@tauri-apps/plugin-opener");
|
||||
await openPath(path);
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function chooseDirectory() {
|
||||
if (!("__TAURI_INTERNALS__" in window)) return null;
|
||||
const { open } = await import("@tauri-apps/plugin-dialog");
|
||||
const selected = await open({ directory: true, multiple: false });
|
||||
return typeof selected === "string" ? selected : null;
|
||||
}
|
||||
Reference in New Issue
Block a user