downloadFile and uploadFiles return objects instead of promises

This commit is contained in:
Chris Dell 2016-08-10 20:29:14 +01:00
parent 0b47ced162
commit 5af22b2782
3 changed files with 30 additions and 28 deletions

View File

@ -1,5 +1,5 @@
[ignore]
.*/node_modules/.*
.*/node_modules/react_native/.*
[include]
@ -8,4 +8,4 @@
[options]
[version]
0.27.0
0.28.0

View File

@ -272,19 +272,7 @@ var RNFS = {
return RNFSManager.appendFile(filepath, b64);
},
downloadFile(options: DownloadFileOptions): Promise<DownloadResult> {
if (arguments.length > 1) {
console.warn('Deprecated: Please see updated docs for `downloadFile`');
options = {
fromUrl: arguments[0],
toFile: arguments[1],
begin: arguments[2],
progress: arguments[3],
background: false
};
}
downloadFile(options: DownloadFileOptions): { jobId: number, promise: Promise<DownloadResult> } {
if (typeof options !== 'object') throw new Error('downloadFile: Invalid value for argument `options`');
if (typeof options.fromUrl !== 'string') throw new Error('downloadFile: Invalid value for property `fromUrl`');
if (typeof options.toFile !== 'string') throw new Error('downloadFile: Invalid value for property `toFile`');
@ -312,13 +300,23 @@ var RNFS = {
progressDivider: options.progressDivider || 0
};
return RNFSManager.downloadFile(bridgeOptions).then(res => {
subscriptions.forEach(sub => sub.remove());
return res;
});
return {
jobId,
promise: RNFSManager.downloadFile(bridgeOptions).then(res => {
subscriptions.forEach(sub => sub.remove());
return res;
})
};
},
uploadFiles(options: UploadFileOptions): Promise<UploadResult> {
uploadFiles(options: UploadFileOptions): { jobId: number, promise: Promise<UploadResult> } {
if (!RNFSManager.uploadFiles) {
return {
jobId: -1,
promise: Promise.reject(new Error('`uploadFiles` is unsupported on this platform'))
};
}
var jobId = getJobId();
var subscriptions = [];
@ -354,10 +352,13 @@ var RNFS = {
method: options.method || 'POST'
};
return RNFSManager.uploadFiles(bridgeOptions).then(res => {
subscriptions.forEach(sub => sub.remove());
return res;
});
return {
jobId,
promise: RNFSManager.uploadFiles(bridgeOptions).then(res => {
subscriptions.forEach(sub => sub.remove());
return res;
})
};
},
MainBundlePath: RNFSManager.RNFSMainBundlePath,

View File

@ -7,8 +7,9 @@ Native filesystem access for react-native
- Removed attributes from `writeFile` and `appendFile` for iOS / Android consistency
- `downloadFile` takes `options` object rather than parameters
- `stopDownload` will cause the rejection of promise returned by `downloadFile`
- `uploadFile` promise result `response` property is now `body`
- `uploadFiles` promise result `response` property is now `body`
- A boolean is no longer returned from any method except `exists`
- `downloadFile` and `uploadFiles` return an object of the form `{ jobId: number, promise: Promise }`
## Usage (iOS)
@ -242,7 +243,7 @@ RNFS.uploadFiles({
},
begin: uploadBegin,
progress: uploadProgress
}).then((response) => {
}).promise.then((response) => {
if (response.statusCode == 200) {
console.log('FILES UPLOADED!'); // response.statusCode, response.headers, response.body
} else {
@ -345,7 +346,7 @@ Create a directory at `filepath`. Automatically creates parents and does not thr
(IOS only): If `excludeFromBackup` is true, then `NSURLIsExcludedFromBackupKey` attribute will be set. Apple will *reject* apps for storing offline cache data that does not have this attribute.
### `downloadFile(options: DownloadFileOptions): Promise<DownloadResult>`
### `downloadFile(options: DownloadFileOptions): { jobId: number, promise: Promise<DownloadResult> }`
```
type DownloadFileOptions = {
@ -405,7 +406,7 @@ If `progressDivider` = 0, you will receive all `progressCallback` calls, default
Abort the current download job with this ID. The partial file will remain on the filesystem.
### (iOS only) `uploadFiles(options: UploadFileOptions): Promise<UploadResult>`
### (iOS only) `uploadFiles(options: UploadFileOptions): { jobId: number, promise: Promise<UploadResult> }`
`options` (`Object`) - An object containing named parameters