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

@uppy/tus: set response from tus-js-client #5456

Merged
merged 3 commits into from
Sep 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/uploader/tus.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ new Uppy()
.use(Tus, { endpoint: 'https://tusd.tusdemo.net/files/' });
```

### TypeScript

If you want the `response` argument on the `upload-success` event and
`file.response.body` to be typed, you have to pass a generic to the Uppy class.

```ts showLineNumbers
// ...
import Tus, { type TusBody } from '@uppy/tus';

type MyMeta = {
/* your added meta data */
};

const uppy = new Uppy<MyMeta, TusBody>().use(Tus, {
endpoint: 'https://tusd.tusdemo.net/files/',
});

const [firstFile] = uppy.getFiles();

// Correctly typed as XMLHttpRequest.
// Populated after uppy.upload()
firstFile.response.body.xhr;
```

## API

### Options
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/tus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dependencies": {
"@uppy/companion-client": "workspace:^",
"@uppy/utils": "workspace:^",
"tus-js-client": "^4.1.0"
"tus-js-client": "^4.2.3"
},
"devDependencies": {
"vitest": "^1.2.1"
Expand Down
13 changes: 11 additions & 2 deletions packages/@uppy/tus/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, expectTypeOf, it } from 'vitest'
import Core from '@uppy/core'
import Tus from './index.ts'
import Tus, { type TusBody } from './index.ts'

describe('Tus', () => {
it('Throws errors if autoRetry option is true', () => {
Expand Down Expand Up @@ -35,4 +35,13 @@ describe('Tus', () => {
/The `autoRetry` option was deprecated and has been removed/,
)
})

it('propagates the TusBody type', () => {
const uppy = new Core<any, TusBody>()
const id = uppy.addFile({ name: 'test.jpg', data: { size: 1024 } })
const file = uppy.getFile(id)
expectTypeOf(file.response?.body).toEqualTypeOf<
{ xhr: XMLHttpRequest } | undefined
>()
})
})
18 changes: 14 additions & 4 deletions packages/@uppy/tus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type RestTusUploadOptions = Omit<

export type TusDetailedError = tus.DetailedError

export type TusBody = { xhr: XMLHttpRequest }

export interface TusOpts<M extends Meta, B extends Body>
extends PluginOpts,
RestTusUploadOptions {
Expand Down Expand Up @@ -307,11 +309,19 @@ export default class Tus<M extends Meta, B extends Body> extends BasePlugin<
})
}

uploadOptions.onSuccess = () => {
const uploadResp = {
uploadOptions.onSuccess = (payload) => {
const uploadResp: UppyFile<M, B>['response'] = {
uploadURL: upload.url ?? undefined,
status: 200,
body: {} as B,
body: {
// We have to put `as XMLHttpRequest` because tus-js-client
// returns `any`, as the type differs in Node.js and the browser.
// In the browser it's always `XMLHttpRequest`.
xhr: payload.lastResponse.getUnderlyingObject() as XMLHttpRequest,
// Body extends Record<string, unknown> and thus `xhr` is not known
// but we export the `TusBody` type, which people pass as a generic into the Uppy class,
// so on the implementer side it works as expected.
} as unknown as B,
}

this.resetUploaderReferences(file.id)
Expand All @@ -325,7 +335,7 @@ export default class Tus<M extends Meta, B extends Body> extends BasePlugin<
this.uppy.log(`Download ${name} from ${upload.url}`)
}
if (typeof opts.onSuccess === 'function') {
opts.onSuccess()
opts.onSuccess(payload)
}

resolve(upload)
Expand Down
17 changes: 16 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9084,7 +9084,7 @@ __metadata:
dependencies:
"@uppy/companion-client": "workspace:^"
"@uppy/utils": "workspace:^"
tus-js-client: "npm:^4.1.0"
tus-js-client: "npm:^4.2.3"
vitest: "npm:^1.2.1"
peerDependencies:
"@uppy/core": "workspace:^"
Expand Down Expand Up @@ -28689,6 +28689,21 @@ __metadata:
languageName: node
linkType: hard

"tus-js-client@npm:^4.2.3":
version: 4.2.3
resolution: "tus-js-client@npm:4.2.3"
dependencies:
buffer-from: "npm:^1.1.2"
combine-errors: "npm:^3.0.3"
is-stream: "npm:^2.0.0"
js-base64: "npm:^3.7.2"
lodash.throttle: "npm:^4.1.1"
proper-lockfile: "npm:^4.1.2"
url-parse: "npm:^1.5.7"
checksum: 10/5beaa5901a4d3f9f8c61b2c10e776a34b0401327c83919239e3a19539321fcc05a4b5051bcdb5e8880d0887c3a857b7c27224730734d3617c17c3d66d64004a4
languageName: node
linkType: hard

"tv4@npm:^1.3.0":
version: 1.3.0
resolution: "tv4@npm:1.3.0"
Expand Down
Loading