From b858aa5bf345886823221447aefedb0f075f8691 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 16 Feb 2024 00:48:55 +0100 Subject: [PATCH 1/2] Refactor request function - Remove and prevent use of `body` argument, it is not used anywhere - Remove uppercasing of method, we can require it to be uppercase --- web_src/js/modules/fetch.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/web_src/js/modules/fetch.js b/web_src/js/modules/fetch.js index b3529d27fcfa1..405e25e4de755 100644 --- a/web_src/js/modules/fetch.js +++ b/web_src/js/modules/fetch.js @@ -8,19 +8,17 @@ const safeMethods = new Set(['GET', 'HEAD', 'OPTIONS', 'TRACE']); // fetch wrapper, use below method name functions and the `data` option to pass in data // which will automatically set an appropriate headers. For json content, only object // and array types are currently supported. -export function request(url, {method = 'GET', headers = {}, data, body, ...other} = {}) { - let contentType; - if (!body) { - if (data instanceof FormData || data instanceof URLSearchParams) { - body = data; - } else if (isObject(data) || Array.isArray(data)) { - contentType = 'application/json'; - body = JSON.stringify(data); - } +export function request(url, {method = 'GET', headers = {}, data, ...other} = {}) { + let body, contentType; + if (data instanceof FormData || data instanceof URLSearchParams) { + body = data; + } else if (isObject(data) || Array.isArray(data)) { + contentType = 'application/json'; + body = JSON.stringify(data); } const headersMerged = new Headers({ - ...(!safeMethods.has(method.toUpperCase()) && {'x-csrf-token': csrfToken}), + ...(!safeMethods.has(method) && {'x-csrf-token': csrfToken}), ...(contentType && {'content-type': contentType}), }); @@ -31,8 +29,8 @@ export function request(url, {method = 'GET', headers = {}, data, body, ...other return fetch(url, { method, headers: headersMerged, - ...(body && {body}), ...other, + ...(body && {body}), }); } From e2dc98a27a72af0836bfd0ef3149f3e3a7dd79d7 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 16 Feb 2024 00:57:05 +0100 Subject: [PATCH 2/2] move arg --- web_src/js/modules/fetch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/modules/fetch.js b/web_src/js/modules/fetch.js index 405e25e4de755..2191a8d4dbfee 100644 --- a/web_src/js/modules/fetch.js +++ b/web_src/js/modules/fetch.js @@ -8,7 +8,7 @@ const safeMethods = new Set(['GET', 'HEAD', 'OPTIONS', 'TRACE']); // fetch wrapper, use below method name functions and the `data` option to pass in data // which will automatically set an appropriate headers. For json content, only object // and array types are currently supported. -export function request(url, {method = 'GET', headers = {}, data, ...other} = {}) { +export function request(url, {method = 'GET', data, headers = {}, ...other} = {}) { let body, contentType; if (data instanceof FormData || data instanceof URLSearchParams) { body = data;