Files
orion-visor/orion-ops-ui/src/hooks/request.ts
2023-10-25 10:26:14 +08:00

21 lines
615 B
TypeScript

import type { AxiosResponse } from 'axios';
import type { HttpResponse } from '@/api/interceptor';
import type { UnwrapRef } from 'vue';
import { ref } from 'vue';
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);
api().then((res) => {
response.value = res.data as unknown as UnwrapRef<T>;
}).finally(() => {
setLoading(false);
});
return { loading, response };
}