@@ -8,74 +8,86 @@ import {
88 UseDownloaderOptions ,
99} from './types' ;
1010
11+ /**
12+ *
13+ * @param param0
14+ * @returns
15+ */
1116export const resolver =
1217 ( {
1318 setSize,
1419 setControllerCallback,
1520 setPercentageCallback,
1621 setErrorCallback,
1722 } : ResolverProps ) =>
18- ( response : Response ) : Response => {
19- if ( ! response . ok ) {
20- throw Error ( `${ response . status } ${ response . type } ${ response . statusText } ` ) ;
21- }
23+ ( response : Response ) : Response => {
24+ if ( ! response . ok ) {
25+ throw Error ( `${ response . status } ${ response . type } ${ response . statusText } ` ) ;
26+ }
2227
23- if ( ! response . body ) {
24- throw Error ( 'ReadableStream not yet supported in this browser.' ) ;
25- }
28+ if ( ! response . body ) {
29+ throw Error ( 'ReadableStream not yet supported in this browser.' ) ;
30+ }
2631
27- const responseBody = response . body ;
32+ const responseBody = response . body ;
2833
29- const contentEncoding = response . headers . get ( 'content-encoding' ) ;
30- const contentLength = response . headers . get (
31- contentEncoding ? 'x-file-size' : 'content-length'
32- ) ;
34+ const contentEncoding = response . headers . get ( 'content-encoding' ) ;
35+ const contentLength = response . headers . get (
36+ contentEncoding ? 'x-file-size' : 'content-length'
37+ ) ;
3338
34- const total = parseInt ( contentLength || '0' , 10 ) ;
39+ const total = parseInt ( contentLength || '0' , 10 ) ;
3540
36- setSize ( ( ) => total ) ;
41+ setSize ( ( ) => total ) ;
3742
38- let loaded = 0 ;
43+ let loaded = 0 ;
3944
40- const stream = new ReadableStream < Uint8Array > ( {
41- start ( controller ) {
42- setControllerCallback ( controller ) ;
45+ const stream = new ReadableStream < Uint8Array > ( {
46+ start ( controller ) {
47+ setControllerCallback ( controller ) ;
4348
44- const reader = responseBody . getReader ( ) ;
49+ const reader = responseBody . getReader ( ) ;
4550
46- async function read ( ) : Promise < void > {
47- return reader
48- . read ( )
49- . then ( ( { done, value } ) => {
50- if ( done ) {
51- return controller . close ( ) ;
52- }
51+ async function read ( ) : Promise < void > {
52+ return reader
53+ . read ( )
54+ . then ( ( { done, value } ) => {
55+ if ( done ) {
56+ return controller . close ( ) ;
57+ }
5358
54- loaded += value ?. byteLength || 0 ;
59+ loaded += value ?. byteLength || 0 ;
5560
56- if ( value ) {
57- controller . enqueue ( value ) ;
58- }
61+ if ( value ) {
62+ controller . enqueue ( value ) ;
63+ }
5964
60- setPercentageCallback ( { loaded, total } ) ;
65+ setPercentageCallback ( { loaded, total } ) ;
6166
62- return read ( ) ;
63- } )
64- . catch ( ( error : Error ) => {
65- setErrorCallback ( error ) ;
66- reader . cancel ( 'Cancelled' ) ;
67+ return read ( ) ;
68+ } )
69+ . catch ( ( error : Error ) => {
70+ setErrorCallback ( error ) ;
71+ reader . cancel ( 'Cancelled' ) ;
6772
68- return controller . error ( error ) ;
69- } ) ;
70- }
73+ return controller . error ( error ) ;
74+ } ) ;
75+ }
7176
72- return read ( ) ;
73- } ,
74- } ) ;
77+ return read ( ) ;
78+ } ,
79+ } ) ;
7580
76- return new Response ( stream ) ;
77- } ;
81+ return new Response ( stream ) ;
82+ } ;
7883
84+ /**
85+ *
86+ * @param {Blob } data
87+ * @param {string } filename
88+ * @param {string } mime
89+ * @returns
90+ */
7991export const jsDownload = (
8092 data : Blob ,
8193 filename : string ,
@@ -118,12 +130,17 @@ export const jsDownload = (
118130 } , 200 ) ;
119131} ;
120132
133+ /**
134+ * Initialise a new instance of downloader.
135+ * @param {UseDownloaderOptions } options
136+ * @returns UseDownloader
137+ */
121138export default function useDownloader (
122139 options : UseDownloaderOptions = { }
123140) : UseDownloader {
124141 let debugMode = false ;
125142 try {
126- debugMode = process ? ! ! process ?. env ?. REACT_APP_DEBUG_MODE : false ;
143+ debugMode = process ? ! ! process ?. env ?. REACT_APP_DEBUG_MODE : false ;
127144 } catch {
128145 debugMode = false ;
129146 }
@@ -182,7 +199,7 @@ export default function useDownloader(
182199 } , [ setControllerCallback ] ) ;
183200
184201 const handleDownload : DownloadFunction = useCallback (
185- async ( downloadUrl , filename , timeout = 0 ) => {
202+ async ( downloadUrl , filename , timeout = 0 , overrideOptions = { } ) => {
186203 if ( isInProgress ) return null ;
187204
188205 clearAllStateCallback ( ) ;
@@ -208,6 +225,7 @@ export default function useDownloader(
208225 return fetch ( downloadUrl , {
209226 method : 'GET' ,
210227 ...options ,
228+ ...overrideOptions ,
211229 signal : fetchController . signal ,
212230 } )
213231 . then ( resolverWithProgress )
0 commit comments