diff --git a/docs/uploader/xhr.mdx b/docs/uploader/xhr.mdx index d65c7e3e44..c62c2aed45 100644 --- a/docs/uploader/xhr.mdx +++ b/docs/uploader/xhr.mdx @@ -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`). +(`(xhr: XMLHttpRequest, retryCount: number, files: UppyFile[]) => void | Promise`). + +The third argument, `files`, is an array of all Uppy files when `bundle` is +`true`. When `false`, it only contains one file. #### `shouldRetry` diff --git a/packages/@uppy/xhr-upload/src/index.ts b/packages/@uppy/xhr-upload/src/index.ts index 8d91c41274..14ea0eccf8 100644 --- a/packages/@uppy/xhr-upload/src/index.ts +++ b/packages/@uppy/xhr-upload/src/index.ts @@ -52,7 +52,12 @@ export interface XhrUploadOpts 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[], + ) => void | Promise shouldRetry?: FetcherOptions['shouldRetry'] onAfterResponse?: FetcherOptions['onAfterResponse'] getResponseData?: (xhr: XMLHttpRequest) => B | Promise @@ -194,13 +199,16 @@ export default class XHRUpload< */ this.#getFetcher = (files: UppyFile[]) => { return async ( - url: Parameters[0], - options: NonNullable[1]>, + url: string, + options: Omit & { + onBeforeRequest?: Opts['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) => {