All Projects → afishhhhh → weapp.request

afishhhhh / weapp.request

Licence: MIT License
为微信小程序提供的网络请求组件,是 wx.request 的扩展,基于 Promise API,添加缓存控制

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to weapp.request

wxapp-computed
在微信小程序中使计算值(computed)
Stars: ✭ 20 (-31.03%)
Mutual labels:  weapp, wxapp, miniprogram
mobx-wxapp
在小程序中使用mobx
Stars: ✭ 54 (+86.21%)
Mutual labels:  weapp, wxapp, miniprogram
Wux Weapp
🐶 一套组件化、可复用、易扩展的微信小程序 UI 组件库
Stars: ✭ 4,706 (+16127.59%)
Mutual labels:  weapp, wxapp, miniprogram
Wxapp Webpack Plugin
📦 微信小程序 webpack 插件
Stars: ✭ 185 (+537.93%)
Mutual labels:  weapp, wxapp
weapp-template
🚀一个简单实用的微信小程序基础配置模板
Stars: ✭ 112 (+286.21%)
Mutual labels:  weapp, miniprogram
Dva Wxapp
微信小程序的dva集成
Stars: ✭ 183 (+531.03%)
Mutual labels:  weapp, wxapp
Cax
HTML5 Canvas 2D Rendering Engine - 小程序、小游戏以及 Web 通用 Canvas 渲染引擎
Stars: ✭ 1,864 (+6327.59%)
Mutual labels:  weapp, miniprogram
Wechat web devtools
微信开发者工具(微信小程序)linux完美支持
Stars: ✭ 2,664 (+9086.21%)
Mutual labels:  weapp, wxapp
Wxa Plugin Canvas
小程序海报组件-生成朋友圈分享海报并生成图片
Stars: ✭ 2,692 (+9182.76%)
Mutual labels:  weapp, wxapp
wxapp-boilerplate
微信小程序开发脚手架 (ES6, Redux, Immutable-js, Async/await, Promise, Reselect, Babel, ESLint, Stylelint, Gulp ... )
Stars: ✭ 35 (+20.69%)
Mutual labels:  weapp, wxapp
weapp-OpenRadio
A base music weapp named OpenRadio for wechat. Can use on weapp getting started.
Stars: ✭ 14 (-51.72%)
Mutual labels:  weapp, wxapp
miniprogram-picker
微信小程序自定义组件Picker。本组件对微信小程序原生Picker组件进行了二次封装,开发者只需要提供固定数据结构的sourceData,再进行一些必要配置,本组件就可以自动帮助开发者处理联动逻辑。
Stars: ✭ 30 (+3.45%)
Mutual labels:  weapp, miniprogram
Omi
Front End Cross-Frameworks Framework - 前端跨框架跨平台框架
Stars: ✭ 12,153 (+41806.9%)
Mutual labels:  weapp, miniprogram
Leshare Shop Weapp
基于微信小程序的电商平台,采用原生框架开发
Stars: ✭ 183 (+531.03%)
Mutual labels:  weapp, wxapp
We Cropper
微信小程序图片裁剪工具
Stars: ✭ 1,972 (+6700%)
Mutual labels:  weapp, wxapp
Weapp
🐧 微信小程序组件和功能封装,基于微信Component自定义组件开发
Stars: ✭ 235 (+710.34%)
Mutual labels:  weapp, wxapp
quickstart-miniprogram
🎉微信小程序webpack模板
Stars: ✭ 32 (+10.34%)
Mutual labels:  wxapp, miniprogram
mpapi
🐤 小程序API兼容插件,一次编写,多端运行。支持:微信小程序、支付宝小程序、百度智能小程序、字节跳动小程序
Stars: ✭ 40 (+37.93%)
Mutual labels:  weapp, miniprogram
Weapp Qrcode Base64
微信小程序生成二维码的插件,基于base64编码输出二维码,不依赖canvas
Stars: ✭ 100 (+244.83%)
Mutual labels:  weapp, wxapp
Wx Book
仿追书神器的小说阅读器小程序
Stars: ✭ 122 (+320.69%)
Mutual labels:  weapp, wxapp

weapp.request

一个为微信小程序提供的,基于 wx.request 扩展的网络请求组件库。

Features

  1. Promise API
  2. 缓存控制
  3. 请求/响应拦截器

Install

推荐使用 npm 安装
npm install weapp.request -S

Quick Start

引入 weapp.request
const request = require('weapp.request')
  1. 发送一个 GET 请求

    request('https://api.github.com').then(onFulfilled).catch(onRejected)

    因为所有的 request 调用都会返回一个 Promise,所以可以使用 then 对请求结果进行进一步处理,用 catch 来捕获内部抛出的错误。

  2. 发送一个 GET 请求,并写入缓存

    request('https://api.github.com', {}, {
      cache: true
    })
  3. 发送一个 POST 请求

    request.post('https://api.github.com', {
      user: 'afishhhhh'
    })

    除了 GET 请求以外,所有其他的 method 都要以 request.method 的形式进行调用。

    根据微信官方文档的说明,以上 POST 方法且 Content-Type 默认为 application/json,会对数据进行 JSON 序列化。

    如果需要以 query string 的形式将数据发送给服务器,可以采取以下调用方法,不需要显示的将 Content-Type 写为 application/x-www-form-urlencoded

    request.post('https://api.github.com', {
      form: {
        user: 'afishhhhh'
      }
    })
  4. 全局配置

    配置选项 类型 说明 必填 默认值
    baseUrl String/Undefined 基础请求路径
    cacheMaxAge Number/Undefined 缓存有效期,时间单位为秒 1800
    validStatusCode Function/Undefined status code 合法区间,该函数接受一个参数,并返回一个 Boolean code => code >= 200 && code < 300
    request.config({
      baseUrl: 'https://api.github.com'
    })
  5. 请求/响应拦截器

    // 添加请求拦截器
    request.interceptors.req.use(function (request) {
      request.header['X-Tag'] = 'weapp.request'
      // return request 可以显式地返回一个 request,如果没有 return,则默认返回当前 request
    })
    

    响应拦截器同理。

    // 移除请求拦截器
    request.interceptors.req.remove()
    

APIs

request(url, params, options)

发起一个 GET 请求。

params:请求参数,类型为 Object,非必填。

options:配置项,类型为 Object,非必填,可以有以下属性值:

属性 类型 必填 默认值 说明
cache Boolean/Undefined undefined undefined 表示从服务器获取最新数据,不写入缓存;true 表示优先从缓存中获取数据,如果缓存中不存在该数据或者缓存已失效,则从服务器获取数据,并写入缓存;false 表示优先从服务器获取数据,并将数据写入缓存
header 同微信官方文档
dataType 同微信官方文档
responseType 同微信官方文档
request.method(url, params, options)

method 可以是 getpost 等等。

request.config(options)

options:配置项,类型为 Object

License

This code is distributed under the terms and conditions of the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].