hiuphub-gamified
Version:
app of gamified
44 lines (39 loc) • 1.11 kB
JavaScript
import axios from "axios";
const defaults = {
error: {
code: "INTERNAL_ERROR",
message:
"Something went wrong. Please check your internet connection or contact our support.",
status: 503,
data: {},
},
};
const api = (method, url, variables) =>
new Promise((resolve, reject) => {
axios({
url,
method,
params: method === "get" ? variables : undefined,
data: method !== "get" ? variables : undefined,
})
.then(response => resolve(response.data))
.catch(error => {
console.log({ axiosApiError: error });
if (error.response) {
if (error.response.data.error === "jwt expired") {
// do something better here
}
reject(error.response.data.error);
} else {
reject(defaults.error);
}
});
});
const API = {
get: (...args) => api("get", ...args),
post: (...args) => api("post", ...args),
put: (...args) => api("put", ...args),
patch: (...args) => api("patch", ...args),
delete: (...args) => api("delete", ...args),
};
export { API as default };