-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fetch wrappers, ignore network errors in actions view
- Loading branch information
1 parent
049b9f3
commit 9050830
Showing
3 changed files
with
57 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 && {'content-type': 'application/json'}), | ||
...headers, | ||
}, | ||
...(json && {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 | ||
]); | ||
|
||
export function isNetworkError(msg) { | ||
return networkErrorMsgs.has(msg); | ||
} |
This file contains 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(); | ||
}); | ||
|
||
test('isNetworkError', () => { | ||
expect(isNetworkError('Failed to fetch')).toEqual(true); | ||
}); |