diff --git a/src/composables/useApi.ts b/src/composables/useApi.ts index 71496a5..9c736cb 100644 --- a/src/composables/useApi.ts +++ b/src/composables/useApi.ts @@ -1,4 +1,3 @@ -import { fetch } from '@tauri-apps/plugin-http'; import { useStore } from './useStore.ts'; import { useCrypto } from './useCrypto.ts'; @@ -9,6 +8,9 @@ type Settings = { password: string; }; +const isTauri = () => + typeof window !== 'undefined' && Boolean((window as typeof window & { __TAURI__?: unknown }).__TAURI__); + async function buildAuthHeader(): Promise { const {decrypt} = useCrypto(); const { getValue } = useStore(); @@ -36,10 +38,15 @@ export function useApi() { ...options.headers, } as Record; - const response = await fetch(url, { - ...options, - headers, - }); + const response = isTauri() + ? await (await import('@tauri-apps/plugin-http')).fetch(url, { + ...options, + headers, + }) + : await fetch(url, { + ...options, + headers, + }); if (!response.ok) { throw new Error(`API call failed: ${response.statusText}`); diff --git a/src/composables/useStore.ts b/src/composables/useStore.ts index 606a3a7..ed0231d 100644 --- a/src/composables/useStore.ts +++ b/src/composables/useStore.ts @@ -1,8 +1,13 @@ -import { load, type Store } from '@tauri-apps/plugin-store'; +import type { Store } from '@tauri-apps/plugin-store'; + let storePromise: Promise | null = null; -function getStore(): Promise { +const isTauri = () => + typeof window !== 'undefined' && Boolean((window as typeof window & { __TAURI__?: unknown }).__TAURI__); + +async function getStore(): Promise { if (!storePromise) { + const { load } = await import('@tauri-apps/plugin-store'); storePromise = load('store.json', { autoSave: false, defaults: {}, @@ -14,15 +19,28 @@ function getStore(): Promise { export function useStore() { const setValue = async (key: string, value: T) => { console.log('setValue',key,value); - const store = await getStore(); - await store.set(key, value); - await store.save(); + if (isTauri()) { + const store = await getStore(); + await store.set(key, value); + await store.save(); + return; + } + localStorage.setItem(key, JSON.stringify(value)); }; const getValue = async (key: string) => { console.log('getValue',key); - const store = await getStore(); - return store.get(key); + if (isTauri()) { + const store = await getStore(); + return store.get(key); + } + const rawValue = localStorage.getItem(key); + if (rawValue === null) return null; + try { + return JSON.parse(rawValue) as T; + } catch { + return null; + } }; return {