Skip to content

Instantly share code, notes, and snippets.

@raduGaspar
Last active April 1, 2019 18:24
Show Gist options
  • Save raduGaspar/b566b819adaa46dfb7c746ec974912c0 to your computer and use it in GitHub Desktop.
Save raduGaspar/b566b819adaa46dfb7c746ec974912c0 to your computer and use it in GitHub Desktop.
const API = (function () {
const url = 'https://purelyawespme.com/api'
const handleError = (res) => res.ok ? res : Promise.reject(res.statusText)
const doGet = (endpoint) => window.fetch(url + endpoint, {
method: 'GET',
headers: new Headers({
'Accept': 'application/json'
})
})
.then(handleError)
.catch(error => { throw new Error(error) })
const doPost = (endpoint, body) => window.fetch(url + endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body
})
.then(handleError)
.catch(error => { throw new Error(error) })
return {
post: doPost,
get: doGet
}
}())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment