4 Star 2 Fork 0

Gitee 极速下载 / weapp-wrapper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/xixilive/weapp
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

weapp-next

Weapp(微信小程序) official API wrapper, purpose to expose fashionable, friendly and fluent programming API.

Build Status

weapp-screen-shot

Features

  • Promisified async API

  • Shortcuts for wx.request API

  • Enhancements official APIs

  • RESTful http client

Install

# via Github
npm i xixilive/weapp-next --save-dev
# via npm
npm i weapp-next --save-dev

Examples

non-callback and RESTful HTTP

const weapp = require('weapp-next')(wx)
const client = weapp.Http('https://api.server.com/')

//async/await
async function postLogin(){
  const {code} = await weapp.login()
  const {errMsg, ...userInfo} = await weapp.getUserInfo({withCredentials: true})
  return await client.post('/login', {data: {...userInfo, code}})
}

//promise
function postLogin(){
  const getUserInfo = code => opts => {
    return weapp.getUserInfo(opts).then(({errMsg, ...userInfo}) => ({userInfo, code})
  }

  const postRequest = data => client.post('/login', {data})
  return weapp.login().then(getUserInfo).then(postRequest)
}

Usage

weapp-next use UMD module system, you can load it in Commonjs or AMD format.

import weapp from 'weapp-next'

// get wrapped wx Object
const {request, Http} = weapp(wx)

// use request API
request({url: 'https://test.com', method: 'GET'}).then(response => console.log)

// use shortcuts of request API, such as get, post, put, etc.
request.get('https://test.com').then(response => console.log)

// use Http client
const http = Http('https://server.com/api')
http.get('/path').then(response => console.log)

// or
const weapp = require('weapp-next')(wx)

Wraps almost all of official APIs, see Wrapped methods

Enhancements

Create http request shortcuts according to wechat mini-program declared verbs(RFC 2616). specially, the PATCH verb maybe useful for strict RESTful-ist, and so it has defined also.

weapp.request

It resolves a response which take a statusCode in range of [200, 300), and reject a response which out of the range.

The resolved response and rejected reason/error is the response object from the native wx.request.

import weapp from 'weapp-next'
const {request} = weapp(wx)

request({url, method: 'GET'})
  .then(response => {
    // response is the response object from wx.request
  })
  .catch(error => {
    // error is the response object from wx.request
  })

request.get(url:String [, init:Function])
request.post(url:String, body:String/Object, [, init:Function])
request.put(url:String, body:String/Object, [, init:Function])
request.patch(url:String, body:String/Object, [, init:Function])
request.delete(url:String [, init:Function])
request.head(url:String [, init:Function])
request.options(url:String [, init:Function])
request.trace(url:String [, init:Function])
request.connect(url:String [, init:Function])

Optional init argument is a zero-arugments function to interpolate request parameters, and it expects to return an Object value by contract. you can override any request params by the returned object except the url and method.

// logic of init function
const config = {...}
return {...config, ...init(), url, method}

weapp.requireAuth (DPRECIATED)

DPRECIATED

Here is a Express middleware for weapp login scenario which purpose to make it easy to integrate weapp login and getUserInfo logic. express-weapp-auth

Use RESTful Http client

import weapp from 'weapp-next'

const http = weapp(wx).Http('https://api.server.com/')
http.get('/status', {version: '1'}) // /status?version=1
http.post('/status', {data: {}})

Changes

Change log

Copyright (c) 2016-2017 xixilive@gmail.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/mirrors/weapp-wrapper.git
git@gitee.com:mirrors/weapp-wrapper.git
mirrors
weapp-wrapper
weapp-wrapper
master

搜索帮助

14c37bed 8189591 565d56ea 8189591