Skip to content

Instantly share code, notes, and snippets.

@korayguclu
Forked from dbshoupe/Http.js
Created May 11, 2018 16:39
Show Gist options
  • Save korayguclu/3fa547193540843330615b84c4d375cb to your computer and use it in GitHub Desktop.
Save korayguclu/3fa547193540843330615b84c4d375cb to your computer and use it in GitHub Desktop.
Wrapper class for Axios in a Vue project that uses interceptor to inject token (stored using Vuex) into header for each request.
import axios from 'axios';
import store from "@/data/state"
class Http {
constructor() {
let service = axios.create({});
service.interceptors.request.use((config) => {
config.headers.common['x-access-token'] = store.state.token
return config
})
this.service = service;
}
redirectTo = (document, path) => {
document.location = path
}
get(path) {
return this.service.get(path)
}
patch(path, payload, callback) {
return this.service.request({
method: 'PATCH',
url: path,
responseType: 'json',
data: payload
}).then((response) => callback(response.status, response.data));
}
post(path, payload) {
return this.service.request({
method: 'POST',
url: path,
responseType: 'json',
data: payload
})
}
}
export default new Http;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment