reco-fetch
Fetch for browser.
Install
$ npm isntall reco-fetch
Including reco-fetch
Script tag
Import
import recoFetch from 'reco-fetch'
Config
In addition to the parameters given below, please combine other parameters MDN.
/** * @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
Example
GET
const options = method: 'get' params: key: 1 value: 2
POST JSON
const options = method: 'post' body: key: 1 value: 2 type: 'json'
POST formData
const options = method: 'post' body: key: 1 value: 2 type: 'formData' // or const form = documentconst options = method: 'post' body: form
PUT
const options = method: 'put' body: key: 1 value: 2 type: 'json' // or const options = method: 'put' body: JSON
DELETE
const options = method: 'delete' params: key: 1 value: 2
uploadFile
const fileField = document const options = method: 'post' body: file: fileFieldfiles0 type: 'formData' // or const formData = const fileField = document formData const options = method: 'post' body: formData