diff --git a/lib/options.ts b/lib/options.ts index 53a1e284..47a95295 100644 --- a/lib/options.ts +++ b/lib/options.ts @@ -63,6 +63,7 @@ export interface UploadOptions { onError?: (error: Error | DetailedError) => void onShouldRetry?: (error: DetailedError, retryAttempt: number, options: UploadOptions) => boolean onUploadUrlAvailable?: () => void | Promise + onShouldRemoveFromUrlStorage?: () => boolean | Promise overridePatchMethod: boolean headers: { [key: string]: string } diff --git a/lib/upload.ts b/lib/upload.ts index 3cebb34c..87751cbc 100644 --- a/lib/upload.ts +++ b/lib/upload.ts @@ -32,6 +32,7 @@ export const defaultOptions = { onSuccess: undefined, onError: undefined, onUploadUrlAvailable: undefined, + onShouldRemove: undefined, overridePatchMethod: false, headers: {}, @@ -938,12 +939,20 @@ export class BaseUpload { /** * Remove the entry in the URL storage, if it has been saved before. + * If the `onShouldRemoveFromUrlStorage` option is provided and is a function, + * it will be invoked to determine whether the removal should proceed. + * If it returns or resolves to `false`, the removal is skipped. * * @api private */ private async _removeFromUrlStorage(): Promise { if (!this._urlStorageKey) return + if (typeof this.options.onShouldRemoveFromUrlStorage === 'function') { + const shouldRemove = await Promise.resolve(this.options.onShouldRemoveFromUrlStorage()) + if (!shouldRemove) return + } + await this.options.urlStorage.removeUpload(this._urlStorageKey) this._urlStorageKey = undefined }