Skip to content

Instantly share code, notes, and snippets.

@teyfix
Created June 23, 2021 06:10
Show Gist options
  • Save teyfix/640713b53f2b8ee5e51aea5257c0ad2a to your computer and use it in GitHub Desktop.
Save teyfix/640713b53f2b8ee5e51aea5257c0ad2a to your computer and use it in GitHub Desktop.
react - json placeholder api helper
const api = async (endpoint) => {
const url = new URL(
endpoint,
'https://jsonplaceholder.typicode.com',
).toString();
const response = await fetch(url);
if (response.status === 200) {
return response.json();
}
throw new Error(response.status.toString());
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment