104 lines
2.4 KiB
TypeScript
104 lines
2.4 KiB
TypeScript
import axios, {AxiosError} from "axios";
|
|
|
|
export class PostRequestState
|
|
{
|
|
loading?: boolean = true;
|
|
}
|
|
|
|
export function usePost<R, P>(path: string, body?: P, onSuccess?: (response: R) => void, onError?: () => {})
|
|
{
|
|
const state = ref(new PostRequestState());
|
|
axios.post<R>(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<R>
|
|
{
|
|
constructor(
|
|
public loading: boolean,
|
|
public data?: R,
|
|
public error?: AxiosError
|
|
) {}
|
|
}
|
|
|
|
export function useGet<R>(path: string)
|
|
{
|
|
const state = ref(new GetRequestState<R>(true, undefined, undefined));
|
|
axios.get<R>(path)
|
|
.then((response) => {
|
|
state.value = new GetRequestState<R>(false, response.data, undefined);
|
|
})
|
|
.catch((error) => {
|
|
state.value = new GetRequestState<R>(false, undefined, error);
|
|
});
|
|
return state;
|
|
}
|
|
|
|
export class DeleteRequestState
|
|
{
|
|
constructor(
|
|
public loading: boolean
|
|
) {}
|
|
}
|
|
|
|
export function useDelete<R>(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<R, P>(path: string, body?: P, onSuccess?: (response: R) => void, onError?: () => {})
|
|
{
|
|
const state = ref(new PutRequestState());
|
|
axios.put<R>(path, body)
|
|
.then((response) => {
|
|
if (onSuccess)
|
|
{
|
|
onSuccess(response.data);
|
|
}
|
|
state.value.loading = false;
|
|
})
|
|
.catch(() => {
|
|
if (onError)
|
|
{
|
|
onError();
|
|
}
|
|
state.value.loading = false;
|
|
});
|
|
return state;
|
|
} |