fetch api update

master
Tudor Stanciu 2020-05-06 18:54:17 +03:00
parent c591291917
commit 542a332f40
2 changed files with 60 additions and 9 deletions

56
src/api/api.js Normal file
View File

@ -0,0 +1,56 @@
function getHeaders() {
const headers = new Headers();
headers.append("Accept", "application/json");
headers.append("Content-Type", "application/json");
return headers;
}
function internalFetch(url, options) {
return fetch(url, options).then((res) =>
res.text().then((text) => {
let t = text ? JSON.parse(text) : res.statusText;
if (!res.ok) {
if (res.status === 404) throw new Error(t || "Not found");
if (res.status >= 400 && res.status < 600 && t) throw t;
throw new Error(t || "Unknown error");
}
return t;
})
);
}
export function post(url, body) {
const options = {
method: "POST",
body: JSON.stringify(body),
headers: getHeaders()
};
return internalFetch(url, options);
}
export function put(url, body) {
const options = {
method: "PUT",
body: JSON.stringify(body),
headers: getHeaders()
};
return internalFetch(url, options);
}
export function get(url) {
const options = {
method: "GET",
headers: getHeaders()
};
return internalFetch(url, options);
}
export function del(url) {
const options = {
method: "DELETE",
headers: getHeaders()
};
return internalFetch(url, options);
}

View File

@ -1,14 +1,9 @@
import { handleResponse, handleError } from "../../api/apiUtils"; import { get } from "../../api/api";
const baseUrl = process.env.REVERSE_PROXY_API_URL + "/system"; const baseUrl = process.env.REVERSE_PROXY_API_URL + "/system";
const getSystemDateTime = () => const getSystemDateTime = () => get(`${baseUrl}/datetime`);
fetch(`${baseUrl}/datetime`).then(handleResponse).catch(handleError); const getSystemVersion = () => get(`${baseUrl}/version`);
const getReleaseNotes = () => get(`${baseUrl}/release-notes`);
const getSystemVersion = () =>
fetch(`${baseUrl}/version`).then(handleResponse).catch(handleError);
const getReleaseNotes = () =>
fetch(`${baseUrl}/release-notes`).then(handleResponse).catch(handleError);
export default { export default {
getSystemDateTime, getSystemDateTime,