import { invoke } from "@tauri-apps/api/core"; // Thin wrappers over the generic Rust bridge (community_api.rs): the client // passes the server's own `/api/v1/...` path, Rust resolves the base URL + // signs the request with the same auth every other client call uses. See // desktop/src-tauri/src/community_api.rs for why this is generic rather than // one typed Tauri command per endpoint. // // On a non-2xx response the Rust side rejects with a stringified // RemoteAccessError::InvalidResponse whose message is the server's own // `statusMessage`/`message` -- callers can show `(e as string)` directly. export function apiGet( path: string, query?: Record, ): Promise { const q = query ? Object.entries(query) .filter(([, v]) => v !== undefined) .map(([k, v]) => [k, String(v)] as [string, string]) : undefined; return invoke("api_get", { path, query: q }); } // Same as apiGet, but for `/api/v1/client/*` routes built on // `defineClientEventHandler` (news, library, game manifests, ...). Those // routes authenticate with the desktop client's own short-lived JWT // (Rust's generate_authorization_header), NOT the aclManager webtoken // apiGet's api_get command mints -- sending them a webtoken 403s no matter // what ACLs it carries, since defineClientEventHandler never looks at // ACLs at all. See desktop/src-tauri/src/community_api.rs's module header // for the full explanation of why there are two auth paths here. export function apiGetClient( path: string, query?: Record, ): Promise { const q = query ? Object.entries(query) .filter(([, v]) => v !== undefined) .map(([k, v]) => [k, String(v)] as [string, string]) : undefined; return invoke("api_get_jwt", { path, query: q }); } export function apiPost(path: string, body?: unknown): Promise { return invoke("api_post", { path, body: body ?? {} }); } export function apiDelete(path: string): Promise { return invoke("api_delete", { path }); }