Преглед изворни кода

@uppy/xhr-upload: pass files to onBeforeRequest (#5447)

Merlijn Vos пре 7 месеци
родитељ
комит
f33162236d
2 измењених фајлова са 16 додато и 5 уклоњено
  1. 4 1
      docs/uploader/xhr.mdx
  2. 12 4
      packages/@uppy/xhr-upload/src/index.ts

+ 4 - 1
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<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`
 

+ 12 - 4
packages/@uppy/xhr-upload/src/index.ts

@@ -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>
@@ -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) => {