import { ref } from 'vue' import type { Ref } from 'vue' import { handleApiError } from '../utils/error-handling' import type { NormalizedApiError } from '../utils/error-handling' export interface UseApiReturn { data: Ref loading: Ref error: Ref execute: (...args: unknown[]) => Promise reset: () => void } /** * Generic API call wrapper composable. * Manages loading, error, and data state for an async API function. * * @param apiFn - An async function that performs the API call * @returns Reactive refs for data, loading, and error plus an execute function */ export function useApi( apiFn: (...args: never[]) => Promise ): UseApiReturn { const data = ref(null) as Ref const loading = ref(false) const error = ref(null) as Ref async function execute(...args: unknown[]): Promise { loading.value = true error.value = null try { const result = await (apiFn as (...a: unknown[]) => Promise)(...args) data.value = result return result } catch (err: unknown) { const normalized = handleApiError(err) error.value = normalized return null } finally { loading.value = false } } function reset(): void { data.value = null loading.value = false error.value = null } return { data, loading, error, execute, reset } }