Created
April 20, 2021 18:45
-
-
Save gegeke/5ab54d294c7a661925f9bfa96812ce79 to your computer and use it in GitHub Desktop.
Creating an API HUB 06
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 fetchAPI = async ({ url = null } = { url: null }) => { | |
if (url) { | |
try { | |
const response = await fetch(url) | |
if (!response.ok) { | |
throw { | |
isError: true, | |
data: [], | |
} | |
} | |
const json = await response.json() | |
return { | |
isError: false, | |
data: json, | |
} | |
} catch (err) { | |
// handling non-fetch errors in the unified way | |
if (err instanceof Error) { | |
throw { | |
isError: true, | |
data: [], | |
} | |
} else { | |
throw err | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment