reco-fetch

reco_luan2018-09-13frontEndnpmfetchvuerect

TIP

封装的主要内容

  1. fetch 的请求方式同 $ajaxaxios 都不太一样,并且它本身的get请求同其他请求对数据的处理和herder也不太相同,所以为了统一请求行为,方便请求过程,将请求过程进行封装;
  2. fetch 请求的结果均返回到.then()中,但是平时的习惯是是在 .then() 中处理正确结果,.catch() 中处理错误,所以对请求结果进行统一处理;
  3. fetch 本身没有 请求超时 这个概念,所以通过 Promise.race 来处理,它的作用是多个promise同时运行,返回的结果以最快返回结果的那个promise的值为准。

Fetch for browser.

Install

$ npm isntall reco-fetch

Including reco-fetch

Script tag

<script src="/node_modules/reco-fetch/dist/recoFetch.min.js"></script>

Import

import recoFetch from 'reco-fetch'

Config

In addition to the parameters given below, please combine other parameters MDNopen in new window.

/**
 * @param {String, must} url    API URL
 * @param {String, must} options Parameter objects, including:
 *        method  {String, optional} Request method, default 'get'
 *        headers {Object, optional} Set request header
 *        params  {Object, optional} url parameters
 *        body    {Object, optional} request body
 *        timeout {Number, optional} Request timeout
 *        type    {String, optional} When 'post' requests, you can set: 'json', 'formData'
 */

const options = {
  method: 'post',
  headers: {},
  timeout: 1000,
  body: {
    id: 1,
    value: 2
  }
}

recoFetch(url, options). then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
})

Example

GET

const options = {
  method: 'get',
  params: {
    key: 1,
    value: 2
  }
}

recoFetch(url, options). then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
})

POST JSON

const options = {
  method: 'post',
  body: {
    key: 1,
    value: 2
  },
  type: 'json'
}

recoFetch(url, options). then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
})

POST formData

const options = {
  method: 'post',
  body: {
    key: 1,
    value: 2
  },
  type: 'formData'
}

// or

const form = document.querySelector('form')
const options = {
  method: 'post',
  body: form
}

recoFetch(url, options). then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
})

PUT

const options = {
  method: 'put',
  body: {
    key: 1,
    value: 2
  },
  type: 'json'
}

// or

const options = {
  method: 'put',
  body: JSON.stringify({
    key: 1,
    value: 2
  })
}

recoFetch(url, options). then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
})

DELETE

const options = {
  method: 'delete',
  params: {
    key: 1,
    value: 2
  }
}

recoFetch(url, options). then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
})

uploadFile

const fileField = document.querySelector("input[type='file']")

const options = {
  method: 'post',
  body: {
    file: fileField.files[0]
  },
  type: 'formData'
}

// or

const formData = new FormData()
const fileField = document.querySelector("input[type='file']")

formData.append('file', fileField.files[0])

const options = {
  method: 'post',
  body: formData
}

recoFetch(url, options). then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
})

License

MITopen in new window

如果觉得还可以,欢迎给个 Staropen in new window

个人博客:午后南杂open in new window

最后更新时间 2020/5/30 00:30:10