Created
April 1, 2019 18:24
-
-
Save raduGaspar/aa34570e1d942f1e725d376007e3e487 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
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