Skip to content

Instantly share code, notes, and snippets.

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