-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add fetch wrappers, ignore network errors in actions view #26985
Changes from 16 commits
9050830
8f8b7b0
11eb0d3
c02f5bd
be46bd0
9f91c9e
cbec6f8
8686836
3806e13
ae24962
8c972b2
053b7b8
1c9d82c
d697c50
4d77102
05b97c8
48d30c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {isObject} from '../utils.js'; | ||
|
||
const {csrfToken} = window.config; | ||
|
||
// fetch wrapper, use below method name functions and the `data` option to pass in data | ||
// which will automatically set an appropriate content-type header. For json content, | ||
// only object and array types are currently supported. | ||
function request(url, {headers, data, body, ...other} = {}) { | ||
let contentType; | ||
if (!body) { | ||
if (data instanceof FormData) { | ||
contentType = 'multipart/form-data'; | ||
body = data; | ||
} else if (data instanceof URLSearchParams) { | ||
contentType = 'application/x-www-form-urlencoded'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
While I do not see why it should support (not blocker) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the backend actually support both encodings interchangbly? Normal form behaviour is |
||
body = data; | ||
} else if (isObject(data) || Array.isArray(data)) { | ||
contentType = 'application/json'; | ||
body = JSON.stringify(data); | ||
} | ||
} | ||
|
||
return fetch(url, { | ||
headers: { | ||
'x-csrf-token': csrfToken, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are headers case-insensitive? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, HTTP headers by definition are to be treated case-insensitive while reading them and in fact we should lowercase them all everywhere. In HTTP2 the common headers are transferred via a mapping table that outputs lowercase: https://datatracker.ietf.org/doc/html/rfc7541#appendix-A |
||
...(contentType && {'content-type': contentType}), | ||
...headers, | ||
}, | ||
...(body && {body}), | ||
...other, | ||
}); | ||
} | ||
|
||
export const GET = (url, opts) => request(url, {method: 'GET', ...opts}); | ||
export const POST = (url, opts) => request(url, {method: 'POST', ...opts}); | ||
export const PATCH = (url, opts) => request(url, {method: 'PATCH', ...opts}); | ||
export const PUT = (url, opts) => request(url, {method: 'PUT', ...opts}); | ||
export const DELETE = (url, opts) => request(url, {method: 'DELETE', ...opts}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {test, expect} from 'vitest'; | ||
import {GET, POST, PATCH, PUT, DELETE} from './fetch.js'; | ||
|
||
// tests here are only to satisfy the linter for unused functions | ||
test('exports', () => { | ||
expect(GET).toBeTruthy(); | ||
expect(POST).toBeTruthy(); | ||
expect(PATCH).toBeTruthy(); | ||
expect(PUT).toBeTruthy(); | ||
expect(DELETE).toBeTruthy(); | ||
}); | ||
silverwind marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CSRF
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will fix this in followup #27026.