Skip to content

Commit c40ee6f

Browse files
authored
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
1 parent f2d5c6e commit c40ee6f

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

Diff for: web_src/js/modules/fetch.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ const safeMethods = new Set(['GET', 'HEAD', 'OPTIONS', 'TRACE']);
88
// fetch wrapper, use below method name functions and the `data` option to pass in data
99
// which will automatically set an appropriate headers. For json content, only object
1010
// and array types are currently supported.
11-
export function request(url, {method = 'GET', headers = {}, data, body, ...other} = {}) {
12-
let contentType;
13-
if (!body) {
14-
if (data instanceof FormData || data instanceof URLSearchParams) {
15-
body = data;
16-
} else if (isObject(data) || Array.isArray(data)) {
17-
contentType = 'application/json';
18-
body = JSON.stringify(data);
19-
}
11+
export function request(url, {method = 'GET', data, headers = {}, ...other} = {}) {
12+
let body, contentType;
13+
if (data instanceof FormData || data instanceof URLSearchParams) {
14+
body = data;
15+
} else if (isObject(data) || Array.isArray(data)) {
16+
contentType = 'application/json';
17+
body = JSON.stringify(data);
2018
}
2119

2220
const headersMerged = new Headers({
23-
...(!safeMethods.has(method.toUpperCase()) && {'x-csrf-token': csrfToken}),
21+
...(!safeMethods.has(method) && {'x-csrf-token': csrfToken}),
2422
...(contentType && {'content-type': contentType}),
2523
});
2624

@@ -31,8 +29,8 @@ export function request(url, {method = 'GET', headers = {}, data, body, ...other
3129
return fetch(url, {
3230
method,
3331
headers: headersMerged,
34-
...(body && {body}),
3532
...other,
33+
...(body && {body}),
3634
});
3735
}
3836

0 commit comments

Comments
 (0)