From f33162236d7169eb13c0440416a9502e03e74055 Mon Sep 17 00:00:00 2001 From: Merlijn Vos Date: Thu, 5 Sep 2024 12:44:12 +0200 Subject: [PATCH] @uppy/xhr-upload: pass files to onBeforeRequest (#5447) --- docs/uploader/xhr.mdx | 5 ++++- packages/@uppy/xhr-upload/src/index.ts | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) 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) => {