Skip to content

Commit

Permalink
Update Fetch responses and dev tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrakern committed Dec 11, 2024
1 parent a904185 commit 496843b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 34 deletions.
8 changes: 4 additions & 4 deletions js/complete.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dev-tools.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/utility.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/js/dev-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
* @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})
*/

function fcn_printAjaxResponse(payload, method = 'get') {
function fcn_printAjaxResponse(payload, { method = 'get', url = null } = {}) {
if (method === 'get') {
FcnUtils.aGet(payload).then((response) => { console.log(response); });
FcnUtils.aGet(payload, url).then((response) => { console.log(response); });
} else {
FcnUtils.aPost(payload).then((response) => { console.log(response); });
FcnUtils.aPost(payload, url).then((response) => { console.log(response); });
}
}
54 changes: 30 additions & 24 deletions src/js/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,15 +819,15 @@ async function fcn_ajaxPost(data = {}, url = null, headers = {}) {
// Get URL if not provided
url = url ? url : (fictioneer_ajax.ajax_url ?? FcnGlobals.ajaxURL);

// Default headers
let final_headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache'
// Merge default headers with custom headers (if any)
const final_headers = {
...{
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache'
},
...headers
};

// Merge in custom headers (if any)
final_headers = {...final_headers, ...headers};

// Merge with default nonce
data = {...{'nonce': FcnUtils.nonce()}, ...data};

Expand All @@ -840,11 +840,14 @@ async function fcn_ajaxPost(data = {}, url = null, headers = {}) {
body: new URLSearchParams(data)
});

// Return response
if (response.ok) {
return response.json();
} else {
return Promise.reject(response);
// Handle response by status code
switch (response.status) {
case 200: // OK
return response.json();
case 204: // No Content
return null;
default:
return Promise.reject(response);
}
}

Expand All @@ -869,15 +872,15 @@ async function fcn_ajaxGet(data = {}, url = null, headers = {}) {
data = {...{'nonce': FcnUtils.nonce()}, ...data};
url = FcnUtils.buildUrl(data, url);

// Default headers
let final_headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache'
// Merge default headers with custom headers (if any)
const final_headers = {
...{
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache'
},
...headers
};

// Merge in custom headers (if any)
final_headers = {...final_headers, ...headers};

// Fetch promise
const response = await fetch(url, {
method: 'GET',
Expand All @@ -886,10 +889,13 @@ async function fcn_ajaxGet(data = {}, url = null, headers = {}) {
mode: 'same-origin'
});

// Return response
if (response.ok) {
return response.json();
} else {
return Promise.reject(response);
// Handle response by status code
switch (response.status) {
case 200: // OK
return response.json();
case 204: // No Content
return null;
default:
return Promise.reject(response);
}
}

0 comments on commit 496843b

Please sign in to comment.