Last active
May 26, 2020 17:35
-
-
Save nirnejak/7ec04d6777381e80aecd1452422195b9 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 Vue from 'vue' | |
import axios from 'axios' | |
import NProgress from 'nprogress' | |
import * as Sentry from '@sentry/browser' | |
if (process.env.NODE_ENV === 'production') { | |
axios.defaults.baseURL = 'https://api.myserver.com/api' | |
} else { | |
axios.defaults.baseURL = 'https://mystagingserver.herokuapp.com/api' | |
// axios.defaults.baseURL = 'http://localhost:3000/api' | |
} | |
export const setAuthToken = token => { | |
axios.defaults.headers.common['Authorization'] = token | |
} | |
export const removeAuthToken = () => { | |
axios.defaults.headers.common['Authorization'] = '' | |
} | |
axios.defaults.headers.common['Authorization'] = localStorage.token | |
axios.interceptors.request.use(config => { | |
NProgress.start() | |
return config | |
}) | |
axios.interceptors.response.use( | |
response => { | |
NProgress.done() | |
return response | |
}, | |
error => { | |
NProgress.done() | |
if (error.response) { | |
switch (error.response.status) { | |
case 409: | |
// { title: `Credentials Expired`, text: `Database credentials are expired` } | |
window.location.href = `/update-database/${localStorage.getItem("currentDatabase")}` | |
break; | |
case 401: | |
case 403: | |
// { title: `${error.response.status} Error`, text: `${error.response.status} Error` } | |
window.location.href = '/logout/' | |
break; | |
case 404: | |
if (process.env.NODE_ENV === 'production' && navigator.onLine) { | |
Sentry.captureException(error) | |
window.location.href = `/error/${error.response.status}` | |
} else { | |
// { title: '404', text: error.response.data.error || '404 Not Found' } | |
} | |
break; | |
default: | |
if (process.env.NODE_ENV === 'production' && navigator.onLine) { | |
Sentry.captureException(error) | |
if (error.response.status.toString().startsWith('5')) { | |
window.location.href = `/error/${error.response.status}` | |
} | |
} else { | |
// { text: error.response.data.error } | |
} | |
} | |
} | |
if (error.message === 'Network Error' && process.env.NODE_ENV === 'production') { | |
Sentry.captureException(error) | |
// { text: error.message } | |
window.location.href = '/error/0' | |
} | |
if (error.code === 'ECONNABORTED') { | |
Sentry.captureException(error) | |
window.location.href = '/error/1' | |
// { text: "Request Timeout" } | |
} | |
return Promise.reject(error) | |
} | |
) | |
export default axios |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment