Нет описания

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import RNFetchBlob from '../index.js'
  2. import Log from '../utils/log.js'
  3. import fs from '../fs'
  4. import unicode from '../utils/unicode'
  5. const log = new Log('FetchPolyfill')
  6. log.level(3)
  7. export default class Fetch {
  8. provider:RNFetchBlobFetch;
  9. constructor(config:RNFetchBlobConfig) {
  10. this.provider = new RNFetchBlobFetch(config)
  11. }
  12. }
  13. class RNFetchBlobFetch {
  14. constructor(config:RNFetchBlobConfig) {
  15. this._fetch = (url, options) => {
  16. let bodyUsed = false
  17. options.headers = options.headers || {}
  18. options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
  19. options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
  20. return RNFetchBlob.config(config)
  21. .fetch(options.method, url, options.headers, options.body)
  22. .then((resp) => {
  23. let info = resp.info()
  24. return Promise.resolve({
  25. headers : info.headers,
  26. ok : info.status >= 200 && info.status <= 299,
  27. status : info.status,
  28. type : 'basic',
  29. bodyUsed,
  30. arrayBuffer : () => {
  31. log.verbose('to arrayBuffer', info)
  32. },
  33. text : () => {
  34. log.verbose('to text', resp, info)
  35. switch (info.rnfbEncode) {
  36. case 'base64':
  37. let result = unicode(resp.text())
  38. return Promise.resolve(result)
  39. break
  40. case 'path':
  41. return resp.readFile('utf8').then((data) => {
  42. data = unicode(data)
  43. return Promise.resolve(data)
  44. })
  45. break
  46. case '':
  47. default:
  48. return Promise.resolve(resp.data)
  49. break
  50. }
  51. },
  52. json : () => {
  53. log.verbose('to json', resp, info)
  54. switch (info.rnfbEncode) {
  55. case 'base64':
  56. return Promise.resolve(resp.json())
  57. case 'path':
  58. return resp.readFile('utf8').then((data) => {
  59. return Promise.resolve(JSON.parse(data))
  60. })
  61. case '':
  62. default:
  63. return Promise.resolve(JSON.parse(resp.data))
  64. }
  65. },
  66. formData : () => {
  67. log.verbose('to formData', resp, info)
  68. }
  69. })
  70. })
  71. }
  72. }
  73. get fetch() {
  74. return this._fetch
  75. }
  76. }