import type { ApiResponse } from "~/types/api" export function useApi() { const userStore = useUserStore() const config = useRuntimeConfig() function getHeaders(): Record { const headers: Record = {} if (userStore.token) { headers.Authorization = `Bearer ${userStore.token}` } return headers } async function unwrap(raw: any): Promise { if (raw && typeof raw === "object" && "code" in raw) { if (raw.code !== 0) { throw createError({ statusCode: raw.code, statusMessage: raw.msg || "Error" }) } return raw.data as T } return raw as T } async function get(path: string, params?: Record): Promise { const raw = await $fetch(`${config.public.apiBase}${path}`, { method: "GET", params, headers: getHeaders(), }) return unwrap(raw) } async function post(path: string, body?: Record): Promise { const raw = await $fetch(`${config.public.apiBase}${path}`, { method: "POST", body, headers: getHeaders(), }) return unwrap(raw) } async function del(path: string): Promise { const raw = await $fetch(`${config.public.apiBase}${path}`, { method: "DELETE", headers: getHeaders(), }) return unwrap(raw) } return { get, post, del } }