瀏覽代碼

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

* @uppy/tus: set response from tus-js-client

* Revert UppyFile comment

* Bump tus-js-client
Merlijn Vos 7 月之前
父節點
當前提交
f665a5ab81
共有 5 個文件被更改,包括 66 次插入8 次删除
  1. 24 0
      docs/uploader/tus.mdx
  2. 1 1
      packages/@uppy/tus/package.json
  3. 11 2
      packages/@uppy/tus/src/index.test.ts
  4. 14 4
      packages/@uppy/tus/src/index.ts
  5. 16 1
      yarn.lock

+ 24 - 0
docs/uploader/tus.mdx

@@ -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

+ 1 - 1
packages/@uppy/tus/package.json

@@ -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"

+ 11 - 2
packages/@uppy/tus/src/index.test.ts

@@ -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', () => {
@@ -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
+    >()
+  })
 })

+ 14 - 4
packages/@uppy/tus/src/index.ts

@@ -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 {
@@ -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)
@@ -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)

+ 16 - 1
yarn.lock

@@ -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:^"
@@ -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"