-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add fetch wrappers, ignore network errors in actions view #26985
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9050830
Add fetch wrappers, ignore network errors in actions view
silverwind 8f8b7b0
check for undefined to allow 0 and null as json
silverwind 11eb0d3
use single data argument
silverwind c02f5bd
allow only object/array for json
silverwind be46bd0
typo
silverwind 9f91c9e
rewrite the fetching and parallelize it
silverwind cbec6f8
remove network error code
silverwind 8686836
add docs
silverwind 3806e13
tweak
silverwind ae24962
tweak
silverwind 8c972b2
tweak
silverwind 053b7b8
tweak
silverwind 1c9d82c
add promise info
silverwind d697c50
swap order
silverwind 4d77102
move comment
silverwind 05b97c8
move comment
silverwind 48d30c9
Merge branch 'main' into fetch
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const {csrfToken} = window.config; | ||
|
||
function request(url, {headers, json, ...other} = {}) { | ||
return window.fetch(url, { | ||
headers: { | ||
'x-csrf-token': csrfToken, | ||
...(json !== undefined && {'content-type': 'application/json'}), | ||
wxiaoguang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
...headers, | ||
}, | ||
...(json !== undefined && {body: JSON.stringify(json)}), | ||
...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}); | ||
|
||
// network errors are currently only detectable by error message | ||
// based on https://github.com/sindresorhus/p-retry/blob/main/index.js | ||
const networkErrorMsgs = new Set([ | ||
'Failed to fetch', // Chrome | ||
'NetworkError when attempting to fetch resource.', // Firefox | ||
'The Internet connection appears to be offline.', // Safari | ||
]); | ||
wxiaoguang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export function isNetworkError(msg) { | ||
return networkErrorMsgs.has(msg); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {test, expect} from 'vitest'; | ||
import {GET, POST, PATCH, PUT, DELETE, isNetworkError} from './fetch.js'; | ||
|
||
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
|
||
|
||
test('isNetworkError', () => { | ||
expect(isNetworkError('Failed to fetch')).toEqual(true); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Are headers case-insensitive?
I've only seen them be called
X-Csrf-Token
yet…Uh oh!
There was an error while loading. Please reload this page.
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.
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