-
-
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.
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'; | |
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