Aucune description

Fetch.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. constructor(config:RNFetchBlobConfig) {
  9. Object.assign(this, new RNFetchBlobFetchPolyfill(config))
  10. }
  11. }
  12. class RNFetchBlobFetchPolyfill {
  13. constructor(config:RNFetchBlobConfig) {
  14. this.build = () => (url, options) => {
  15. options.headers = options.headers || {}
  16. options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
  17. options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
  18. return RNFetchBlob.config(config)
  19. .fetch(options.method, url, options.headers, options.body)
  20. .then((resp) => {
  21. let info = resp.info()
  22. return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
  23. })
  24. }
  25. }
  26. }
  27. class RNFetchBlobFetchRepsonse {
  28. constructor(resp:FetchBlobResponse) {
  29. let info = resp.info()
  30. this.headers = info.headers
  31. this.ok = info.status >= 200 && info.status <= 299,
  32. this.status = info.status
  33. this.type = 'basic'
  34. this.bodyUsed = false
  35. this.resp = resp
  36. this.rnfbRespInfo = info
  37. this.rnfbResp = resp
  38. }
  39. arrayBuffer(){
  40. log.verbose('to arrayBuffer', this.rnfbRespInfo)
  41. return readArrayBuffer(this.rnfbResp, this.rnfbRespInfo)
  42. }
  43. text() {
  44. log.verbose('to text', this.rnfbResp, this.rnfbRespInfo)
  45. return readText(this.rnfbResp, this.rnfbRespInfo)
  46. }
  47. json() {
  48. log.verbose('to json', this.rnfbResp, this.rnfbRespInfo)
  49. return readJSON(this.rnfbResp, this.rnfbRespInfo)
  50. }
  51. formData() {
  52. log.verbose('to formData', this.rnfbResp, this.rnfbRespInfo)
  53. return readFormData(this.rnfbResp, this.rnfbRespInfo)
  54. }
  55. blob() {
  56. log.verbose('to blob', this.rnfbResp, this.rnfbRespInfo)
  57. return readBlob(this.rnfbResp, this.rnfbRespInfo)
  58. }
  59. }
  60. function readText(resp, info):Promise<string> {
  61. switch (info.rnfbEncode) {
  62. case 'base64':
  63. return Promise.resolve(resp.text())
  64. break
  65. case 'path':
  66. return resp.readFile('utf8').then((data) => {
  67. data = unicode(data)
  68. return Promise.resolve(data)
  69. })
  70. break
  71. default:
  72. return Promise.resolve(resp.text())
  73. break
  74. }
  75. }
  76. function readJSON(resp, info):Promise<object> {
  77. switch (info.rnfbEncode) {
  78. case 'base64':
  79. return Promise.resolve(resp.json())
  80. case 'path':
  81. return resp.readFile('utf8').then((data) => {
  82. return Promise.resolve(JSON.parse(data))
  83. })
  84. default:
  85. return Promise.resolve(JSON.parse(resp.data))
  86. }
  87. }