Skip to content

Commit 2076ed1

Browse files
cjauvinAcconutMarius
authored
Expose the onUploadUrlAvailable callback option (#543)
* Expose the onUploadUrlAvailable callback option * Implement requested changes * Update api.md * Fix linting issues --------- Co-authored-by: Marius <[email protected]> Co-authored-by: Marius <[email protected]>
1 parent 97d9fdf commit 2076ed1

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

docs/api.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,24 @@ onShouldRetry: function (err, retryAttempt, options) {
9494
console.log("Error", err)
9595
console.log("Request", err.originalRequest)
9696
console.log("Response", err.originalResponse)
97-
97+
9898
var status = err.originalResponse ? err.originalResponse.getStatus() : 0
9999
// Do not retry if the status is a 403.
100100
if (status === 403) {
101101
return false
102102
}
103-
103+
104104
// For any other status code, we retry.
105105
return true
106106
}
107107
```
108108

109+
#### onUploadUrlAvailable
110+
111+
*Default value:* `null`
112+
113+
An optional function called once the upload URL is available. At this point, the `tus.Upload#url` property is guaranteed to be accessible and valid. This occurs after inspecting the `Location` header in the response to the initial POST request, or when an upload URL is confirmed using a HEAD request. Due to network errors and retries, this callback might be invoked multiple times for a single upload.
114+
109115
#### headers
110116

111117
*Default value:* `{}`

lib/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface UploadOptions {
3333
onSuccess?: (() => void) | null;
3434
onError?: ((error: (Error | DetailedError)) => void) | null;
3535
onShouldRetry?: ((error: (Error | DetailedError), retryAttempt: number, options: UploadOptions) => boolean) | null;
36+
onUploadUrlAvailable?: (() => void) | null;
3637

3738
overridePatchMethod?: boolean;
3839
headers?: { [key: string]: string };

lib/upload.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ const defaultOptions = {
1212
fingerprint: null,
1313
uploadSize : null,
1414

15-
onProgress : null,
16-
onChunkComplete : null,
17-
onSuccess : null,
18-
onError : null,
19-
_onUploadUrlAvailable: null,
15+
onProgress : null,
16+
onChunkComplete : null,
17+
onSuccess : null,
18+
onError : null,
19+
onUploadUrlAvailable: null,
2020

2121
overridePatchMethod: false,
2222
headers : {},
@@ -311,7 +311,7 @@ class BaseUpload {
311311
},
312312
// Wait until every partial upload has an upload URL, so we can add
313313
// them to the URL storage.
314-
_onUploadUrlAvailable: () => {
314+
onUploadUrlAvailable: () => {
315315
this._parallelUploadUrls[index] = upload.url
316316
// Test if all uploads have received an URL
317317
if (this._parallelUploadUrls.filter(u => Boolean(u)).length === parts.length) {
@@ -568,8 +568,8 @@ class BaseUpload {
568568
this.url = resolveUrl(this.options.endpoint, location)
569569
log(`Created upload at ${this.url}`)
570570

571-
if (typeof this.options._onUploadUrlAvailable === 'function') {
572-
this.options._onUploadUrlAvailable()
571+
if (typeof this.options.onUploadUrlAvailable === 'function') {
572+
this.options.onUploadUrlAvailable()
573573
}
574574

575575
if (this._size === 0) {
@@ -647,8 +647,8 @@ class BaseUpload {
647647
return
648648
}
649649

650-
if (typeof this.options._onUploadUrlAvailable === 'function') {
651-
this.options._onUploadUrlAvailable()
650+
if (typeof this.options.onUploadUrlAvailable === 'function') {
651+
this.options.onUploadUrlAvailable()
652652
}
653653

654654
this._saveUploadInUrlStorage()

test/spec/test-common.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ describe('tus', () => {
4040
nonlatin: 'słońce',
4141
number : 100,
4242
},
43-
withCredentials: true,
43+
withCredentials : true,
4444
onProgress () {},
45-
onSuccess : waitableFunction(),
45+
onUploadUrlAvailable: waitableFunction('onUploadUrlAvailable'),
46+
onSuccess : waitableFunction('onSuccess'),
4647
}
4748
spyOn(options, 'onProgress')
4849

@@ -68,6 +69,8 @@ describe('tus', () => {
6869

6970
req = await testStack.nextRequest()
7071

72+
expect(options.onUploadUrlAvailable).toHaveBeenCalled()
73+
7174
expect(req.url).toBe('https://tus.io/uploads/blargh')
7275
expect(req.method).toBe('PATCH')
7376
expect(req.requestHeaders.Custom).toBe('blargh')
@@ -533,11 +536,12 @@ describe('tus', () => {
533536
const testStack = new TestHttpStack()
534537
const file = getBlob('hello world')
535538
const options = {
536-
httpStack: testStack,
537-
endpoint : 'http://tus.io/uploads',
538-
uploadUrl: 'http://tus.io/files/upload',
539+
httpStack : testStack,
540+
endpoint : 'http://tus.io/uploads',
541+
uploadUrl : 'http://tus.io/files/upload',
539542
onProgress () {},
540-
onSuccess: waitableFunction('onSuccess'),
543+
onUploadUrlAvailable: waitableFunction('onUploadUrlAvailable'),
544+
onSuccess : waitableFunction('onSuccess'),
541545
fingerprint () {},
542546
}
543547
spyOn(options, 'fingerprint').and.resolveTo('fingerprinted')
@@ -562,6 +566,9 @@ describe('tus', () => {
562566
})
563567

564568
req = await testStack.nextRequest()
569+
570+
expect(options.onUploadUrlAvailable).toHaveBeenCalled()
571+
565572
expect(req.url).toBe('http://tus.io/files/upload')
566573
expect(req.method).toBe('PATCH')
567574
expect(req.requestHeaders['Tus-Resumable']).toBe('1.0.0')

0 commit comments

Comments
 (0)