Created
August 29, 2016 09:45
-
-
Save pparidans/45a4d0801b3a3ccc5085941c3f7f1af1 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
export function parseJSON(response) { | |
return response.json() | |
} | |
export function checkStatus(response) { | |
if (response.status >= 200 && response.status < 300) { | |
return response | |
} else { | |
const error = new Error(response.statusText) | |
error.response = response | |
throw error | |
} | |
} | |
export function fetchJSON(url, options = {}) { | |
const opts = { | |
...options, | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
} | |
} | |
return fetch(url, opts).then(checkStatus).then(parseJSON) | |
} | |
export function getJSON(url, options) { | |
return fetchJSON(url, options) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment