-
Notifications
You must be signed in to change notification settings - Fork 2k
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
@uppy/utils: add fetcher #5073
Merged
Merged
@uppy/utils: add fetcher #5073
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a445a43
@uppy/utils: add fetcher
Murderlon 33582de
Improve callbacks
Murderlon ef3bf22
Add to export map
Murderlon a9ab8ee
Make body type more correct
Murderlon 62c1c8c
Apply suggestions from code review
Murderlon e996a3f
Prettier
Murderlon c8cc624
Merge branch 'main' into fetcher-util
Murderlon 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 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,145 @@ | ||
import NetworkError from './NetworkError.ts' | ||
import ProgressTimeout from './ProgressTimeout.ts' | ||
|
||
const noop = (): void => {} | ||
|
||
export type FetcherOptions = { | ||
/** The HTTP method to use for the request. Default is 'GET'. */ | ||
method?: string | ||
|
||
/** The request payload, if any. Default is null. */ | ||
body?: Document | XMLHttpRequestBodyInit | null | ||
|
||
/** Milliseconds between XMLHttpRequest upload progress events before the request is aborted. Default is 30000 ms. */ | ||
timeout?: number | ||
|
||
/** Sets the withCredentials property of the XMLHttpRequest object. Default is false. */ | ||
withCredentials?: boolean | ||
|
||
/** Sets the responseType property of the XMLHttpRequest object. Default is an empty string. */ | ||
responseType?: XMLHttpRequestResponseType | ||
|
||
/** An object representing any headers to send with the request. */ | ||
headers?: Record<string, string> | ||
|
||
/** The number of retry attempts to make if the request fails. Default is 3. */ | ||
retries?: number | ||
|
||
/** Called before the request is made. */ | ||
onBeforeRequest?: ( | ||
xhr: XMLHttpRequest, | ||
retryCount: number, | ||
) => void | Promise<void> | ||
|
||
/** Function for tracking upload progress. */ | ||
onUploadProgress?: (event: ProgressEvent) => void | ||
|
||
/** A function to determine whether to retry the request. */ | ||
shouldRetry?: (xhr: XMLHttpRequest) => boolean | ||
|
||
/** Called after the response has succeeded or failed but before the promise is resolved. */ | ||
onAfterRequest?: ( | ||
xhr: XMLHttpRequest, | ||
retryCount: number, | ||
) => void | Promise<void> | ||
|
||
/** Called when no XMLHttpRequest upload progress events have been received for `timeout` ms. */ | ||
onTimeout?: () => void | ||
|
||
/** Signal to abort the upload. */ | ||
signal?: AbortSignal | ||
} | ||
|
||
/** | ||
* Fetches data from a specified URL using XMLHttpRequest, with optional retry functionality and progress tracking. | ||
* | ||
* @param url The URL to send the request to. | ||
* @param options Optional settings for the fetch operation. | ||
*/ | ||
export function fetcher( | ||
url: string, | ||
options: FetcherOptions = {}, | ||
): Promise<XMLHttpRequest> { | ||
const { | ||
body = null, | ||
headers = {}, | ||
method = 'GET', | ||
onBeforeRequest = noop, | ||
onUploadProgress = noop, | ||
shouldRetry = () => true, | ||
onAfterRequest = noop, | ||
onTimeout = noop, | ||
responseType, | ||
retries = 3, | ||
signal = null, | ||
timeout = 30_000, | ||
withCredentials = false, | ||
} = options | ||
|
||
// 300 ms, 600 ms, 1200 ms, 2400 ms, 4800 ms | ||
const delay = (attempt: number): number => 0.3 * 2 ** (attempt - 1) * 1000 | ||
const timer = new ProgressTimeout(timeout, onTimeout) | ||
|
||
function requestWithRetry(retryCount = 0): Promise<XMLHttpRequest> { | ||
// eslint-disable-next-line no-async-promise-executor | ||
return new Promise(async (resolve, reject) => { | ||
const xhr = new XMLHttpRequest() | ||
|
||
xhr.open(method, url, true) | ||
xhr.withCredentials = withCredentials | ||
if (responseType) { | ||
xhr.responseType = responseType | ||
} | ||
|
||
signal?.addEventListener('abort', () => { | ||
xhr.abort() | ||
// Using DOMException for abort errors aligns with | ||
// the convention established by the Fetch API. | ||
reject(new DOMException('Aborted', 'AbortError')) | ||
}) | ||
|
||
xhr.onload = async () => { | ||
await onAfterRequest(xhr, retryCount) | ||
|
||
if (xhr.status >= 200 && xhr.status < 300) { | ||
timer.done() | ||
resolve(xhr) | ||
} else if (shouldRetry(xhr) && retryCount < retries) { | ||
setTimeout(() => { | ||
requestWithRetry(retryCount + 1).then(resolve, reject) | ||
}, delay(retryCount)) | ||
} else { | ||
timer.done() | ||
reject(new NetworkError(xhr.statusText, xhr)) | ||
} | ||
} | ||
|
||
xhr.onerror = () => { | ||
if (shouldRetry(xhr) && retryCount < retries) { | ||
setTimeout(() => { | ||
requestWithRetry(retryCount + 1).then(resolve, reject) | ||
}, delay(retryCount)) | ||
} else { | ||
timer.done() | ||
reject(new NetworkError(xhr.statusText, xhr)) | ||
} | ||
} | ||
|
||
xhr.upload.onprogress = (event: ProgressEvent) => { | ||
timer.progress() | ||
onUploadProgress(event) | ||
} | ||
|
||
if (headers) { | ||
Object.keys(headers).forEach((key) => { | ||
xhr.setRequestHeader(key, headers[key]) | ||
}) | ||
} | ||
|
||
await onBeforeRequest(xhr, retryCount) | ||
xhr.send(body) | ||
}) | ||
} | ||
|
||
return requestWithRetry() | ||
} |
Oops, something went wrong.
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.
We should consider keeping this list in ASCII order