Skip to content

Instantly share code, notes, and snippets.

@kylealwyn
Created December 28, 2016 00:44
Show Gist options
  • Save kylealwyn/1ce3fcc11796df9b7f39961ca8250df8 to your computer and use it in GitHub Desktop.
Save kylealwyn/1ce3fcc11796df9b7f39961ca8250df8 to your computer and use it in GitHub Desktop.
import axios from 'axios';
class Http {
constructor() {
this.axios = axios.create({
baseURL: 'http://localhost:4567',
timeout: 5000
});
}
request(method, url, options = {}) {
const requestConfig = Object.assign({}, {
headers: {}
}, options);
if (localStorage.getItem('token')) {
requestConfig.headers.Authorization = localStorage.getItem('token');
}
return this.axios({
url,
method,
...requestConfig
});
}
get(url, options = {}) {
return this.request('get', url, options);
}
post(url, data, options = {}) {
return this.request('post', url, Object.assign({}, options, data));
}
delete(url, options = {}) {
return this.request('delete', url, options);
}
update(url, data, options = {}) {
return this.request('put', url, Object.assign({}, options, data));
}
}
export default new Http();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment