Skip to content

Commit

Permalink
@uppy/tus: add file argument to onBeforeRequest (#3984)
Browse files Browse the repository at this point in the history
Add file argument to `onBeforeRequest`.

(cherry picked from commit 8195b22)
  • Loading branch information
Murderlon authored and aduh95 committed Aug 17, 2022
1 parent 9aa5d6a commit 3d0972a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/@uppy/tus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
6 changes: 4 additions & 2 deletions packages/@uppy/tus/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<UploadOptions, Exclude<keyof UploadOptions,
| 'fingerprint'
| 'metadata'
| 'onBeforeRequest'
| 'onProgress'
| 'onChunkComplete'
| 'onShouldRetry'
Expand All @@ -21,6 +22,7 @@ export interface TusOptions extends PluginOptions, TusUploadOptions {
useFastRemoteRetry?: boolean
withCredentials?: boolean
onShouldRetry?: (err: Error | undefined, retryAttempt: number, options: TusOptions, next: Next) => boolean
onBeforeRequest?: (req: HttpRequest, file: UppyFile) => Promise<void>
}

declare class Tus extends BasePlugin<TusOptions> {}
Expand Down
6 changes: 5 additions & 1 deletion website/src/docs/tus.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 3d0972a

Please sign in to comment.