Created
July 7, 2021 20:15
-
-
Save Josema/a3d94a519eeee50e52d31498f85c2e34 to your computer and use it in GitHub Desktop.
This file contains 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
async function fetchWrapper({ url, method, headers = {}, body }) { | |
const opts = { method, headers } | |
if (method === 'POST' && typeof body === 'string') { | |
headers['Content-Type'] = 'application/x-www-form-urlencoded' | |
opts.body = body | |
} | |
const response = await fetch(url, opts) | |
let output | |
try { | |
output = await response.clone().json() | |
} catch (e) { | |
output = await response.text() | |
} | |
if (typeof output == 'string') { | |
return output | |
} | |
if (output.hasOwnProperty('error')) { | |
throw output | |
} | |
return output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment