Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Axios to return value from interceptor without actually making call

I want to check for value in my Axios interceptor for a value in the params. If it matches a condition, don't make the call, but return an empty array.

Here is my entire Axios config:


import Axios from 'axios'
import store from '@/store'

const AxiosConfig = Axios.create({
    baseURL: process.env.VUE_APP_BASE_API_URL
})

const addToken = (config) => {
    const token = store.getters['AuthModule/getAuthToken']
    if (token) {
        config.headers.Authorization = `Bearer ${token}`
    }
}

let numOfRequests = 0

AxiosConfig.interceptors.request.use(
    (config) => {
        addToken(config)
        if (config.params.autocomplete.length < 2){
          // don't make the call and return an empty [] like I did make the call.
        } 
        store.commit('SET_LOADER_TO_STATE', true)
        numOfRequests++
        return config
    },
    (error) => {
        store.commit('SET_LOADER_TO_STATE', false)
        numOfRequests--
        return Promise.reject(error)
    }
)

AxiosConfig.interceptors.response.use(
    (response) => {
        numOfRequests--
        if (numOfRequests === 0) {
            store.commit('SET_LOADER_TO_STATE', false)
        }
        return response
    },
    (error) => {
        store.commit('SET_LOADER_TO_STATE', false)
        numOfRequests--
        if (numOfRequests === 0) {
            store.commit('SET_LOADER_TO_STATE', false)
        }
        if (
            error.response.status === 401 &&
            error?.response?.data?.error?.message === 'Unauthenticated.'
        ) {
            store.dispatch('AuthModule/logoutUser')
            return
        }
        if (error?.response?.data?.error) {
            throw new Error(
                error?.response?.data?.error.message ||
                    error.response.data.error ||
                    error.response.data
            )
        } else if (error?.response?.data?.errors) {
            const errors = {}
            const errorsList = error?.response?.data?.errors

            for (const key in errorsList) {
                if (Object.prototype.hasOwnProperty.call(errorsList, key)) {
                    errors[key] = errorsList[key][0]
                }
            }

            throw new Error(JSON.stringify(errors))
        } else if (error) {
            throw new Error(error.response.data.message)
        } else {
            throw new Error('Network Error')
        }
    }
)

export default AxiosConfig

Is this possible?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!