Skip to content

Commit 0585038

Browse files
committed
Merge branch 'develop'
2 parents 4a5261e + 21c00f6 commit 0585038

File tree

6 files changed

+79
-21
lines changed

6 files changed

+79
-21
lines changed

.all-contributorsrc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@
7272
"bug",
7373
"code"
7474
]
75+
},
76+
{
77+
"login": "davecarlson",
78+
"name": "Dave Carlson",
79+
"avatar_url": "https://avatars.githubusercontent.com/u/299702?v=4",
80+
"profile": "https://github.com/davecarlson",
81+
"contributions": [
82+
"ideas"
83+
]
7584
}
7685
],
7786
"contributorsPerLine": 7,
@@ -82,4 +91,4 @@
8291
"skipCi": true,
8392
"commitConvention": "angular",
8493
"commitType": "docs"
85-
}
94+
}

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
### [1.2.8](https://github.com/the-bugging/react-use-downloader/compare/v1.2.6...v1.2.8) (2024-04-17)
6+
7+
8+
### Documentation
9+
10+
* update .all-contributorsrc [skip ci] ([6ee52a6](https://github.com/the-bugging/react-use-downloader/commit/6ee52a6393f393d3241d68c7c68c491d6f8bfe65))
11+
* update README.md [skip ci] ([7b00739](https://github.com/the-bugging/react-use-downloader/commit/7b00739007fdf78b457bc22ed7d21e5e83a5a9a0))
12+
513
### [1.2.7](https://github.com/the-bugging/react-use-downloader/compare/v1.2.6...v1.2.7) (2024-04-16)
614

715
### [1.2.6](https://github.com/the-bugging/react-use-downloader/compare/v1.2.5...v1.2.6) (2024-04-09)

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
| Statements | Branches | Functions | Lines |
1010
| ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
11-
| ![Statements](https://img.shields.io/badge/statements-87.38%25-yellow.svg?style=flat&logo=jest) | ![Branches](https://img.shields.io/badge/branches-72.5%25-red.svg?style=flat&logo=jest) | ![Functions](https://img.shields.io/badge/functions-81.81%25-yellow.svg?style=flat&logo=jest) | ![Lines](https://img.shields.io/badge/lines-88%25-yellow.svg?style=flat&logo=jest) |
11+
| ![Statements](https://img.shields.io/badge/statements-87.38%25-yellow.svg?style=flat&logo=jest) | ![Branches](https://img.shields.io/badge/branches-73.17%25-red.svg?style=flat&logo=jest) | ![Functions](https://img.shields.io/badge/functions-81.81%25-yellow.svg?style=flat&logo=jest) | ![Lines](https://img.shields.io/badge/lines-88%25-yellow.svg?style=flat&logo=jest) |
1212

1313
## Table of Contents
1414

@@ -76,15 +76,15 @@ export default function App() {
7676

7777
- An object with the following keys:
7878

79-
| key | description | arguments |
80-
| ------------ | -------------------------------- | --------------------------------------------------------- |
81-
| size | size in bytes | n/a |
82-
| elapsed | elapsed time in seconds | n/a |
83-
| percentage | percentage in string | n/a |
84-
| download | download function handler | (downloadUrl: string, filename: string, timeout?: number) |
85-
| cancel | cancel function handler | n/a |
86-
| error | error object from the request | n/a |
87-
| isInProgress | boolean denoting download status | n/a |
79+
| key | description | arguments |
80+
| ------------ | -------------------------------- | ------------------------------------------------------------------------------------------------- |
81+
| size | size in bytes | n/a |
82+
| elapsed | elapsed time in seconds | n/a |
83+
| percentage | percentage in string | n/a |
84+
| download | download function handler | (downloadUrl: string, filename: string, timeout?: number, overrideOptions?: UseDownloaderOptions) |
85+
| cancel | cancel function handler | n/a |
86+
| error | error object from the request | n/a |
87+
| isInProgress | boolean denoting download status | n/a |
8888

8989
```jsx
9090
const { size, elapsed, percentage, download, cancel, error, isInProgress } =
@@ -123,7 +123,10 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
123123
<td align="center" valign="top" width="14.28%"><a href="http://bzbetty.blogspot.com"><img src="https://avatars.githubusercontent.com/u/533131?v=4?s=100" width="100px;" alt="Sam "Betty" McKoy"/><br /><sub><b>Sam "Betty" McKoy</b></sub></a><br /><a href="https://github.com/the-bugging/react-use-downloader/issues?q=author%3Abzbetty" title="Bug reports">🐛</a></td>
124124
<td align="center" valign="top" width="14.28%"><a href="https://github.com/peranosborn"><img src="https://avatars.githubusercontent.com/u/1318002?v=4?s=100" width="100px;" alt="Peran Osborn"/><br /><sub><b>Peran Osborn</b></sub></a><br /><a href="https://github.com/the-bugging/react-use-downloader/issues?q=author%3Aperanosborn" title="Bug reports">🐛</a> <a href="#ideas-peranosborn" title="Ideas, Planning, & Feedback">🤔</a></td>
125125
<td align="center" valign="top" width="14.28%"><a href="https://github.com/MarcosRS"><img src="https://avatars.githubusercontent.com/u/12486814?v=4?s=100" width="100px;" alt="Marcos"/><br /><sub><b>Marcos</b></sub></a><br /><a href="https://github.com/the-bugging/react-use-downloader/issues?q=author%3AMarcosRS" title="Bug reports">🐛</a> <a href="#ideas-MarcosRS" title="Ideas, Planning, & Feedback">🤔</a></td>
126-
<td align="center" valign="top" width="14.28%"><a href="https://github.com/9swampy"><img src="https://avatars.githubusercontent.com/u/523054?v=4?s=100" width="100px;" alt="9swampy"/><br /><sub><b>9swampy</b></sub></a><br /><a href="https://github.com/the-bugging/react-use-downloader/issues?q=author%3A9swampy" title="Bug reports">🐛</a> <a href="https://github.com/the-bugging/react-use-downloader/commits?author%3A9swampy" title="Code">💻</a></td>
126+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/9swampy"><img src="https://avatars.githubusercontent.com/u/523054?v=4?s=100" width="100px;" alt="9swampy"/><br /><sub><b>9swampy</b></sub></a><br /><a href="https://github.com/the-bugging/react-use-downloader/issues?q=author%3A9swampy" title="Bug reports">🐛</a> <a href="https://github.com/the-bugging/react-use-downloader/commits?author=9swampy" title="Code">💻</a></td>
127+
</tr>
128+
<tr>
129+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/davecarlson"><img src="https://avatars.githubusercontent.com/u/299702?v=4?s=100" width="100px;" alt="Dave Carlson"/><br /><sub><b>Dave Carlson</b></sub></a><br /><a href="#ideas-davecarlson" title="Ideas, Planning, & Feedback">🤔</a></td>
127130
</tr>
128131
</tbody>
129132
</table>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-use-downloader",
3-
"version": "1.2.7",
3+
"version": "1.2.8",
44
"description": "Creates a download handler function and gives progress information",
55
"author": "Olavo Parno",
66
"license": "MIT",

src/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {
88
UseDownloaderOptions,
99
} from './types';
1010

11+
/**
12+
* Resolver function to handle the download progress.
13+
* @param {ResolverProps} props
14+
* @returns {Response}
15+
*/
1116
export const resolver =
1217
({
1318
setSize,
@@ -76,6 +81,13 @@ export const resolver =
7681
return new Response(stream);
7782
};
7883

84+
/**
85+
* jsDownload function to handle the download process.
86+
* @param {Blob} data
87+
* @param {string} filename
88+
* @param {string} mime
89+
* @returns {boolean | NodeJS.Timeout}
90+
*/
7991
export const jsDownload = (
8092
data: Blob,
8193
filename: string,
@@ -118,12 +130,17 @@ export const jsDownload = (
118130
}, 200);
119131
};
120132

133+
/**
134+
* useDownloader hook to handle the download process.
135+
* @param {UseDownloaderOptions} options
136+
* @returns {UseDownloader}
137+
*/
121138
export 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)

src/types.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ export type ErrorMessage = {
44
errorMessage: string;
55
} | null;
66

7+
/** useDownloader options for fetch call
8+
* See fetch RequestInit for more details
9+
*/
10+
export type UseDownloaderOptions = RequestInit;
11+
12+
/**
13+
* Initiate the download of the specified asset from the specified url. Optionally supply timeout and overrideOptions.
14+
* @example await download('https://example.com/file.zip', 'file.zip')
15+
* @example await download('https://example.com/file.zip', 'file.zip', 500) timeouts after 500ms
16+
* @example await download('https://example.com/file.zip', 'file.zip', undefined, { method: 'GET' }) skips optional timeout but supplies overrideOptions
17+
*/
718
export type DownloadFunction = (
819
/** Download url
920
* @example https://upload.wikimedia.org/wikipedia/commons/4/4d/%D0%93%D0%BE%D0%B2%D0%B5%D1%80%D0%BB%D0%B0_%D1%96_%D0%9F%D0%B5%D1%82%D1%80%D0%BE%D1%81_%D0%B2_%D0%BF%D1%80%D0%BE%D0%BC%D1%96%D0%BD%D1%8F%D1%85_%D0%B2%D1%80%D0%B0%D0%BD%D1%96%D1%88%D0%BD%D1%8C%D0%BE%D0%B3%D0%BE_%D1%81%D0%BE%D0%BD%D1%86%D1%8F.jpg
@@ -14,9 +25,23 @@ export type DownloadFunction = (
1425
*/
1526
filename: string,
1627
/** Optional timeout to download items */
17-
timeout?: number
28+
timeout?: number,
29+
/** Optional options to supplement and/or override UseDownloader options */
30+
overrideOptions?: UseDownloaderOptions
1831
) => Promise<void | null>;
1932

33+
/**
34+
* Provides access to Downloader functionality and settings.
35+
*
36+
* @interface EditDialogField
37+
* @field {number} size in bytes.
38+
* @field {number} elapsed time in seconds.
39+
* @field {number} percentage in string
40+
* @field {DownloadFunction} download function handler
41+
* @field {void} cancel function handler
42+
* @field {ErrorMessage} error object from the request
43+
* @field {boolean} isInProgress boolean flag denoting download status
44+
*/
2045
export interface UseDownloader {
2146
/** Size in bytes */
2247
size: number;
@@ -60,8 +85,3 @@ interface CustomNavigator extends Navigator {
6085
export interface WindowDownloaderEmbedded extends Window {
6186
navigator: CustomNavigator;
6287
}
63-
64-
/** useDownloader options for fetch call
65-
* See fetch RequestInit for more details
66-
*/
67-
export type UseDownloaderOptions = RequestInit;

0 commit comments

Comments
 (0)