From 174301ec27c06c49c674a5bafa4aafe4eb65c1d3 Mon Sep 17 00:00:00 2001 From: Dani Latorre Date: Wed, 22 Sep 2021 13:29:05 +0200 Subject: [PATCH 1/2] Adding support to define the headers as a function in the Tus protocol plugin --- packages/@uppy/tus/src/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/@uppy/tus/src/index.js b/packages/@uppy/tus/src/index.js index c73741e249..f1ef595b4a 100644 --- a/packages/@uppy/tus/src/index.js +++ b/packages/@uppy/tus/src/index.js @@ -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, From f03f4a62619d490488f86961d4196283d54677c0 Mon Sep 17 00:00:00 2001 From: Dani Latorre Date: Tue, 28 Sep 2021 16:52:43 +0200 Subject: [PATCH 2/2] Updating the TUS protocol documentation adding the explanation about how to add headers to the requests as a function --- website/src/docs/tus.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/website/src/docs/tus.md b/website/src/docs/tus.md index 486f3b83c3..d4591dfa14 100644 --- a/website/src/docs/tus.md +++ b/website/src/docs/tus.md @@ -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` @@ -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