import { useState, useCallback } from 'react'; import type { AxiosResponse, AxiosError } from 'axios'; interface UseRequestOptions { manual?: boolean; onSuccess?: (data: T | undefined, response: AxiosResponse>) => void; onError?: (error: AxiosError) => void; onFinally?: () => void; } interface UseRequestResult { data: T | undefined; loading: boolean; success: boolean | null; error: AxiosError | null; run: (...args: any[]) => Promise> | undefined>; } function useRequest( apiFunction: (...args: any[]) => Promise>>, options: UseRequestOptions = {} ): UseRequestResult { const { manual = false, onSuccess, onError, onFinally } = options; const [data, setData] = useState(); const [loading, setLoading] = useState(!manual); const [success, setSuccess] = useState(null); const [error, setError] = useState(null); const run = useCallback( async (...args: any[]) => { try { setLoading(true); setSuccess(null); setError(null); const response = await apiFunction(...args); setData(response.data.data); setSuccess(true); if (onSuccess) { onSuccess(response.data.data, response); } return response; } catch (err) { const axiosError = err as AxiosError; setError(axiosError); setSuccess(false); if (onError) { onError(axiosError); } return undefined; } finally { setLoading(false); if (onFinally) { onFinally(); } } }, [apiFunction, onSuccess, onError, onFinally] ); // 自动执行 useState(() => { if (!manual) { run(); } }); return { data, loading, success, error, run }; } export default useRequest;