Browse Source

refactor: interceptor

pull/7444/head
Pranav C 10 months ago
parent
commit
339dd6db94
  1. 12
      packages/nc-gui/composables/useApi/interceptors.ts

12
packages/nc-gui/composables/useApi/interceptors.ts

@ -11,7 +11,9 @@ export function addAxiosInterceptors(api: Api<any>) {
const route = router.currentRoute const route = router.currentRoute
const optimisedQuery = useState('optimisedQuery', () => true) const optimisedQuery = useState('optimisedQuery', () => true)
api.instance.interceptors.request.use((config) => { const axiosInstance = api.instance;
axiosInstance.interceptors.request.use((config) => {
config.headers['xc-gui'] = 'true' config.headers['xc-gui'] = 'true'
if (state.token.value) config.headers['xc-auth'] = state.token.value if (state.token.value) config.headers['xc-auth'] = state.token.value
@ -38,7 +40,7 @@ export function addAxiosInterceptors(api: Api<any>) {
}) })
// Return a successful response back to the calling service // Return a successful response back to the calling service
api.instance.interceptors.response.use( axiosInstance.interceptors.response.use(
(response) => { (response) => {
return response return response
}, },
@ -70,7 +72,7 @@ export function addAxiosInterceptors(api: Api<any>) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const config = error.config const config = error.config
config.headers['xc-auth'] = token config.headers['xc-auth'] = token
api.instance axiosInstance
.request(config) .request(config)
.then((response) => { .then((response) => {
resolve(response) resolve(response)
@ -96,7 +98,7 @@ export function addAxiosInterceptors(api: Api<any>) {
} }
// Try request again with new token // Try request again with new token
return api.instance return axiosInstance
.post('/auth/token/refresh', null, { .post('/auth/token/refresh', null, {
withCredentials: true, withCredentials: true,
}) })
@ -111,7 +113,7 @@ export function addAxiosInterceptors(api: Api<any>) {
refreshTokenPromise = null refreshTokenPromise = null
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
api.instance axiosInstance
.request(config) .request(config)
.then((response) => { .then((response) => {
resolve(response) resolve(response)

Loading…
Cancel
Save