Created
June 23, 2021 06:10
-
-
Save teyfix/640713b53f2b8ee5e51aea5257c0ad2a to your computer and use it in GitHub Desktop.
react - json placeholder api helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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