/** * HTTP for native shells. WebView fetch() is blocked by CORS on Authentik * (/auth/* has no Access-Control-Allow-Origin). Rust reqwest bypasses that. */ import { invoke } from "@/lib/native/bridge" import { isTauriRuntime } from "@/lib/platform" type NativeHttpResponse = { status: number body: string } function headersRecord(init?: RequestInit): Record { if (!init?.headers) return {} const out: Record = {} const h = new Headers(init.headers) h.forEach((value, key) => { out[key] = value }) return out } async function nativeHttpRequest( url: string, init?: RequestInit ): Promise { const result = await invoke("plugin:ulti-core|http_request", { url, method: init?.method ?? "GET", headers: headersRecord(init), body: typeof init?.body === "string" ? init.body : undefined, }) if (!result) return null return new Response(result.body, { status: result.status }) } /** fetch() on web; native HTTP in Tauri (no WebView CORS fallback). */ export async function runtimeFetch( url: string, init?: RequestInit ): Promise { if (isTauriRuntime()) { const res = await nativeHttpRequest(url, init) if (res) return res throw new TypeError(`Native HTTP failed: ${url}`) } return fetch(url, init) }