import axios from 'axios'

export default async ({ app, Vue }) => {
  Vue.prototype.$apiBase = `//${location.hostname}:${location.port}/api/`
  Vue.prototype.$axios = axios
  axios.defaults.baseURL = Vue.prototype.$apiBase

  axios.interceptors.request.use(function (config) {
    const $state = Vue.prototype.$state

    if ($state.user.loggedIn) {
      config.headers.Authorization = $state.user.token
    }
    if (config.filter) {
      if (!config.params) config.params = {}
      config.params.filter = config.filter
    }
    return config
  })
}
