Update Fetch responses and dev tools

This commit is contained in:
Tetrakern 2024-12-11 16:44:48 +01:00
parent a9041850cd
commit 496843bec4
5 changed files with 42 additions and 34 deletions

8
js/complete.min.js vendored

File diff suppressed because one or more lines are too long

2
js/dev-tools.min.js vendored
View File

@ -1 +1 @@
async function fcn_benchmarkAjax(e=1,t={},n=null,o={},r="get"){let s=0;console.log(`Starting benchmark with ${e} AJAX requests...`);try{"get"===r?await FcnUtils.aGet(t,n,o):await FcnUtils.aPost(t,n,o)}catch(e){console.error("Error during warm-up request:",e)}for(let c=0;c<e;c++){const e=performance.now();try{"get"===r?await FcnUtils.aGet(t,n,o):await FcnUtils.aPost(t,n,o),s+=performance.now()-e}catch(e){console.error("Error during AJAX request:",e)}}const c=s/e;return console.log(`Finished benchmarking. Average AJAX response time over ${e} requests: ${c.toFixed(2)} ms`),c}function fcn_printAjaxResponse(e,t="get"){"get"===t?FcnUtils.aGet(e).then((e=>{console.log(e)})):FcnUtils.aPost(e).then((e=>{console.log(e)}))} async function fcn_benchmarkAjax(e=1,t={},n=null,o={},r="get"){let s=0;console.log(`Starting benchmark with ${e} AJAX requests...`);try{"get"===r?await FcnUtils.aGet(t,n,o):await FcnUtils.aPost(t,n,o)}catch(e){console.error("Error during warm-up request:",e)}for(let c=0;c<e;c++){const e=performance.now();try{"get"===r?await FcnUtils.aGet(t,n,o):await FcnUtils.aPost(t,n,o),s+=performance.now()-e}catch(e){console.error("Error during AJAX request:",e)}}const c=s/e;return console.log(`Finished benchmarking. Average AJAX response time over ${e} requests: ${c.toFixed(2)} ms`),c}function fcn_printAjaxResponse(e,{method:t="get",url:n=null}={}){"get"===t?FcnUtils.aGet(e,n).then((e=>{console.log(e)})):FcnUtils.aPost(e,n).then((e=>{console.log(e)}))}

2
js/utility.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -59,15 +59,17 @@ async function fcn_benchmarkAjax(n = 1, data = {}, url = null, headers = {}, met
* Makes a GET request and prints the response to the console * Makes a GET request and prints the response to the console
* *
* @param {Object} payload - Payload to be sent with the request. * @param {Object} payload - Payload to be sent with the request.
* @param {String} method - Either 'get' or 'post'. Default 'get'. * @param {Object} [options] - Optional. Additional options for the request.
* @param {String} [options.method=get] - Optional. The request method. Default 'get'.
* @param {String|null} [options.url=null] - Optional. The request URL. Default null.
* *
* @example fcn_ajaxPrintResponse({'action': 'the_function', 'fcn_fast_ajax': 1}) * @example fcn_ajaxPrintResponse({'action': 'the_function', 'fcn_fast_ajax': 1})
*/ */
function fcn_printAjaxResponse(payload, method = 'get') { function fcn_printAjaxResponse(payload, { method = 'get', url = null } = {}) {
if (method === 'get') { if (method === 'get') {
FcnUtils.aGet(payload).then((response) => { console.log(response); }); FcnUtils.aGet(payload, url).then((response) => { console.log(response); });
} else { } else {
FcnUtils.aPost(payload).then((response) => { console.log(response); }); FcnUtils.aPost(payload, url).then((response) => { console.log(response); });
} }
} }

View File

@ -819,15 +819,15 @@ async function fcn_ajaxPost(data = {}, url = null, headers = {}) {
// Get URL if not provided // Get URL if not provided
url = url ? url : (fictioneer_ajax.ajax_url ?? FcnGlobals.ajaxURL); url = url ? url : (fictioneer_ajax.ajax_url ?? FcnGlobals.ajaxURL);
// Default headers // Merge default headers with custom headers (if any)
let final_headers = { const final_headers = {
...{
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache' 'Cache-Control': 'no-cache'
},
...headers
}; };
// Merge in custom headers (if any)
final_headers = {...final_headers, ...headers};
// Merge with default nonce // Merge with default nonce
data = {...{'nonce': FcnUtils.nonce()}, ...data}; data = {...{'nonce': FcnUtils.nonce()}, ...data};
@ -840,10 +840,13 @@ async function fcn_ajaxPost(data = {}, url = null, headers = {}) {
body: new URLSearchParams(data) body: new URLSearchParams(data)
}); });
// Return response // Handle response by status code
if (response.ok) { switch (response.status) {
case 200: // OK
return response.json(); return response.json();
} else { case 204: // No Content
return null;
default:
return Promise.reject(response); return Promise.reject(response);
} }
} }
@ -869,15 +872,15 @@ async function fcn_ajaxGet(data = {}, url = null, headers = {}) {
data = {...{'nonce': FcnUtils.nonce()}, ...data}; data = {...{'nonce': FcnUtils.nonce()}, ...data};
url = FcnUtils.buildUrl(data, url); url = FcnUtils.buildUrl(data, url);
// Default headers // Merge default headers with custom headers (if any)
let final_headers = { const final_headers = {
...{
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache' 'Cache-Control': 'no-cache'
},
...headers
}; };
// Merge in custom headers (if any)
final_headers = {...final_headers, ...headers};
// Fetch promise // Fetch promise
const response = await fetch(url, { const response = await fetch(url, {
method: 'GET', method: 'GET',
@ -886,10 +889,13 @@ async function fcn_ajaxGet(data = {}, url = null, headers = {}) {
mode: 'same-origin' mode: 'same-origin'
}); });
// Return response // Handle response by status code
if (response.ok) { switch (response.status) {
case 200: // OK
return response.json(); return response.json();
} else { case 204: // No Content
return null;
default:
return Promise.reject(response); return Promise.reject(response);
} }
} }