File tree Expand file tree Collapse file tree 1 file changed +10
-3
lines changed
Expand file tree Collapse file tree 1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change 11import fs from 'fs-extra' ;
22import { uninterceptedApiClient } from '../lib/api/ApiClient' ;
3- export async function downloadFile ( url : string , filePath : string ) {
3+
4+ export async function downloadFile ( url : string , filePath : string ) : Promise < void > {
5+ // Ensure the file exists before attempting to write to it
46 fs . ensureFileSync ( filePath ) ;
7+
8+ // Create a writable stream to save the downloaded file
59 const writer = fs . createWriteStream ( filePath ) ;
610
11+ // Fetch the file as a stream
712 const response = await uninterceptedApiClient . get ( url , {
813 responseType : 'stream' ,
914 } ) ;
1015
16+ // Pipe the data stream to the file writer
1117 response . data . pipe ( writer ) ;
1218
19+ // Return a promise that resolves when the download is finished or rejects if an error occurs
1320 return new Promise ( ( resolve , reject ) => {
14- writer . on ( 'finish' , resolve ) ;
15- writer . on ( 'error' , reject ) ;
21+ writer . on ( 'finish' , ( ) => resolve ( ) ) ; // Ensure resolve is called without any arguments
22+ writer . on ( 'error' , error => reject ( error ) ) ; // Directly pass the error to the reject function
1623 } ) ;
1724}
You can’t perform that action at this time.
0 commit comments