import { ref, onMounted } from 'vue'; export function useFetch(url) { const data = ref(null); const error = ref(null); const loading = ref(true); async function doFetch() { loading.value = true; error.value = null; try { const res = await fetch(url); if (!res.ok) { throw new Error(`HTTP error! status: ${res.status}`); } data.value = await res.json(); } catch (e) { error.value = e; } finally { loading.value = false; } } onMounted(() => { doFetch(); }); return { data, error, loading, doFetch }; }