Created
August 5, 2020 20:09
-
-
Save marktellez/9a770bda280ad0b46ee0ca4efeef60e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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