import { ref } from 'vue' // Owns the loading/error lifecycle shared by most Pinia stores. `run` // flips loading on, clears error, runs `fn`, captures any throw into error // (swallowed so callers can read `store.error`), and always resets loading. // Returns fn's resolved value on success, or undefined if it threw. // // `errorAs` matches each store's existing convention so migrations don't // change what `store.error` holds: 'raw' keeps the thrown object (the // ApiError, with .status/.body), 'message' keeps just the string. export function useAsyncAction({ errorAs = 'raw' } = {}) { const loading = ref(false) const error = ref(null) async function run(fn) { loading.value = true error.value = null try { return await fn() } catch (e) { error.value = errorAs === 'message' ? (e?.message ?? String(e)) : e } finally { loading.value = false } } return { loading, error, run } }