Skip to content

Instantly share code, notes, and snippets.

@romannmk
Created January 17, 2019 10:14
Show Gist options
  • Save romannmk/0471bdb6e2c0b9858cc0ef624cc54141 to your computer and use it in GitHub Desktop.
Save romannmk/0471bdb6e2c0b9858cc0ef624cc54141 to your computer and use it in GitHub Desktop.
call to api function
export default async (method, url, payload) => {
try {
const config = {
...(method !== 'GET' && { body: JSON.stringify(payload) }),
method: method,
headers: {
'Content-Type': 'application/json',
...(token && { Bearer: `token` })
}
};
const res = await fetch(`${url}`, config);
const data = await res.json();
if (!res.ok) throw data;
return data;
} catch (error) {
throw error;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment