Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to define the headers as a function in the Tus protocol plugin #3221

Merged
merged 2 commits into from Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/@uppy/tus/src/index.js
Expand Up @@ -185,6 +185,10 @@ module.exports = class Tus extends BasePlugin {
...(file.tus || {}),
}

if (typeof opts.headers === 'function') {
opts.headers = opts.headers(file)
}

/** @type {RawTusOptions} */
const uploadOptions = {
...tusDefaultOptions,
Expand Down
22 changes: 21 additions & 1 deletion website/src/docs/tus.md
Expand Up @@ -47,7 +47,25 @@ Destination URL for your uploads. This should be where your tus.io server is run

### `headers: {}`

Additional request headers to send to the Tus endpoint when making requests.
An object containing additional HTTP headers to send to the Tus endpoint when making requests.
Keys are header names, values are header values.

```js
const headers = {
authorization: `Bearer ${window.getCurrentUserToken()}`,
}
```

Header values can also be derived from file data by providing a function. The function receives a [File Object][File Objects] and must return an object where the keys are header names, and values are header values.

```js
const headers = (file) => {
return {
authorization: `Bearer ${window.getCurrentUserToken()}`,
expires: file.meta.expires,
}
}
```

### `chunkSize: Infinity`

Expand Down Expand Up @@ -80,3 +98,5 @@ Pass an array of field names to limit the metadata fields that will be added to
Limit the amount of uploads going on at the same time. Setting this to `0` means there is no limit on concurrent uploads.

[tus-js-client]: https://github.com/tus/tus-js-client

[File Objects]: /docs/uppy/#File-Objects