* refactor: split mock-tauri.ts into focused modules The monolithic mock-tauri.ts (1927 lines, Code Health 8.8) is now split into 5 cohesive modules under src/mock-tauri/: - index.ts (barrel, isTauri, simplified mockInvoke) — 10.0 - mock-content.ts (markdown test data) — 10.0 - mock-entries.ts (VaultEntry[] + bulk generator) — 10.0 - mock-handlers.ts (command handlers + state) — 9.68 - vault-api.ts (vault API detection + proxy) — 10.0 Key improvements: - mockInvoke complexity reduced from cc=17 to trivial (vault API extracted to tryVaultApi, conditional routing via data-driven map) - save_settings complexity reduced via trimOrNull helper - All 726 tests pass unchanged, coverage remains above 70% - Public API surface is identical (no import changes needed) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * style: cargo fmt --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
/**
|
|
* Vault API detection and proxy for browser dev mode.
|
|
* When a local vault API server is running, routes read commands through it
|
|
* instead of returning hardcoded mock data.
|
|
*/
|
|
|
|
let vaultApiAvailable: boolean | null = null
|
|
|
|
async function checkVaultApi(): Promise<boolean> {
|
|
if (vaultApiAvailable !== null) return vaultApiAvailable
|
|
try {
|
|
const res = await fetch('/api/vault/ping', { signal: AbortSignal.timeout(500) })
|
|
vaultApiAvailable = res.ok
|
|
} catch {
|
|
vaultApiAvailable = false
|
|
}
|
|
console.info(`[mock-tauri] Vault API available: ${vaultApiAvailable}`)
|
|
return vaultApiAvailable
|
|
}
|
|
|
|
const VAULT_API_COMMANDS: Record<string, (args: Record<string, unknown>) => string | null> = {
|
|
list_vault: (args) => args.path ? `/api/vault/list?path=${encodeURIComponent(args.path as string)}` : null,
|
|
get_note_content: (args) => args.path ? `/api/vault/content?path=${encodeURIComponent(args.path as string)}` : null,
|
|
get_all_content: (args) => args.path ? `/api/vault/all-content?path=${encodeURIComponent(args.path as string)}` : null,
|
|
}
|
|
|
|
export async function tryVaultApi<T>(cmd: string, args?: Record<string, unknown>): Promise<T | undefined> {
|
|
const available = await checkVaultApi()
|
|
if (!available) return undefined
|
|
|
|
const urlBuilder = VAULT_API_COMMANDS[cmd]
|
|
if (!urlBuilder || !args) return undefined
|
|
|
|
const url = urlBuilder(args)
|
|
if (!url) return undefined
|
|
|
|
try {
|
|
const res = await fetch(url)
|
|
if (!res.ok) return undefined
|
|
const data = await res.json()
|
|
return (cmd === 'get_note_content' ? data.content : data) as T
|
|
} catch (err) {
|
|
console.warn(`[mock-tauri] Vault API call failed for ${cmd}, falling back to mock:`, err)
|
|
return undefined
|
|
}
|
|
}
|