Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7398 | Rev 7400 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import Axios from 'axios'

export const cancelToken = Axios.CancelToken.source()

const axiosInstance = Axios.create({
  baseURL: 'https://dev-services.leaderslinked.com',
})

axiosInstance.all = Axios.all
axiosInstance.spread = Axios.spread

axiosInstance.upload = (url, data) => {
  return axiosInstance.post(url, new FormData(data), {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })
}

axiosInstance.interceptors.request.use(
  async (request) => {
    request.headers.ORIGIN = window.location.host

    if (['post', 'put', 'delete'].includes(request.method)) {
      try {
        const resp = await axiosInstance.get('/csrf')
        if (resp.data.success) {
          request.headers['X-CSRF-TOKEN'] = resp.data.data

          return request
        }
      } catch (err) {
        throw new Error(
          `Axios problem with request during pre-flight phase: ${err}.`
        )
      }
    }

    if (request.method === 'get') {
      let url = request.url

      if (!request.url.includes('t=')) {
        if (request.url.includes('?')) {
          url += '&'
        } else {
          url += '?'
        }
        url += 't=' + new Date().getTime()
      }

      request.url = url

      request.headers = {
        Accept: 'application/json',
        Vary: 'Accept',
        'Cache-Control': 'no-cache, no-store',
      }
    }
  },
  (error) => {
    return Promise.reject(error)
  }
)
export default axiosInstance