Skip to content

Instantly share code, notes, and snippets.

@rsturim
Last active July 2, 2020 11:00
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 rsturim/514b3b248728e1715db2be15e4908b56 to your computer and use it in GitHub Desktop.
Save rsturim/514b3b248728e1715db2be15e4908b56 to your computer and use it in GitHub Desktop.
custom hook for fetch
const useFetch = endpoint => {
const defaultHeader = {
Accept: "application/json",
"Content-Type": "application/json"
};
const customFetch = (
url,
method = "GET",
body = false,
headers = defaultHeader
) => {
const options = {
method,
headers
};
if (body) options.body = JSON.stringify(body);
return fetch(url, options)
.then(response => response.json())
.catch(err => {
throw new Error(err);
});
};
const get = id => {
const url = `${endpoint}${id ? `/${id}` : ""}`;
return customFetch(url);
};
const post = (body = false) => {
if (!body) throw new Error("to make a post you must provide a body");
return customFetch(endpoint, "POST", body);
};
const put = (id = false, body = false) => {
if (!id || !body)
throw new Error("to make a put you must provide the id and the body");
const url = `${endpoint}/${id}`;
return customFetch(url, "PUT", body);
};
const del = (id = false) => {
if (!id)
throw new Error("to make a delete you must provide the id and the body");
const url = `${endpoint}/${id}`;
return customFetch(url, "DELETE");
};
return {
get,
post,
put,
del
};
};
export default useFetch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment