From ba5d37559c525aacaa703eaacaabc9e52af7f7ca Mon Sep 17 00:00:00 2001 From: Zoran Pesic Date: Sat, 25 Dec 2021 00:02:44 -0800 Subject: [PATCH 1/3] allow a beforeSend callback to be set in config This change allows the user to specify a callback which will fire before a request is triggered. The function will have access to the XHR object and settings object. --- src/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 4421da36..e6cb34a9 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ let config = { cookieDomain: null, headers: {}, visitParams: {}, + beforeSend: null, withCredentials: false, visitDuration: 4 * 60, // default 4 hours visitorDuration: 2 * 365 * 24 * 60 // default 2 years @@ -170,6 +171,11 @@ function CSRFProtection(xhr) { if (token) xhr.setRequestHeader("X-CSRF-Token", token); } +function beforeSend(xhr, settings) { + CSRFProtection(xhr); + if (config.beforeSend) config.beforeSend(xhr, settings); +} + function sendRequest(url, data, success) { if (canStringify) { if ($ && $.ajax) { @@ -179,7 +185,7 @@ function sendRequest(url, data, success) { data: JSON.stringify(data), contentType: "application/json; charset=utf-8", dataType: "json", - beforeSend: CSRFProtection, + beforeSend: beforeSend, success: success, headers: config.headers, xhrFields: { @@ -201,7 +207,7 @@ function sendRequest(url, data, success) { success(); } }; - CSRFProtection(xhr); + beforeSend(xhr, { data: data }); xhr.send(JSON.stringify(data)); } } From 874465a97705dc0093f56030cb6490bc0e8db694 Mon Sep 17 00:00:00 2001 From: Zoran Pesic Date: Sat, 25 Dec 2021 00:11:00 -0800 Subject: [PATCH 2/3] make callback data object consistent between ajax and xhr requests This ensures the request data being passed to the beforeSend callback is in the same format for both the ajax and XMLHttpRequest versions. --- src/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index e6cb34a9..c9e10397 100644 --- a/src/index.js +++ b/src/index.js @@ -177,12 +177,13 @@ function beforeSend(xhr, settings) { } function sendRequest(url, data, success) { + let requestData = JSON.stringify(data); if (canStringify) { if ($ && $.ajax) { $.ajax({ type: "POST", url: url, - data: JSON.stringify(data), + data: requestData, contentType: "application/json; charset=utf-8", dataType: "json", beforeSend: beforeSend, @@ -207,8 +208,8 @@ function sendRequest(url, data, success) { success(); } }; - beforeSend(xhr, { data: data }); - xhr.send(JSON.stringify(data)); + beforeSend(xhr, { data: requestData }); + xhr.send(requestData); } } } From 32943857d38e2ba979f1f5cbb4b518caf808298b Mon Sep 17 00:00:00 2001 From: Zoran Pesic Date: Sat, 25 Dec 2021 14:14:10 -0800 Subject: [PATCH 3/3] update readme to reflect new config option --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fe57af8a..7fada804 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,7 @@ ahoy.configure({ cookieDomain: null, headers: {}, visitParams: {}, + beforeSend: null, withCredentials: false, visitDuration: 4 * 60, // 4 hours visitorDuration: 2 * 365 * 24 * 60 // 2 years