1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-11 17:51:01 +00:00

refactor(js): remove RequestBuilder

This commit is contained in:
Frédéric Guillot 2025-08-02 15:07:56 -07:00
parent bbe3c2ea71
commit 4910f1f0f4
4 changed files with 28 additions and 86 deletions

View file

@ -1,14 +1,28 @@
// Sentinel values for specific list navigation // Sentinel values for specific list navigation.
const TOP = 9999; const TOP = 9999;
const BOTTOM = -9999; const BOTTOM = -9999;
/** /**
* Get the CSRF token from the HTML document. * Send a POST request to the specified URL with the given body.
* *
* @returns {string} The CSRF token. * @param {string} url - The URL to send the request to.
* @param {Object} [body] - The body of the request (optional).
* @returns {Promise<Response>} The response from the fetch request.
*/ */
function getCsrfToken() { function sendPOSTRequest(url, body = null) {
return document.body.dataset.csrfToken || ""; const options = {
method: "POST",
headers: {
"X-Csrf-Token": document.body.dataset.csrfToken || ""
}
};
if (body !== null) {
options.headers["Content-Type"] = "application/json";
options.body = JSON.stringify(body);
}
return fetch(url, options);
} }
/** /**
@ -298,19 +312,10 @@ function goToListItem(offset) {
*/ */
async function handleShare() { async function handleShare() {
const link = document.querySelector(':is(a, button)[data-share-status]'); const link = document.querySelector(':is(a, button)[data-share-status]');
const title = document.querySelector(".entry-header > h1 > a");
if (link.dataset.shareStatus === "shared") { if (link.dataset.shareStatus === "shared") {
const title = document.querySelector(".entry-header > h1 > a");
await triggerWebShare(title, link.href); await triggerWebShare(title, link.href);
} }
else if (link.dataset.shareStatus === "share") {
const request = new RequestBuilder(link.href);
request.withCallback((r) => {
// Ensure title is not null before passing to triggerWebShare
triggerWebShare(title, r.url);
});
request.withHttpMethod("GET");
request.execute();
}
} }
/** /**
@ -335,7 +340,6 @@ async function triggerWebShare(title, url) {
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
window.location.reload();
} }
/** /**
@ -584,9 +588,7 @@ function handleRefreshAllFeeds() {
*/ */
function updateEntriesStatus(entryIDs, status, callback) { function updateEntriesStatus(entryIDs, status, callback) {
const url = document.body.dataset.entriesStatusUrl; const url = document.body.dataset.entriesStatusUrl;
const request = new RequestBuilder(url); sendPOSTRequest(url, { entry_ids: entryIDs, status: status }).then((resp) => {
request.withBody({ entry_ids: entryIDs, status: status });
request.withCallback((resp) => {
resp.json().then(count => { resp.json().then(count => {
if (callback) { if (callback) {
callback(resp); callback(resp);
@ -599,7 +601,6 @@ function updateEntriesStatus(entryIDs, status, callback) {
} }
}); });
}); });
request.execute();
} }
/** /**
@ -630,8 +631,7 @@ function saveEntry(element, toasting) {
element.textContent = ""; element.textContent = "";
insertIconLabelElement(element, element.dataset.labelLoading); insertIconLabelElement(element, element.dataset.labelLoading);
const request = new RequestBuilder(element.dataset.saveUrl); sendPOSTRequest(element.dataset.saveUrl).then(() => {
request.withCallback(() => {
element.textContent = ""; element.textContent = "";
insertIconLabelElement(element, element.dataset.labelDone); insertIconLabelElement(element, element.dataset.labelDone);
element.dataset.completed = "true"; element.dataset.completed = "true";
@ -640,7 +640,6 @@ function saveEntry(element, toasting) {
showToast(element.dataset.toastDone, iconElement); showToast(element.dataset.toastDone, iconElement);
} }
}); });
request.execute();
} }
/** /**
@ -670,8 +669,7 @@ function toggleBookmark(parentElement, toasting) {
buttonElement.textContent = ""; buttonElement.textContent = "";
insertIconLabelElement(buttonElement, buttonElement.dataset.labelLoading); insertIconLabelElement(buttonElement, buttonElement.dataset.labelLoading);
const request = new RequestBuilder(buttonElement.dataset.bookmarkUrl); sendPOSTRequest(buttonElement.dataset.bookmarkUrl).then(() => {
request.withCallback(() => {
const currentStarStatus = buttonElement.dataset.value; const currentStarStatus = buttonElement.dataset.value;
const newStarStatus = currentStarStatus === "star" ? "unstar" : "star"; const newStarStatus = currentStarStatus === "star" ? "unstar" : "star";
const isStarred = currentStarStatus === "star"; const isStarred = currentStarStatus === "star";
@ -688,7 +686,6 @@ function toggleBookmark(parentElement, toasting) {
insertIconLabelElement(buttonElement, label); insertIconLabelElement(buttonElement, label);
buttonElement.dataset.value = newStarStatus; buttonElement.dataset.value = newStarStatus;
}); });
request.execute();
} }
/** /**
@ -707,8 +704,7 @@ function handleFetchOriginalContent() {
buttonElement.textContent = ""; buttonElement.textContent = "";
insertIconLabelElement(buttonElement, buttonElement.dataset.labelLoading); insertIconLabelElement(buttonElement, buttonElement.dataset.labelLoading);
const request = new RequestBuilder(buttonElement.dataset.fetchContentUrl); sendPOSTRequest(buttonElement.dataset.fetchContentUrl).then((response) => {
request.withCallback((response) => {
buttonElement.textContent = ''; buttonElement.textContent = '';
buttonElement.appendChild(previousElement); buttonElement.appendChild(previousElement);
@ -722,7 +718,6 @@ function handleFetchOriginalContent() {
} }
}); });
}); });
request.execute();
} }
/** /**
@ -792,11 +787,9 @@ function openSelectedItem() {
function unsubscribeFromFeed() { function unsubscribeFromFeed() {
const unsubscribeLink = document.querySelector("[data-action=remove-feed]"); const unsubscribeLink = document.querySelector("[data-action=remove-feed]");
if (unsubscribeLink) { if (unsubscribeLink) {
const request = new RequestBuilder(unsubscribeLink.dataset.url); sendPOSTRequest(unsubscribeLink.dataset.url).then(() => {
request.withCallback(() => {
window.location.href = unsubscribeLink.dataset.redirectUrl || window.location.href; window.location.href = unsubscribeLink.dataset.redirectUrl || window.location.href;
}); });
request.execute();
} }
} }
@ -952,9 +945,7 @@ function handlePlayerProgressionSaveAndMarkAsReadOnCompletion(playerElement) {
) { ) {
playerElement.dataset.lastPosition = currentPositionInSeconds.toString(); playerElement.dataset.lastPosition = currentPositionInSeconds.toString();
const request = new RequestBuilder(playerElement.dataset.saveUrl); sendPOSTRequest(playerElement.dataset.saveUrl, { progression: currentPositionInSeconds });
request.withBody({ progression: currentPositionInSeconds });
request.execute();
// Handle the mark as read on completion // Handle the mark as read on completion
if (markAsReadOnCompletion >= 0 && playerElement.duration > 0) { if (markAsReadOnCompletion >= 0 && playerElement.duration > 0) {
@ -1199,8 +1190,7 @@ function initializeClickHandlers() {
// Generic confirmation handler // Generic confirmation handler
onClick(":is(a, button)[data-confirm]", (event) => { onClick(":is(a, button)[data-confirm]", (event) => {
handleConfirmationMessage(event.target, (url, redirectURL) => { handleConfirmationMessage(event.target, (url, redirectURL) => {
const request = new RequestBuilder(url); sendPOSTRequest(url).then((response) => {
request.withCallback((response) => {
if (redirectURL) { if (redirectURL) {
window.location.href = redirectURL; window.location.href = redirectURL;
} else if (response?.redirected && response.url) { } else if (response?.redirected && response.url) {
@ -1209,7 +1199,6 @@ function initializeClickHandlers() {
window.location.reload(); window.location.reload();
} }
}); });
request.execute();
}); });
}); });

View file

@ -1,39 +0,0 @@
class RequestBuilder {
constructor(url) {
this.callback = null;
this.url = url;
this.options = {
method: "POST",
cache: "no-cache",
credentials: "include",
body: null,
headers: new Headers({
"Content-Type": "application/json",
"X-Csrf-Token": getCsrfToken()
})
};
}
withHttpMethod(method) {
this.options.method = method;
return this;
}
withBody(body) {
this.options.body = JSON.stringify(body);
return this;
}
withCallback(callback) {
this.callback = callback;
return this;
}
execute() {
fetch(new Request(this.url, this.options)).then((response) => {
if (this.callback) {
this.callback(response);
}
});
}
}

View file

@ -52,14 +52,7 @@ class WebAuthnHandler {
url += "?username=" + username; url += "?username=" + username;
} }
return fetch(url, { return sendPOSTRequest(url, data);
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Csrf-Token": getCsrfToken()
},
body: JSON.stringify(data),
});
} }
async get(urlKey, username) { async get(urlKey, username) {

View file

@ -116,7 +116,6 @@ func GenerateJavascriptBundles() error {
"js/tt.js", // has to be first "js/tt.js", // has to be first
"js/touch_handler.js", "js/touch_handler.js",
"js/keyboard_handler.js", "js/keyboard_handler.js",
"js/request_builder.js",
"js/modal_handler.js", "js/modal_handler.js",
"js/webauthn_handler.js", "js/webauthn_handler.js",
"js/app.js", "js/app.js",