Bug 40172: Replace jQuery with vanilla JS in js/fetch/http-client.js

So that we can reuse it from somewhere else.
The current need is to be able to reuse it from Cypress tests.

Test plan:
Try to hit several places where APIClient is used:
 * Syspref edition
 * Translation of item type description (be aware of bug 40161)
 * Delete of cover images
 * checkin, renew from the checkout list
 * etc.

Note that this does not affect Vue components.

Signed-off-by: David Cook <dcook@prosentient.com.au>
Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
(cherry picked from commit 44ed8d5400)
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
This commit is contained in:
Jonathan Druart 2025-06-16 09:39:38 +02:00 committed by Paul Derscheid
parent 9fcc9a30f3
commit d7784c1b27
No known key found for this signature in database
GPG Key ID: C25F9FD44CED595B
1 changed files with 33 additions and 7 deletions

View File

@ -1,16 +1,31 @@
function _ifDocumentAvailable(callback) {
if (typeof document !== "undefined" && document.getElementById) {
callback();
}
}
class Dialog {
constructor(options = {}) {}
_appendMessage(type, message) {
_ifDocumentAvailable(() => {
const messagesContainer = document.getElementById("messages");
if (!messagesContainer) {
return;
}
const htmlString =
`<div class="alert alert-${type}">%s</div>`.format(message);
messagesContainer.insertAdjacentHTML("beforeend", htmlString);
});
}
setMessage(message) {
$("#messages").append(
'<div class="alert alert-info">%s</div>'.format(message)
);
this._appendMessage("info", message);
}
setError(error) {
$("#messages").append(
'<div class="alert alert-warning">%s</div>'.format(error)
);
this._appendMessage("warning", error);
}
}
@ -22,7 +37,18 @@ class HttpClient {
"Content-Type": "application/json;charset=utf-8",
"X-Requested-With": "XMLHttpRequest",
};
this.csrf_token = $('meta[name="csrf-token"]').attr("content");
this.csrf_token = this._getCsrfToken(options);
}
_getCsrfToken(options) {
let token = null;
_ifDocumentAvailable(() => {
const metaTag = document.querySelector('meta[name="csrf-token"]');
if (metaTag) {
token = metaTag.getAttribute("content");
}
});
return token !== null ? token : options.csrfToken || null;
}
async _fetchJSON(