Sha256: b3d5482a823c28be3ce0e8c53c41659545da6e9c6c0fe186f522f85249808811

Contents?: true

Size: 1.66 KB

Versions: 31

Compression:

Stored size: 1.66 KB

Contents

function buildPromise(xhr) {
  return new Promise((resolve, reject) => {
    xhr.onload = () => {
      if (xhr.status >= 200 && xhr.status < 400) {
        try {
          resolve({
            data: JSON.parse(xhr.responseText),
            status: xhr.status
          })
        } catch (error) {
          reject(error)
        }
      } else {
        try {
          reject(JSON.parse(xhr.responseText))
        } catch (error) {
          reject(error)
        }
      }
    }
    xhr.onerror = () => {
      reject(new Error("An error occurred during the transaction"))
    }
  })
}

export function getToken() {
  const metaTag = document.querySelector('meta[name="csrf-token"]')
  return metaTag.attributes.content.textContent
}

export function get(url, params) {
  return ajax("GET", url, params)
}

export function patch(url, data) {
  return ajax("PATCH", url, data)
}

export function post(url, data, accept = "application/json") {
  return ajax("POST", url, data, accept)
}

export default function ajax(method, path, data, accept = "application/json") {
  const xhr = new XMLHttpRequest()
  const promise = buildPromise(xhr)
  const url = new URL(window.location.origin + path)

  if (data && method.toLowerCase() === "get") {
    url.search = new URLSearchParams(data).toString()
  }

  xhr.open(method, url.toString())
  xhr.setRequestHeader("Content-type", "application/json; charset=utf-8")
  xhr.setRequestHeader("Accept", accept)
  xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest")
  xhr.setRequestHeader("X-CSRF-Token", getToken())

  if (data && method.toLowerCase() !== "get") {
    xhr.send(JSON.stringify(data))
  } else {
    xhr.send()
  }

  return promise
}

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
alchemy_cms-7.3.5 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.2.8 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.1.13 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.3.4 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.3.3 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.3.2 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.2.7 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.3.1 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.3.0 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.2.6 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.2.5 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.1.12 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.2.4 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.1.11 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.2.3 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.1.10 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.2.2 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.1.9 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.2.1 app/javascript/alchemy_admin/utils/ajax.js
alchemy_cms-7.1.8 app/javascript/alchemy_admin/utils/ajax.js