import { isStorageError } from '../lib/errors' import { DownloadResult } from '../lib/types' export default class StreamDownloadBuilder implements PromiseLike> { constructor( private downloadFn: () => Promise, private shouldThrowOnError: boolean ) {} then, TResult2 = never>( onfulfilled?: | ((value: DownloadResult) => TResult1 | PromiseLike) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | null ): Promise { return this.execute().then(onfulfilled, onrejected) } private async execute(): Promise> { try { const result = await this.downloadFn() return { data: result.body as ReadableStream, error: null, } } catch (error) { if (this.shouldThrowOnError) { throw error } if (isStorageError(error)) { return { data: null, error } } throw error } } }