react-native-fetch-blob
Advanced tools
Comparing version 0.3.2 to 0.4.0
88
index.js
@@ -1,28 +0,41 @@ | ||
import { NativeModules } from 'react-native'; | ||
/** | ||
* @author wkh237 | ||
* @version 0.3.3 | ||
*/ | ||
import { NativeModules } from 'react-native' | ||
import base64 from 'base-64' | ||
const RNFetchBlob = NativeModules.RNFetchBlob | ||
// Show warning if native module not detected | ||
if(RNFetchBlob === void 0) { | ||
console.warn( | ||
'react-native-fetch-blob could not find native module.', | ||
'please make sure you have linked native modules using `rnpm link`,', | ||
'and restart RN packager or manually compile IOS/Android project.' | ||
) | ||
} | ||
// Promise wrapper function | ||
const fetch = (...args) => new Promise((resolve, reject) => { | ||
const fetch = (...args) => { | ||
let [method, url, headers, body] = [...args] | ||
let promise = new Promise((resolve, reject) => { | ||
if(Array.isArray(body)) | ||
RNFetchBlob.fetchBlobForm(method, url, headers, body, (err, data) => { | ||
let [method, url, headers, body] = [...args] | ||
let nativeMethodName = Array.isArray(body) ? 'fetchBlobForm' : 'fetchBlob' | ||
RNFetchBlob[nativeMethodName](method, url, headers, body, (err, ...data) => { | ||
if(err) | ||
reject(new Error(err, data)) | ||
reject(new Error(err, ...data)) | ||
else | ||
resolve(new FetchBlobResponse(data)) | ||
resolve(new FetchBlobResponse(...data)) | ||
}) | ||
else | ||
RNFetchBlob.fetchBlob(method, url, headers, body, (err, data) => { | ||
if(err) | ||
reject(new Error(err, data)) | ||
else | ||
resolve(new FetchBlobResponse(data)) | ||
}) | ||
}) | ||
}) | ||
return promise | ||
} | ||
/** | ||
@@ -42,3 +55,4 @@ * RNFetchBlob response object class. | ||
this.blob = (contentType, sliceSize) => { | ||
return b64toBlob(this.data, contentType, sliceSize) | ||
console.warn('FetchBlobResponse.blob() is deprecated and has no funtionality.') | ||
return null | ||
} | ||
@@ -50,3 +64,3 @@ /** | ||
this.text = () => { | ||
return atob(this.data) | ||
return base64.decode(this.data) | ||
} | ||
@@ -58,4 +72,8 @@ /** | ||
this.json = () => { | ||
return JSON.parse(atob(this.data)) | ||
return JSON.parse(base64.decode(this.data)) | ||
} | ||
/** | ||
* Return BASE64 string directly. | ||
* @return {string} BASE64 string of response body. | ||
*/ | ||
this.base64 = () => { | ||
@@ -69,36 +87,4 @@ return this.data | ||
/** | ||
* Convert base64 string to blob, source : StackOverflow | ||
* {@link http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript} | ||
* @param {string} b64Data Base64 string of data. | ||
* @param {string} contentType MIME type of data. | ||
* @param {number} sliceSize Slice size, default to 512. | ||
* @return {blob} Return Blob object. | ||
*/ | ||
function b64toBlob(b64Data, contentType, sliceSize) { | ||
contentType = contentType || ''; | ||
sliceSize = sliceSize || 512; | ||
var byteCharacters = atob(b64Data); | ||
var byteArrays = []; | ||
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) { | ||
var slice = byteCharacters.slice(offset, offset + sliceSize); | ||
var byteNumbers = new Array(slice.length); | ||
for (var i = 0; i < slice.length; i++) { | ||
byteNumbers[i] = slice.charCodeAt(i); | ||
} | ||
var byteArray = new Uint8Array(byteNumbers); | ||
byteArrays.push(byteArray); | ||
} | ||
var blob = new Blob(byteArrays, {type: contentType}); | ||
return blob; | ||
} | ||
export default { | ||
fetch, FetchBlobResponse | ||
fetch, FetchBlobResponse, base64 | ||
} |
{ | ||
"name": "react-native-fetch-blob", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "A react-native plugin for fetch blob data via HTTP.", | ||
@@ -9,6 +9,10 @@ "main": "index.js", | ||
}, | ||
"dependencies": { | ||
"base-64": "0.1.0" | ||
}, | ||
"keywords": [ | ||
"react-native", | ||
"fetch", | ||
"blob" | ||
"blob", | ||
"image header" | ||
], | ||
@@ -18,4 +22,4 @@ "repository": { | ||
}, | ||
"author": "suzuri04x2", | ||
"author": "wkh237", | ||
"license": "MIT" | ||
} |
@@ -5,7 +5,7 @@ # react-native-fetch-blob [![npm version](https://badge.fury.io/js/react-native-fetch-blob.svg)](https://badge.fury.io/js/react-native-fetch-blob) | ||
If you're dealing with image or file server that requires an `Authorization` token in the header, or you're having problem with `fetch` API when receiving blob data, you might try this module (this is also the reason why I made this plugin). | ||
If you're dealing with image or file server that requires an `Authorization` token in the header, or you're having problem with `fetch` API when receiving blob data, you might try this module (this is also the reason why I made this). | ||
See [[fetch] Does fetch with blob() marshal data across the bridge?]([fetch] Does fetch with blob() marshal data across the bridge?). | ||
See [[fetch] Does fetch with blob() marshal data across the bridge?](https://github.com/facebook/react-native/issues/854). | ||
This module enables you upload/download binary data in js, see [Examples](#user-content-usage) bellow. | ||
This module enables you upload/download binary data in js, see [Examples](#user-content-usage) bellow. | ||
@@ -131,8 +131,15 @@ The source code is very simple, just an implementation of native HTTP request, supports both Android (uses awesome native library [AsyncHttpClient](https://github.com/AsyncHttpClient/async-http-client])) and IOS. | ||
#### body:`string | Array<Object>` (Optional) | ||
Body of the HTTP request, body can either be a BASE64 string, or an array contains object elements, each element should use the following format. | ||
Body of the HTTP request, body can either be a BASE64 string, or an array contains object elements, each element have 2 required property `name`, and `data`, and 1 optional property `filename`, once `filename` is set, content in `data` property will be consider as BASE64 string that will be converted into byte array later. | ||
When body is a base64 string , this string will be converted into byte array in native code, and the request body will be sent as `application/octet-stream`. | ||
#### `base64` | ||
When body is a base64 string , this string will be converted into byte array in native code, and the request body will be sent as `application/octet-stream`. | ||
A helper object simply uses [base-64](https://github.com/mathiasbynens/base64) for decode and encode BASE64 data. | ||
```js | ||
RNFetchBlob.base64.encode(data) | ||
RNFetchBlob.base64.decode(data) | ||
``` | ||
### FetchBlobResponse | ||
@@ -148,5 +155,4 @@ | ||
returns decoded base64 string (done in js context) | ||
#### blob():Blob | ||
returns Blob object (one in js context) | ||
### TODO | ||
@@ -156,2 +162,1 @@ | ||
* Custom MIME type in form data | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
105060
28
159
1
1
283
+ Addedbase-64@0.1.0
+ Addedbase-64@0.1.0(transitive)