import Axios, {AxiosRequestConfig, AxiosPromise } from 'axios'


const axios = Axios.create({
  baseURL: '/api',
})

axios.interceptors.response.use(function (config:AxiosRequestConfig) {
  return config
}, error => {
  const { config } = error.response
  if (!config.copeError) {
    alert(error.response.data.message)
  }
  return Promise.reject(error);
})

axios.interceptors.request.use(function (config: AxiosRequestConfig) {
  return config;
}, error => {
  return Promise.reject(error);
})

export const request = function <Request,Response> (config: AxiosRequestConfig<Request>): AxiosPromise<Response> {
  return axios({
    loading: false,
    copeError: false,
    isToken: true,
    ...config
  })
}

export interface  Options {
  loading?: boolean
  copeError?: boolean
}

export default request
