Created
August 19, 2019 13:18
-
-
Save nandomoreirame/865b597a4bf110b387bbf72e4cfed131 to your computer and use it in GitHub Desktop.
Axios http interceptors with NProgress
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 NProgress from 'nprogress' | |
import config, { isProduction } from '@/config' | |
/** | |
* Axios methods | |
* Doc: https://kapeli.com/cheat_sheets/Axios.docset/Contents/Resources/Documents/index | |
* | |
* http | |
* .get(url, { params }) | |
* .post(url, { params }) | |
* .delete(url, { params }) | |
* .put(url, { params }) | |
* .patch(url, { params }) | |
* */ | |
export const API_PREFIX = 'api/admin' | |
const token = localStorage.getItem('token') | |
// create a new axios instance | |
const http = axios.create({ | |
baseURL: config.ApiBaseUrl, | |
headers: { | |
Authorization: token ? `Bearer ${token}` : '' | |
} | |
}) | |
// before a request is made start the nprogress | |
http.interceptors.request.use(config => { | |
if (!isProduction) { | |
Vue.$log.info('request', config) | |
} | |
NProgress.start() | |
return config | |
}) | |
// before a response is returned stop nprogress | |
http.interceptors.response.use(response => { | |
if (!isProduction) { | |
Vue.$log.info('response', response) | |
} | |
NProgress.done() | |
return response | |
}) | |
export default http |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment