import axios from 'axios'
import { VueAxios } from './http.axios'
import { requestCommon, errorCommon, responseCommon } from '@/http/http.interceptors'

// 创建 axios 实例
const service = axios.create({
  baseURL: process.env.VUE_APP_PROXY + process.env.VUE_APP_BASE_API,
  timeout: 1000 * 30
})

service.interceptors.request.use(
  config => {
    requestCommon(config)
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  async response => {
    try {
      return await responseCommon(response)
    } catch (e) {
      return Promise.reject(e)
    }
  },
  error => {
    errorCommon(error)
    return Promise.reject(error)
  }
)

const installer = {
  vm: {},
  install(Vue, router = {}) {
    Vue.use(VueAxios, router, service)
  }
}

export { installer as VueAxios, service as axios }
