Atif Zia
Atif Zia

Reputation: 793

NuxtJs axios module global config

I need to set global configuration for @nuxt/axios.

For example I need default Content-Type, Accept, Authorization header etc. and other configurations for all of my API calls. Is there any way to add these settings globally in nuxt to avoid repetition of code.

Upvotes: -1

Views: 1169

Answers (1)

kissu
kissu

Reputation: 46602

As stated here: https://axios.nuxtjs.org/extend
You can create a plugin and dump your axios configuration there like this nuxt.config.js

export default {
  plugins: [
    '~/plugins/axios'
  ]
}

/plugins/axios.js

export default ({ $axios, $config: { baseUrlIam, secretToken } }) => {
  $axios.defaults.baseURL = baseUrlIam
  $axios.defaults.headers.Authorization = secretToken
}

Also, setHeader may be useful.

Upvotes: 1

Related Questions