History

Use fetch helpers instead of fetch (#27026)

WIP because: - [x] Some calls set a `content-type` but send no body, can likely remove the header - [x] Need to check whether `charset=utf-8` has any significance on the webauthn calls, I assume not as it is the default for json content. - [x] Maybe `no-restricted-globals` is better for eslint, but will require a lot of duplication in the yaml or moving eslint config to a `.js` extension. - [x] Maybe export `request` as `fetch`, shadowing the global.

silverwind committed 3 years ago

Commit ae8e8f01 parent 809923817 files · +70 −98Browse files

Showing the changes to web_src/js/modules (1 of 17 files). Show all

Unified · Split

web_src/js/modules/fetch.js+17−10
@@ -2,17 +2,18 @@ import {isObject} from '../utils.js'; · 1 unmodified line
22
33 const {csrfToken} = window.config;
44
5+// safe HTTP methods that don't need a csrf token
6+const safeMethods = new Set(['GET', 'HEAD', 'OPTIONS', 'TRACE']);
7+
58 // fetch wrapper, use below method name functions and the `data` option to pass in data
6−// which will automatically set an appropriate content-type header. For json content,
7−// only object and array types are currently supported.
8−function request(url, {headers, data, body, ...other} = {}) {
9+// which will automatically set an appropriate headers. For json content, only object
10+// and array types are currently supported.
11+export function request(url, {method = 'GET', headers = {}, data, body, ...other} = {}) {
912 let contentType;
1013 if (!body) {
1114 if (data instanceof FormData) {
12− contentType = 'multipart/form-data';
1315 body = data;
1416 } else if (data instanceof URLSearchParams) {
15− contentType = 'application/x-www-form-urlencoded';
1617 body = data;
1718 } else if (isObject(data) || Array.isArray(data)) {
1819 contentType = 'application/json';
@@ -20,12 +21,18 @@ function request(url, {headers, data, body, ...other} = {}) { · 1 unmodified line
2021 }
2122 }
2223
24+ const headersMerged = new Headers({
25+ ...(!safeMethods.has(method.toUpperCase()) && {'x-csrf-token': csrfToken}),
26+ ...(contentType && {'content-type': contentType}),
27+ });
28+
29+ for (const [name, value] of Object.entries(headers)) {
30+ headersMerged.set(name, value);
31+ }
32+
2333 return fetch(url, {
24− headers: {
25− 'x-csrf-token': csrfToken,
26− ...(contentType && {'content-type': contentType}),
27− ...headers,
28− },
34+ method,
35+ headers: headersMerged,
2936 ...(body && {body}),
3037 ...other,
3138 });