Skip to content

Instantly share code, notes, and snippets.

@jochasinga
Last active March 7, 2019 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jochasinga/d8370714eec9bfa70ecd7b3d0cb9f8c0 to your computer and use it in GitHub Desktop.
Save jochasinga/d8370714eec9bfa70ecd7b3d0cb9f8c0 to your computer and use it in GitHub Desktop.
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