feat(fc2a): add useApi composable, import store, extend system store with stats

useApi centralizes fetch error handling — every API call throws ApiError
on non-2xx with the parsed body attached, so components don't repeat the
'check response.ok' boilerplate.

import store wraps import settings, batch status, task list (with cursor
pagination), and the trigger/retry/clear admin actions. system store
gains a refreshStats() that powers the Settings overview tab.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-14 12:29:49 -04:00
parent e597a9300e
commit af9f42ef68
3 changed files with 170 additions and 4 deletions
+50
View File
@@ -0,0 +1,50 @@
// Thin fetch wrapper. Centralized so every call has consistent error handling
// and json parsing. Errors throw an ApiError with the response status + body.
export class ApiError extends Error {
constructor(message, status, body) {
super(message)
this.name = 'ApiError'
this.status = status
this.body = body
}
}
async function request(method, url, { body, params, signal } = {}) {
let fullUrl = url
if (params) {
const search = new URLSearchParams()
for (const [k, v] of Object.entries(params)) {
if (v !== undefined && v !== null) search.set(k, String(v))
}
const qs = search.toString()
if (qs) fullUrl += (url.includes('?') ? '&' : '?') + qs
}
const headers = {}
const init = { method, headers, signal }
if (body !== undefined) {
headers['Content-Type'] = 'application/json'
init.body = JSON.stringify(body)
}
const r = await fetch(fullUrl, init)
const text = await r.text()
let parsed
try { parsed = text ? JSON.parse(text) : null } catch { parsed = text }
if (!r.ok) {
const msg = (parsed && parsed.error) || `${r.status} ${r.statusText}`
throw new ApiError(msg, r.status, parsed)
}
return parsed
}
export function useApi() {
return {
get: (url, opts) => request('GET', url, opts),
post: (url, opts) => request('POST', url, opts),
patch: (url, opts) => request('PATCH', url, opts),
delete: (url, opts) => request('DELETE', url, opts)
}
}