import { ref } from 'vue';

export function useRequest(apiFunction, immediate = true) {
  const data = ref(null);
  const loading = ref(false);
  const error = ref(null);

  const execute = async (...args) => {
    loading.value = true;
    error.value = null;
    try {
      data.value = await apiFunction(...args);
    } catch (err) {
      error.value = err;
    } finally {
      loading.value = false;
    }
  };

  if (immediate) {
    execute(); // 可选：是否立即执行
  }

  return {
    data,
    loading,
    error,
    execute, // 手动触发函数
  };
}