설명 없음

index.js 2.7KB

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