Нема описа

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @author wkh237
  3. * @version 0.3.3
  4. */
  5. import { NativeModules } from 'react-native'
  6. import { DeviceEventEmitter } from 'react-native';
  7. import base64 from 'base-64'
  8. const RNFetchBlob = NativeModules.RNFetchBlob
  9. // Show warning if native module not detected
  10. if(RNFetchBlob === void 0) {
  11. console.warn(
  12. 'react-native-fetch-blob could not find native module.',
  13. 'please make sure you have linked native modules using `rnpm link`,',
  14. 'and restart RN packager or manually compile IOS/Android project.'
  15. )
  16. }
  17. // Promise wrapper function
  18. const fetch = (...args) => {
  19. // create task ID for receiving progress event
  20. let taskId = getUUID()
  21. let promise = new Promise((resolve, reject) => {
  22. let [method, url, headers, body] = [...args]
  23. let nativeMethodName = Array.isArray(body) ? 'fetchBlobForm' : 'fetchBlob'
  24. let progressEventHandler = (e) => {
  25. if(e.taskId === taskId && promise.onProgress) {
  26. promise.onProgress(e.written, e.total)
  27. }
  28. }
  29. DeviceEventEmitter.addListener('RNFetchBlobProgress' + taskId, progressEventHandler)
  30. RNFetchBlob[nativeMethodName](taskId, method, url, headers || {}, body, (err, ...data) => {
  31. // task done, remove event listener
  32. DeviceEventEmitter.removeAllListeners('RNFetchBlobProgress'+taskId)
  33. if(err)
  34. reject(new Error(err, ...data))
  35. else
  36. resolve(new FetchBlobResponse(...data))
  37. })
  38. })
  39. promise.onProgress = null
  40. return promise
  41. }
  42. /**
  43. * RNFetchBlob response object class.
  44. */
  45. class FetchBlobResponse {
  46. constructor(data) {
  47. this.data = data
  48. /**
  49. * Convert result to javascript Blob object.
  50. * @param {string} contentType MIME type of the blob object.
  51. * @param {number} sliceSize Slice size.
  52. * @return {blob} Return Blob object.
  53. */
  54. this.blob = (contentType, sliceSize) => {
  55. console.warn('FetchBlobResponse.blob() is deprecated and has no funtionality.')
  56. return null
  57. }
  58. /**
  59. * Convert result to text.
  60. * @return {string} Decoded base64 string.
  61. */
  62. this.text = () => {
  63. return base64.decode(this.data)
  64. }
  65. /**
  66. * Convert result to JSON object.
  67. * @return {object} Parsed javascript object.
  68. */
  69. this.json = () => {
  70. return JSON.parse(base64.decode(this.data))
  71. }
  72. /**
  73. * Return BASE64 string directly.
  74. * @return {string} BASE64 string of response body.
  75. */
  76. this.base64 = () => {
  77. return this.data
  78. }
  79. }
  80. }
  81. function getUUID(){
  82. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  83. let r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
  84. return v.toString(16);
  85. });
  86. }
  87. export default {
  88. fetch, FetchBlobResponse, base64
  89. }