import type { FetchOptions } from 'ofetch' export function useApi() { const config = useRuntimeConfig() const apiBase = config.public.apiBase as string const fetch = ofetch.create({ baseURL: apiBase, credentials: 'include', headers: { 'Content-Type': 'application/json', }, }) const request = async (url: string, options?: FetchOptions): Promise => { try { return await fetch(url, { ...options, credentials: 'include', }) } catch (error: any) { if (error.status === 401) { const authStore = useAuthStore() authStore.clearUser() } throw error } } return { get: (url: string, options?: FetchOptions) => request(url, { ...options, method: 'GET' }), post: (url: string, body?: any, options?: FetchOptions) => request(url, { ...options, method: 'POST', body }), put: (url: string, body?: any, options?: FetchOptions) => request(url, { ...options, method: 'PUT', body }), delete: (url: string, options?: FetchOptions) => request(url, { ...options, method: 'DELETE' }), } }