Skip to content

Commit

Permalink
@uppy/xhr-upload: pass files to onBeforeRequest (#5447)
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon authored Sep 5, 2024
1 parent 3f2f37f commit f331622
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 4 additions & 1 deletion docs/uploader/xhr.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ credentials (`boolean`, default: `false`).
#### `onBeforeRequest`

An optional function that will be called before a HTTP request is sent out
(`(xhr: XMLHttpRequest, retryCount: number) => void | Promise<void>`).
(`(xhr: XMLHttpRequest, retryCount: number, files: UppyFile<M, B>[]) => void | Promise<void>`).

The third argument, `files`, is an array of all Uppy files when `bundle` is
`true`. When `false`, it only contains one file.

#### `shouldRetry`

Expand Down
16 changes: 12 additions & 4 deletions packages/@uppy/xhr-upload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ export interface XhrUploadOpts<M extends Meta, B extends Body>
limit?: number
responseType?: XMLHttpRequestResponseType
withCredentials?: boolean
onBeforeRequest?: FetcherOptions['onBeforeRequest']
onBeforeRequest?: (
xhr: XMLHttpRequest,
retryCount: number,
/** The files to be uploaded. When `bundle` is `false` only one file is in the array. */
files: UppyFile<M, B>[],
) => void | Promise<void>
shouldRetry?: FetcherOptions['shouldRetry']
onAfterResponse?: FetcherOptions['onAfterResponse']
getResponseData?: (xhr: XMLHttpRequest) => B | Promise<B>
Expand Down Expand Up @@ -194,13 +199,16 @@ export default class XHRUpload<
*/
this.#getFetcher = (files: UppyFile<M, B>[]) => {
return async (
url: Parameters<typeof fetcher>[0],
options: NonNullable<Parameters<typeof fetcher>[1]>,
url: string,
options: Omit<FetcherOptions, 'onBeforeRequest'> & {
onBeforeRequest?: Opts<M, B>['onBeforeRequest']
},
) => {
try {
const res = await fetcher(url, {
...options,
onBeforeRequest: this.opts.onBeforeRequest,
onBeforeRequest: (xhr, retryCount) =>
this.opts.onBeforeRequest?.(xhr, retryCount, files),
shouldRetry: this.opts.shouldRetry,
onAfterResponse: this.opts.onAfterResponse,
onTimeout: (timeout) => {
Expand Down

0 comments on commit f331622

Please sign in to comment.