Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created July 22, 2019 20:46
Show Gist options
  • Save BetterProgramming/b6b06c11f1040ffc4964b33188525889 to your computer and use it in GitHub Desktop.
Save BetterProgramming/b6b06c11f1040ffc4964b33188525889 to your computer and use it in GitHub Desktop.

Revisions

  1. BetterProgramming created this gist Jul 22, 2019.
    50 changes: 50 additions & 0 deletions useFetch.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    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;