<commit_before>import axios from 'axios'

let links

export const setAJAXLinks = ({updatedLinks}) => {
  links = updatedLinks
}

// do not prefix route with basepath, ex. for external links
const addBasepath = (url, excludeBasepath) => {
  const basepath = window.basepath || ''

  return excludeBasepath ? url : `${basepath}${url}`
}

const generateResponseWithLinks = (response, newLinks) => {
  const {
    auth,
    logout,
    external,
    users,
    allUsers,
    organizations,
    me: meLink,
    config,
    environment,
  } = newLinks

  return {
    ...response,
    auth: {links: auth},
    logoutLink: logout,
    external,
    users,
    allUsers,
    organizations,
    meLink,
    config,
    environment,
  }
}

const AJAX = async (
  {url, resource, id, method = 'GET', data = {}, params = {}, headers = {}},
  {excludeBasepath} = {}
) => {
  try {
    if (!links) {
      console.error(
        `AJAX function has no links. Trying to reach url ${url}, resource ${resource}, id ${id}, method ${method}`
      )
    }

    url = addBasepath(url, excludeBasepath)

    if (resource) {
      url = id
        ? addBasepath(`${links[resource]}/${id}`, excludeBasepath)
        : addBasepath(`${links[resource]}`, excludeBasepath)
    }

    const response = await axios({
      url,
      method,
      data,
      params,
      headers,
    })

    // TODO: Just return the unadulterated response without grafting auth, me,
    // and logoutLink onto this object, once those are retrieved via their own
    // AJAX request and action creator.
    return links ? generateResponseWithLinks(response, links) : response
  } catch (error) {
    console.error(error)
    throw error
  }
}

export const getAJAX = async url => {
  try {
    return await axios({
      method: 'GET',
      url: addBasepath(url),
    })
  } catch (error) {
    console.error(error)
    throw error<commit_msg>Spread error on links object<commit_after>import axios from 'axios'

let links

export const setAJAXLinks = ({updatedLinks}) => {
  links = updatedLinks
}

// do not prefix route with basepath, ex. for external links
const addBasepath = (url, excludeBasepath) => {
  const basepath = window.basepath || ''

  return excludeBasepath ? url : `${basepath}${url}`
}

const generateResponseWithLinks = (response, newLinks) => {
  const {
    auth,
    logout,
    external,
    users,
    allUsers,
    organizations,
    me: meLink,
    config,
    environment,
  } = newLinks

  return {
    ...response,
    auth: {links: auth},
    logoutLink: logout,
    external,
    users,
    allUsers,
    organizations,
    meLink,
    config,
    environment,
  }
}

const AJAX = async (
  {url, resource, id, method = 'GET', data = {}, params = {}, headers = {}},
  {excludeBasepath} = {}
) => {
  try {
    if (!links) {
      console.error(
        `AJAX function has no links. Trying to reach url ${url}, resource ${resource}, id ${id}, method ${method}`
      )
    }

    url = addBasepath(url, excludeBasepath)

    if (resource) {
      url = id
        ? addBasepath(`${links[resource]}/${id}`, excludeBasepath)
        : addBasepath(`${links[resource]}`, excludeBasepath)
    }

    const response = await axios({
      url,
      method,
      data,
      params,
      headers,
    })

    // TODO: Just return the unadulterated response without grafting auth, me,
    // and logoutLink onto this object, once those are retrieved via their own
    // AJAX request and action creator.
    return links ? generateResponseWithLinks(response, links) : response
  } catch (error) {
    throw links ? generateResponseWithLinks(error, links) : error // eslint-disable-line no-throw-literal
  }
}

export const getAJAX = async url => {
  try {
    return await axios({
      method: 'GET',
      url: addBasepath(url),
    })
  } catch (error) {
    console.error(error)
    throw error
{'fraction_non_alphanumeric': 0.12422680412371134, 'fraction_numerical': 0.0, 'mean_word_length': 2.671712393566698, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1265352', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1193, 'n_words': 347}