Skip to content

Instantly share code, notes, and snippets.

@pparidans
Created August 29, 2016 09:45
Show Gist options
  • Save pparidans/45a4d0801b3a3ccc5085941c3f7f1af1 to your computer and use it in GitHub Desktop.
Save pparidans/45a4d0801b3a3ccc5085941c3f7f1af1 to your computer and use it in GitHub Desktop.
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