2023-07-24 10:05:07 +08:00
|
|
|
import { ref, UnwrapRef } from 'vue';
|
|
|
|
|
import { AxiosResponse } from 'axios';
|
|
|
|
|
import { HttpResponse } from '@/api/interceptor';
|
|
|
|
|
import useLoading from './loading';
|
|
|
|
|
|
|
|
|
|
export default function useRequest<T>(
|
|
|
|
|
api: () => Promise<AxiosResponse<HttpResponse>>,
|
|
|
|
|
defaultValue = [] as unknown as T,
|
|
|
|
|
isLoading = true
|
|
|
|
|
) {
|
|
|
|
|
const { loading, setLoading } = useLoading(isLoading);
|
|
|
|
|
const response = ref<T>(defaultValue);
|
2023-07-27 18:48:15 +08:00
|
|
|
api().then((res) => {
|
|
|
|
|
response.value = res.data as unknown as UnwrapRef<T>;
|
|
|
|
|
}).finally(() => {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
});
|
2023-07-24 10:05:07 +08:00
|
|
|
return { loading, response };
|
|
|
|
|
}
|