Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Commit 80e6fdc

Browse files
author
Elad Gil
committed
updated readme and version
1 parent 2c7102c commit 80e6fdc

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,38 @@ for (let task of lostTask) {
127127

128128
`task.id` is very important for re-attaching the download task with any UI component representing that task, this is why you need to make sure to give sensible IDs that you know what to do with, try to avoid using random IDs.
129129

130+
### Using costume headers
131+
If you need to send costume headers with your download request, you can do in it 2 ways:
132+
133+
1) Globally using `RNBackgroundDownloader.setHeaders()`:
134+
```javascript
135+
RNBackgroundDownloader.setHeaders({
136+
Authorization: 'Bearer 2we$@$@Ddd223'
137+
});
138+
```
139+
This way, all downloads with have the given headers.
140+
141+
2) Per download by passing a headers object in the options of `RNBackgroundDownloader.download()`:
142+
```javascript
143+
let task = RNBackgroundDownloader.download({
144+
id: 'file123',
145+
url: 'https://link-to-very.large/file.zip'
146+
destination: `${RNBackgroundDownloader.directories.documents}/file.zip`,
147+
headers: {
148+
Authorization: 'Bearer 2we$@$@Ddd223'
149+
}
150+
}).begin((expectedBytes) => {
151+
console.log(`Going to download ${expectedBytes} bytes!`);
152+
}).progress((percent) => {
153+
console.log(`Downloaded: ${percent * 100}%`);
154+
}).done(() => {
155+
console.log('Download is done!');
156+
}).error((error) => {
157+
console.log('Download canceled due to error: ', error);
158+
});
159+
```
160+
Headers given in the `download` function are **merged** with the ones given in `setHeaders`.
161+
130162
## API
131163

132164
### RNBackgroundDownloader
@@ -144,6 +176,7 @@ An object containing options properties
144176
| `id` | String || All | A Unique ID to provide for this download. This ID will help to identify the download task when the app re-launches |
145177
| `url` | String || All | URL to file you want to download |
146178
| `destination` | String || All | Where to copy the file to once the download is done |
179+
| `headers` | Object | | All | Costume headers to add to the download request. These are merged with the headers given in the `setHeaders` function
147180
| `priority` | [Priority (enum)](#priority-enum---android-only) | | Android | The priority of the download. On Android, downloading is limited to 4 simultaneous instances where further downloads are queued. Priority helps in deciding which download to pick next from the queue. **Default:** Priority.MEDIUM |
148181
| `network` | [Network (enum)](#network-enum---android-only) | | Android | Give your the ability to limit the download to WIFI only. **Default:** Network.ALL |
149182

@@ -159,6 +192,12 @@ Checks for downloads that ran in background while you app was terminated. Recomm
159192

160193
`DownloadTask[]` - Array of tasks that were running in the background so you can re-attach callbacks to them
161194

195+
### `setHeaders(headers)`
196+
197+
Sets headers to use in all future downloads.
198+
199+
**headers** - Object
200+
162201
### DownloadTask
163202

164203
A class representing a download task created by `RNBackgroundDownloader.download`
@@ -218,4 +257,4 @@ An absolute path to the app's documents directory. It is recommended that you us
218257
Developed by [Elad Gil](https://github.com/ptelad) of [Eko](http://www.helloeko.com)
219258

220259
## License
221-
Apache 2
260+
Apache 2

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ RNBackgroundDownloaderEmitter.addListener('downloadBegin', event => {
3939
});
4040

4141
export function setHeaders(h = {}) {
42+
if (typeof h !== 'object') {
43+
throw new Error('[RNBackgroundDownloader] headers must be an object');
44+
}
4245
headers = h;
4346
}
4447

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-background-downloader",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "A library for React-Native to help you download large files on iOS and Android both in the foreground and most importantly in the background.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)