feat: implement tauri's fetch
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful

This commit is contained in:
SauceyRed 2025-07-06 03:26:49 +02:00
parent 182d68de17
commit 8ee86f1c6a
Signed by: sauceyred
GPG key ID: 2BF92EB6D8A5CCA7
2 changed files with 34 additions and 10 deletions

View file

@ -6,6 +6,11 @@
"main" "main"
], ],
"permissions": [ "permissions": [
"core:default" "core:default",
"http:default",
{
"identifier": "http:default",
"allow": [{ "url": "https://**" }]
}
] ]
} }

View file

@ -1,6 +1,13 @@
import type { NitroFetchRequest, NitroFetchOptions } from "nitropack"; import type { NitroFetchRequest, NitroFetchOptions } from "nitropack";
import { fetch as tauriFetch, type ClientOptions } from '@tauri-apps/plugin-http';
export default async <T>(path: string, options: NitroFetchOptions<string> = {}) => { declare global {
interface Window {
__TAURI_INTERNALS__: Record<string, unknown>;
}
}
export default async <T>(path: string, options: NitroFetchOptions<string> | (RequestInit & ClientOptions) = {}) => {
console.log("path received:", path); console.log("path received:", path);
if (!path.startsWith("/")) { if (!path.startsWith("/")) {
path = "/" + path; path = "/" + path;
@ -18,7 +25,7 @@ export default async <T>(path: string, options: NitroFetchOptions<string> = {})
return; return;
} }
console.log("path:", path) console.log("path:", path)
const { revoke, refresh } = useAuth(); const { logout, refresh } = useAuth();
let headers: HeadersInit = {}; let headers: HeadersInit = {};
@ -38,12 +45,24 @@ export default async <T>(path: string, options: NitroFetchOptions<string> = {})
}; };
} }
try { try {
console.log("fetching:", URL.parse(apiBase + path)); let res;
const res = await $fetch<T>(URL.parse(apiBase + path)!.href, { if (window.__TAURI_INTERNALS__) {
...options, // Use Tauri's HTTP client
headers, console.log("USING TAURI'S FUCKING BITCHASS FETCH SHIT")
credentials: "include" res = await tauriFetch(URL.parse(apiBase + path)!.href, {
}); ...options,
headers,
credentials: "include"
} as RequestInit & ClientOptions)
} else {
console.log("USING GOATED EPIC NUXT FETCH")
console.log("fetching:", URL.parse(apiBase + path));
res = await $fetch<T>(URL.parse(apiBase + path)!.href, {
...options,
headers,
credentials: "include"
} as NitroFetchOptions<string>);
}
return res; return res;
} catch (error: any) { } catch (error: any) {
@ -62,7 +81,7 @@ export default async <T>(path: string, options: NitroFetchOptions<string> = {})
console.log("Refresh returned 401"); console.log("Refresh returned 401");
reauthFailed = true; reauthFailed = true;
console.log("Revoking"); console.log("Revoking");
await revoke(); await logout();
console.log("Redirecting to login"); console.log("Redirecting to login");
await navigateTo("/login"); await navigateTo("/login");
console.log("redirected"); console.log("redirected");