import type { FetchOptions } from 'ofetch' import { $fetch, FetchError } from 'ofetch' export type ApiClient = { get(path: string, options?: FetchOptions<'json'>): Promise post(path: string, body?: unknown, options?: FetchOptions<'json'>): Promise } export const useApi = (): ApiClient => { const config = useRuntimeConfig() const baseURL = config.public.apiBase ?? '/api' const client = $fetch.create({ baseURL }) return { get(path: string, options?: FetchOptions<'json'>) { return client(path, { ...options, method: 'GET' }) }, post(path: string, body?: unknown, options?: FetchOptions<'json'>) { return client(path, { ...options, method: 'POST', body }) } } } export const getApiStatus = (error: unknown): number | null => { if (error && typeof error === 'object') { if (error instanceof FetchError) { return error.status ?? error.response?.status ?? null } const maybeResponse = (error as { response?: { status?: number } }).response if (typeof maybeResponse?.status === 'number') { return maybeResponse.status } const maybeStatus = (error as { status?: number }).status if (typeof maybeStatus === 'number') { return maybeStatus } } return null }