nuxt-community / axios-module

Secure and easy axios integration for Nuxt 2
https://axios.nuxtjs.org
MIT License
1.19k stars 245 forks source link

How to set the request timeout period ? #504

Open xxxs opened 3 years ago

xxxs commented 3 years ago

Use axios, How to set the request timeout period ?

export default ({ app, $axios, store, redirect, req }) => {
  const axios = $axios

  axios.defaults.timeout = 30000; // 30s This does not take effect
  axios.onRequest((config) => {
    const reqConfig = config
    const { token } = store.state
    if (token) {
      reqConfig.headers = {
        Authorization: `Bearer ${token}`
      }
    }
    return reqConfig
  })

  axios.onError((err) => {
    console.log(err.response)
    const apiUrl = err.response.config.url
    if (
      err.response.status === 401 &&
      err.response.data.error === 'invalid_token'
    ) {
      store.dispatch('userLogout')
    }

    return Promise.reject(err)
  })
}