@@ -22,15 +22,19 @@ import fetchInChunks from 'fetch-in-chunks';
2222### Function Signature
2323
2424``` js
25- async function fetchInChunks (
26- url ,
27- chunkSize = 5 * 1024 * 1024 ,
28- maxParallelRequests = 6 ,
29- progressCallback = null ,
30- signal = null
31- )
25+ async function fetchInChunks (url , options = {})
3226```
3327
28+ ### Options Object
29+
30+ - `chunkSize` (`number`, optional): The size of each chunk in bytes. Defaults to
31+ 5 MB (5 _ 1024 _ 1024).
32+ - `maxParallelRequests` (`number`, optional): The maximum number of parallel
33+ chunk requests. Defaults to 6. `progressCallback` (`function`, optional): A
34+ callback function that is called with the downloaded bytes and total file
35+ size. `signal` (`AbortSignal`, optional): An `AbortSignal` to allow aborting
36+ the request.
37+
3438#### Parameters
3539
3640- `url` (`string`): The URL of the file to download.
@@ -80,14 +84,11 @@ import fetchInChunks from 'fetch-in-chunks';
8084
8185async function downloadFileWithProgress () {
8286 try {
83- const blob = await fetchInChunks (
84- ' https://example.com/largefile.zip' ,
85- 5 * 1024 * 1024 ,
86- 6 ,
87- (downloaded , total ) => {
87+ const blob = await fetchInChunks (' https://example.com/largefile.zip' , {
88+ progressCallback : (downloaded , total ) => {
8889 console .log (` Downloaded ${ ((downloaded / total) * 100 ).toFixed (2 )} %` );
8990 },
90- );
91+ } );
9192 // Create a download link and trigger the download
9293 const url = URL .createObjectURL (blob);
9394 const a = document .createElement (' a' );
@@ -114,15 +115,9 @@ async function downloadFileWithAbort() {
114115 const signal = controller .signal ;
115116
116117 try {
117- const blob = await fetchInChunks (
118- ' https://example.com/largefile.zip' ,
119- 5 * 1024 * 1024 ,
120- 6 ,
121- (downloaded , total ) => {
122- console .log (` Downloaded ${ ((downloaded / total) * 100 ).toFixed (2 )} %` );
123- },
118+ const blob = await fetchInChunks (' https://example.com/largefile.zip' , {
124119 signal,
125- );
120+ } );
126121 // Create a download link and trigger the download
127122 const url = URL .createObjectURL (blob);
128123 const a = document .createElement (' a' );
0 commit comments