import type { Ref } from 'vue' interface IUseRequestOptions { /** Whether to execute immediately */ immediate?: boolean /** Initial data */ initialData?: T } interface IUseRequestReturn { loading: Ref error: Ref data: Ref run: () => Promise } /** * useRequest is a customized request hook for handling async requests and responses. * @param func A function that executes async requests, returns a Promise containing response data. * @param options Object containing request options {immediate, initialData}. * @param options.immediate Whether to execute request immediately, defaults to false. * @param options.initialData Initial data, defaults to undefined. * @returns Returns an object {loading, error, data, run}, containing request loading status, error info, response data and manual trigger function. */ export default function useRequest( func: () => Promise>, options: IUseRequestOptions = { immediate: false }, ): IUseRequestReturn { const loading = ref(false) const error = ref(false) const data = ref(options.initialData) as Ref const run = async () => { loading.value = true return func() .then((res) => { data.value = res.data error.value = false return data.value }) .catch((err) => { error.value = err throw err }) .finally(() => { loading.value = false }) } options.immediate && run() return { loading, error, data, run } }