Sha256: 64b522b79624ba1c06ae1a8a5ae26d5b1049a1fb8914371b2e0e228f0be3805b

Contents?: true

Size: 953 Bytes

Versions: 15

Compression:

Stored size: 953 Bytes

Contents

export function csrfToken() {
  return document.querySelector("[name=csrf-token]").content;
}

function jsonFetchOptions() {
  return({ method: "POST",
           headers: { "Content-Type": "application/json; charset=utf-8",
                      "X-CSRF-Token": csrfToken() } });
}

export async function postJson(url, data) {
  const options = { ...jsonFetchOptions(), method: "POST" };
  if (data) {
    options.body = JSON.stringify(data);
  }
  const response = await fetch(url, options);
  return response.json();
}

export async function putJson(url, data) {
  const options = { ...jsonFetchOptions(), method: "PUT" };
  if (data) {
    options.body = JSON.stringify(data);
  }
  const response = await fetch(url, options);
  return response.json();
}

export async function post(url, data) {
  const response = await fetch(url, {
    method: "POST",
    body: data,
    headers: { "X-CSRF-Token": csrfToken() }
  });
  return response.json();
}

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
pages_core-3.12.1 app/javascript/lib/request.js
pages_core-3.12.0 app/javascript/lib/request.js
pages_core-3.11.3 app/javascript/lib/request.js
pages_core-3.11.2 app/javascript/lib/request.js
pages_core-3.11.1 app/javascript/lib/request.js
pages_core-3.11.0 app/javascript/lib/request.js
pages_core-3.10.2 app/javascript/lib/request.js
pages_core-3.10.1 app/javascript/lib/request.js
pages_core-3.9.2 app/javascript/lib/request.js
pages_core-3.9.1 app/javascript/lib/request.js
pages_core-3.9.0 app/javascript/lib/request.js
pages_core-3.8.3 app/javascript/lib/request.js
pages_core-3.8.2 app/javascript/lib/request.js
pages_core-3.8.1 app/javascript/lib/request.js
pages_core-3.8.0 app/javascript/lib/request.js