2023-02-07 16:22:35 -05:00
|
|
|
import axios from 'axios';
|
|
|
|
|
import useSWR from 'swr';
|
|
|
|
|
import useSWRMutation from 'swr/mutation';
|
|
|
|
|
|
|
|
|
|
const fetcher = (url) => fetch(url).then((res) => res.json());
|
|
|
|
|
|
|
|
|
|
const postRequest = async (url, { arg }) => {
|
|
|
|
|
return await axios.post(url, { arg });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const swr = (path) => useSWR(path, fetcher);
|
|
|
|
|
|
|
|
|
|
export default function manualSWR(path, type, successCallback) {
|
2023-02-07 19:07:48 -05:00
|
|
|
const options = {};
|
|
|
|
|
|
|
|
|
|
if (successCallback) {
|
|
|
|
|
options.onSuccess = successCallback;
|
|
|
|
|
}
|
2023-02-07 16:22:35 -05:00
|
|
|
const fetchFunction = type === 'get' ? fetcher : postRequest;
|
2023-02-07 19:07:48 -05:00
|
|
|
return useSWRMutation(path, fetchFunction, options);
|
2023-02-07 16:22:35 -05:00
|
|
|
};
|