69 lines
1.5 KiB
JavaScript
69 lines
1.5 KiB
JavaScript
import axios from "axios";
|
|
import i18next from "i18next";
|
|
|
|
function getHeaders() {
|
|
return {
|
|
"Content-Type": "application/json",
|
|
Authorization: "Basic ***REMOVED***",
|
|
"Accept-Language": `${i18next.language}`
|
|
};
|
|
}
|
|
|
|
function internalRequest(url, options) {
|
|
return axios
|
|
.request(url, options)
|
|
.then((res) => res.data)
|
|
.catch(function (error) {
|
|
if (error.response && error.response.data) {
|
|
throw (
|
|
{
|
|
...error.response.data,
|
|
message: error.response.data.detail || error.response.data.title
|
|
} || error
|
|
);
|
|
}
|
|
// The request was made but no response was received
|
|
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
|
// http.ClientRequest in node.js
|
|
throw error;
|
|
});
|
|
}
|
|
|
|
export function post(url, data) {
|
|
const options = {
|
|
method: "post",
|
|
data: JSON.stringify(data),
|
|
headers: getHeaders()
|
|
};
|
|
|
|
return internalRequest(url, options);
|
|
}
|
|
|
|
export function put(url, data) {
|
|
const options = {
|
|
method: "put",
|
|
data: JSON.stringify(data),
|
|
headers: getHeaders()
|
|
};
|
|
|
|
return internalRequest(url, options);
|
|
}
|
|
|
|
export function del(url, data) {
|
|
const options = {
|
|
method: "delete",
|
|
data: JSON.stringify(data),
|
|
headers: getHeaders()
|
|
};
|
|
|
|
return internalRequest(url, options);
|
|
}
|
|
|
|
export function get(url) {
|
|
const options = {
|
|
method: "GET",
|
|
headers: getHeaders()
|
|
};
|
|
return internalRequest(url, options);
|
|
}
|