]> BookStack Code Mirror - bookstack/blobdiff - resources/js/services/http.js
OIDC Userinfo: Fixed issues with validation logic from changes
[bookstack] / resources / js / services / http.js
index d0d33e317df9230c2989bbcfefcd0bb2333032cf..49d5b6df4e37ab176d3601722dc3f01bab4d06bd 100644 (file)
@@ -45,6 +45,27 @@ export class HttpError extends Error {
 
 }
 
+/**
+ * @param {String} method
+ * @param {String} url
+ * @param {Object} events
+ * @return {XMLHttpRequest}
+ */
+export function createXMLHttpRequest(method, url, events = {}) {
+    const csrfToken = document.querySelector('meta[name=token]').getAttribute('content');
+    const req = new XMLHttpRequest();
+
+    for (const [eventName, callback] of Object.entries(events)) {
+        req.addEventListener(eventName, callback.bind(req));
+    }
+
+    req.open(method, url);
+    req.withCredentials = true;
+    req.setRequestHeader('X-CSRF-TOKEN', csrfToken);
+
+    return req;
+}
+
 /**
  * Create a new HTTP request, setting the required CSRF information
  * to communicate with the back-end. Parses & formats the response.