Skip to content

Instantly share code, notes, and snippets.

@marktellez
Created August 5, 2020 20:09
Show Gist options
  • Save marktellez/9a770bda280ad0b46ee0ca4efeef60e8 to your computer and use it in GitHub Desktop.
Save marktellez/9a770bda280ad0b46ee0ca4efeef60e8 to your computer and use it in GitHub Desktop.
import fetch from "isomorphic-fetch";
require("es6-promises");
import AbortController from "abort-controller";
function apiFetch(signal, path, method, data) {
const options = {};
const headers = {
"Content-Type": "application/json",
Accepts: "application/json",
};
if (!method) method = "GET";
if (method.toUpperCase() !== "GET") {
options["body"] = JSON.stringify(data);
}
const jwt = localStorage.getItem("jwt");
if (jwt && jwt.length > 0) {
headers["Authorization"] = `Bearer ${jwt}`;
}
return fetch(`//${location.host}/${path}`, {
method: method.toUpperCase(),
headers,
signal,
...options,
}).then((response) => response.json());
}
const abortController = new AbortController();
export { abortController };
export default apiFetch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment