Created
December 28, 2016 00:44
-
-
Save kylealwyn/1ce3fcc11796df9b7f39961ca8250df8 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
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