Skip to content

Instantly share code, notes, and snippets.

@Siemko
Last active June 23, 2019 11:17
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 Siemko/b944e3bd29e16494970b459e85ae640d to your computer and use it in GitHub Desktop.
Save Siemko/b944e3bd29e16494970b459e85ae640d to your computer and use it in GitHub Desktop.
[async fetch api request] #js #javascript #http
export const request = async (url, method = 'GET', type = 'json', body = null) => {
const requestOptions = {
method: method,
headers: { 'Content-Type': 'application/json' },
};
if (body) {
requestOptions.body = JSON.stringify(body);
}
if (type === 'json') {
return await (await fetch(url, requestOptions)).json();
} else {
return await (await fetch(url, requestOptions)).status;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment