JSFiddle - React, Tailwind, and code Playground
by vaibhav2812
JavaScript
class API {
constructor() {
this.url = 'http://whatever.api/v1/';
}
_handleError(_res) {
return _res.ok ? _res : Promise.reject(_res.statusText);
}
get(_endpoint) {
return window.fetch(this.url + _endpoint, { method: 'GET' })
.then(this._handleError)
.then( res => res.json())
.catch( error => {
alert('So sad. There was an error.');
throw new Error(error);
});
}
}