import axios from 'axios'

const instance = axios.create({
  baseURL: import.meta.env.VITE_API_URL,
})

instance.interceptors.response.use(
  // Any status code that lie within the range of 2xx cause this function to trigger
  function (response) {
    return response
  },

  // Any status codes that falls outside the range of 2xx cause this function to trigger
  function ({ response }) {
    const error = new Error(response.data?.message)

    error.code = response.data?.__type
    error.name = response.data?.__type
    error.statusCode = response.status

    return Promise.reject(error)
  },
)

export default instance
