Skip to content

Instantly share code, notes, and snippets.

@pazthor
Last active November 6, 2019 21:17
Show Gist options
  • Save pazthor/ac7b920d56bbde0a621ed4cb5aa9058e to your computer and use it in GitHub Desktop.
Save pazthor/ac7b920d56bbde0a621ed4cb5aa9058e to your computer and use it in GitHub Desktop.
export const FetchPost = async (url = "", data = {}, sessionToken = "") => {
const errorFetchCode = 2;
const errorServerCode = 1;
const notErros = 0;
const POST = "POST";
const errorFetch = "TypeError: Failed to fetch";
const rawResponse = await fetch(url, {
method: POST,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `${sessionToken}`
},
body: JSON.stringify(data)
})
.then(dataJson => {
return { data: dataJson, error: notErros };
})
.catch(err => {
const errorCode = err.toString() === errorFetch ? errorFetchCode : errorServerCode;
return { err: err, error: errorCode };
});
const result = await rawResponse;
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment