微信小程序的http模块,机智得“绕过”最大5个http并发的限制.
npm install wxapp-http
TODO:
1.0.0正式版本将基于@axetroy/event-emitter.js 事件管理将实现真正的发布/订阅者模式
// es6
import http from 'wxapp-http';
// commonJS
const http = require('wxapp-http').default;
http.get('https://www.google.com')
.then(function(response){
})
.catch(function(error){
console.error(error);
});
Response返回的结构体
interface Response${
data: any,
errMsg: string,
header: Object,
statusCode: number
}
Http.prototype.request = function(method:string, url:string, body?:Object | string="", headers?: Object={}, dataType?: String="json"): Promise<Response$>{
}
Http.prototype.get = function(url:string, body?:Object | string="", headers?: Object={}, dataType?: String="json"): Promise<Response$>{
}
Http.prototype.post = function(url:string, body?:Object | string="", headers?: Object={}, dataType?: String="json"): Promise<Response$>{
}
...
配置文件字段
interface Config${
method: string,
url: string,
data: Object | string,
header: Object,
dataType: string
}
返回布尔值,如果为true,则允许发送请求,如果为false,则拒绝发送请求,并且返回的promise进入reject阶段
Http.prototype.requestInterceptor = function(func:(config: Config$)=> boolean): void{
}
http.requestInterceptor(function(config){
// 只允许发送https请求
if(config.url.indexOf('https')===0){
return true;
}else{
return false;
}
});
返回布尔值,如果为true,则返回的promise进入resolve阶段,如果为false,则进入reject阶段
Http.prototype.responseInterceptor = function(func:(config: Config$, response: Response$)=> boolean): void{
}
http.responseInterceptor(function(config, response){
// 如果服务器返回null,则进入reject
if(response && response.data!==null){
return true;
}else{
return false;
}
});
监听全局的http请求
Http.prototype.onRequest = function(func:(config: Config$)=> void): void{
}
http.onRequest(function(config){
console.log(`will send http request: `, config.url);
});
Http.prototype.onSuccess = function(func:(config: Config$, response: Response$)=> void): void{
}
http.onSuccess(function(config, response){
console.log(`http request done: `, config.url);
});
Http.prototype.onFail = function(func:(config: Config$, response: Response$)=> void): void{
}
http.onFail(function(config, response){
console.log(`http request fail: `, config.url);
});
Http.prototype.onComplete = function(func:(config: Config$, response: Response$)=> void): void{
}
http.onComplete(function(config, response){
console.log(`http request complete: `, config.url);
});
Http.prototype.onError = function(func:(error: Error)=> void): void{
}
http.onError(function(error){
console.error(error);
});
requestInterceptor
↓
onRequest
↙ ↘
onSuccess onFail
↘ ↙
responseInterceptor
↓
onComplete
(onError run in hole life circle)
适用于小程序页面切换后,取消掉未发出去的http请求.
Http.prototype.clean = function() : void{
}
http.clean();
最高并发数量默认为5个
import {Http} from 'wxapp-http';
const http = new Http(3); // 设置最高并发3个
http.get('https://www.google.com')
.then(function(response){
})
.catch(function(error){
console.error(error);
});
git clone https://github.com/axetroy/wxapp-http.git
cd ./wxapp-http
yarn
yarn run start
You can flow Contribute Guide
Axetroy💻 🔌 ⚠️ 🐛 🎨 |
---|
The MIT License