微信小程序官方 API 的 wrapper,

网友投稿 763 2022-10-09

微信小程序官方 API 的 wrapper,

微信小程序官方 API 的 wrapper,

weapp-next

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

Features

Promisified async API Shortcuts for wx.request API Enhancements official APIs RESTful http client

Install

# via Githubnpm i xixilive/weapp-next --save-dev

# via npmnpm 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/awaitasync function postLogin(){ const {code} = await weapp.login() const {errMsg, ...userInfo} = await weapp.getUserInfo({withCredentials: true}) return await client.post('/login', {data: {...userInfo, code}})}//promisefunction 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 Objectconst {request, Http} = weapp(wx)// use request APIrequest({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 clientconst http = Http('https://server.com/api')http.get('/path').then(response => console.log)// orconst 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 functionconst 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=1http.post('/status', {data: {}})

Changes

Change log

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Breast Cancer Wisconsin (Diagnostic) Data Set(威斯康星州乳腺癌(诊断)数据集)
下一篇:MySQL总结
相关文章

 发表评论

暂时没有评论,来抢沙发吧~