Skip to content

Instantly share code, notes, and snippets.

@jochasinga jochasinga/refactor3.js
Last active Mar 7, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.