import axios, {AxiosError} from "axios"; export class PostRequestState { loading?: boolean = true; } export function usePost(path: string, body?: any, onSuccess?: (response: R) => void, onError?: () => {}) { const state = ref(new PostRequestState()); axios.post(path, body) .then((response) => { console.log("jfdsl"); if (onSuccess) { onSuccess(response.data); } state.value.loading = false; }) .catch(() => { if (onError) { onError(); } state.value.loading = false; }); return state; } export class GetRequestState { constructor( public loading: boolean, public data?: R, public error?: AxiosError ) {} } export function useGet(path: string) { const state = ref(new GetRequestState(true, undefined, undefined)); axios.get(path) .then((response) => { state.value = new GetRequestState(false, response.data, undefined); }) .catch((error) => { state.value = new GetRequestState(false, undefined, error); }); return state; } export class DeleteRequestState { constructor( public loading: boolean ) {} } export function useDelete(path: string, onSuccess?: () => void, onError?: () => {}) { const state = ref(new DeleteRequestState(true)); axios.delete(path) .then(() => { if (onSuccess) { onSuccess(); } state.value = new DeleteRequestState(false); }) .catch(() => { if (onError) { onError(); } state.value = new DeleteRequestState(false); }); return state; } export class PutRequestState { loading?: boolean = true; } export function usePut(path: string, body?: any, onSuccess?: (response: R) => void, onError?: () => {}) { const state = ref(new PutRequestState()); axios.put(path, body) .then((response) => { if (onSuccess) { onSuccess(response.data); } state.value.loading = false; }) .catch(() => { if (onError) { onError(); } state.value.loading = false; }); return state; }