From 5428f10cc7605663ebf62d5902c909810ca54276 Mon Sep 17 00:00:00 2001 From: Murderlon Date: Tue, 16 Aug 2022 12:01:52 +0200 Subject: [PATCH] Add file argument to `onBeforeRequest` --- packages/@uppy/tus/src/index.js | 2 +- packages/@uppy/tus/types/index.d.ts | 6 ++++-- website/src/docs/tus.md | 6 +++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/@uppy/tus/src/index.js b/packages/@uppy/tus/src/index.js index 57f9c3bdfc..8428bae93d 100644 --- a/packages/@uppy/tus/src/index.js +++ b/packages/@uppy/tus/src/index.js @@ -217,7 +217,7 @@ export default class Tus extends BasePlugin { let userProvidedPromise if (typeof opts.onBeforeRequest === 'function') { - userProvidedPromise = opts.onBeforeRequest(req) + userProvidedPromise = opts.onBeforeRequest(req, file) } if (hasProperty(queuedRequest, 'shouldBeRequeued')) { diff --git a/packages/@uppy/tus/types/index.d.ts b/packages/@uppy/tus/types/index.d.ts index 19c954b9a8..9654561005 100644 --- a/packages/@uppy/tus/types/index.d.ts +++ b/packages/@uppy/tus/types/index.d.ts @@ -1,9 +1,10 @@ -import type { PluginOptions, BasePlugin } from '@uppy/core' -import type { UploadOptions } from 'tus-js-client' +import type { PluginOptions, BasePlugin, UppyFile } from '@uppy/core' +import type { UploadOptions, HttpRequest } from 'tus-js-client' type TusUploadOptions = Pick boolean + onBeforeRequest?: (req: HttpRequest, file: UppyFile) => Promise } declare class Tus extends BasePlugin {} diff --git a/website/src/docs/tus.md b/website/src/docs/tus.md index 7fb4a4eca4..9ef4432f22 100644 --- a/website/src/docs/tus.md +++ b/website/src/docs/tus.md @@ -37,7 +37,7 @@ const { Tus } = Uppy ## Options -**Note**: all options are passed to `tus-js-client` and we document the ones here that we added or changed. This means you can also pass functions like [`onBeforeRequest`](https://github.com/tus/tus-js-client/blob/master/docs/api.md#onbeforerequest) and [`onAfterResponse`](https://github.com/tus/tus-js-client/blob/master/docs/api.md#onafterresponse). +**Note**: all options are passed to `tus-js-client` and we document the ones here that we added or changed. This means you can also pass functions like [`onAfterResponse`](https://github.com/tus/tus-js-client/blob/master/docs/api.md#onafterresponse). We recommended taking a look at the [API reference](https://github.com/tus/tus-js-client/blob/master/docs/api.md) from `tus-js-client` to know what is supported. @@ -91,6 +91,10 @@ When uploading a chunk fails, automatically try again after the millisecond inte Set to `null` to disable automatic retries, and fail instantly if any chunk fails to upload. +### `onBeforeRequest(req, file)` + +Behaves like the [`onBeforeRequest`](https://github.com/tus/tus-js-client/blob/master/docs/api.md#onbeforerequest) function from `tus-js-client` but with the added `file` argument. + ### `onShouldRetry: (err, retryAttempt, options, next) => next(err)` When an upload fails `onShouldRetry` is called with the error and the default retry logic as the second argument. The default retry logic is an [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff) algorithm triggered on HTTP 429 (Too Many Requests) errors. Meaning if your server (or proxy) returns HTTP 429 because it’s being overloaded, @uppy/tus will find the ideal sweet spot to keep uploading without overloading.