Promise based wx.request api for weChat Mini Program,微信小程序请求API 扩展,基于promise设计
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Mini-Program
server
wefetch
.gitignore
README-zh_CN.md
README.MD
package-lock.json
package.json

README.MD

English | 简体中文

wx.request Object

Promise based wx.request api for weChat Mini Program.supported Wechat Mini Program and Mini game.

Feature

  easy to use, Promise based wx.request api for designed
  supported Reqeust && Response InterceptorsManagement

donwload

  git clone https://github.com/jonnyshao/wechat-fetch.git
  cd server
  npm i 
  npm start  // localhost:3000
  open the Mini-Program directory in devtools 

devtools

install (npm build)

npm i wefetch -S

usage for wechat Mini Program

const wf = require('wefetch')

exmaple

// get for request 
  wf.get(url).then(res => {
    // ....
  })

  // you project domain 
  wf.defaults.baseUrl = 'youdomain'
  
  // interceptor request
  wf.before.use(function (request) {
    // do something ...
    return request;
  })

  // interceptor response
  wf.after.use(function (response) {
    // do something ...
    return response;
  })

	// post for reuqest, the default header 'Content-Type' dose't is application/json,if you need JSON format,use the wf.postJson function,please
  wf.post(url, {data: data}, {header: {token: xxx},responseType: 'arraybuffer', dataType: xx}).then(res =>{
    // ...
  }).catch((err => {
    // ...
  }))
  
  // abort the current request, return one Promise request.
  var p = wf.get(url).then(res => {

  })
  // execute abort
  p.task.abort()
  // complete
  wf.get(url).then(() => {

  }).catch(err => {

  }).finally(() => {
    // request complete...
  })

API

  wf.get(url, params, config)
  wf.post(url, params, config) // application/x-www-form-urlencoded;charset=utf-8
  wf.head(url, params, config)
  wf.put(url, params, config)
  wf.get(url, params, config)
  wf.trace(url, params, config)
  wf.delete(url, params, config)
  wf.postJson(url, params, config) //application/json;charset=utf-8

abort Request

var p = wf.get(url)
p.task.abort()

RequestTaks

defaults params

  defaults: {
    baseUrl: '',
    createRequest: wx.request 
    header: {},
    data: {},
    dataType: 'json',  // wechat default params
    responseType: 'text' // wechat default params
  }