Refactor request function (#29187)

- Remove and prevent use of `body` argument, it is not used anywhere
- Remove uppercasing of method, we can require it to be uppercase
This commit is contained in:
silverwind 2024-02-16 14:27:00 +01:00 committed by GitHub
parent f2d5c6edde
commit c40ee6fb73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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) {
export function request(url, {method = 'GET', data, headers = {}, ...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}),
});
}