From 05b5feb1bc3e9296afe610c2c0937afbfdd8e589 Mon Sep 17 00:00:00 2001 From: Marius Kleidl Date: Tue, 10 Sep 2024 17:50:48 +0200 Subject: [PATCH] Correct type definition for `onSuccess` callback Fixes https://github.com/tus/tus-js-client/issues/716 --- lib/index.d.ts | 6 +++++- lib/index.test-d.ts | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 3ef96c9f..4a94eea2 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -32,7 +32,7 @@ interface UploadOptions { onProgress?: ((bytesSent: number, bytesTotal: number) => void) | null onChunkComplete?: ((chunkSize: number, bytesAccepted: number, bytesTotal: number) => void) | null - onSuccess?: (() => void) | null + onSuccess?: ((payload: OnSuccessPayload) => void) | null onError?: ((error: Error | DetailedError) => void) | null onShouldRetry?: | ((error: DetailedError, retryAttempt: number, options: UploadOptions) => boolean) @@ -59,6 +59,10 @@ interface UploadOptions { httpStack?: HttpStack } +interface OnSuccessPayload { + lastResponse: HttpResponse +} + interface UrlStorage { findAllUploads(): Promise findUploadsByFingerprint(fingerprint: string): Promise diff --git a/lib/index.test-d.ts b/lib/index.test-d.ts index 6f1f399d..0869f411 100644 --- a/lib/index.test-d.ts +++ b/lib/index.test-d.ts @@ -27,8 +27,9 @@ const upload = new tus.Upload(file, { console.log(bytesSent, bytesTotal, `${percentage}%`) }, onChunkComplete: (_chunkSize: number, _bytesAccepted: number) => {}, - onSuccess: () => { + onSuccess: (payload: tus.OnSuccessPayload) => { console.log('Download from %s complete', upload.url) + console.log('Response header', payload.lastResponse.getHeader('X-Info')) }, onError: (error: Error | DetailedError) => { console.error(`Failed because: ${error}`)