Last active
March 7, 2019 05:58
-
-
Save jochasinga/d8370714eec9bfa70ecd7b3d0cb9f8c0 to your computer and use it in GitHub Desktop.
This file contains 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
const baseConfig = { | |
mode: "cors", | |
cache: "no-cache", | |
credentials: "same-origin", | |
headers: { | |
"Content-Type": "application/json; charset=utf-8", | |
}, | |
redirect: "follow", | |
referrer: "no-referrer", | |
}; | |
const send = (method, payload) => ( | |
async function(uri, config) { | |
// Create an array of source config objects to be merged. | |
let sources = [config]; | |
if (method === "POST") { | |
sources.push({ body: JSON.stringify(payload) }); | |
} | |
config = Object.assign({ | |
method: method, | |
...baseConfig, | |
}, ...sources); | |
return await fetch(uri, config); | |
} | |
); | |
const get = (uri, config = {}) => ( | |
send("GET")(uri, config) | |
); | |
const post = (uri, data, config = {}) => ( | |
send("POST", data)(uri, config) | |
); | |
export {get, post}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment