cnfapi-miniprogram
author: bugszhou | Email:bugszhou@outlook.com
description: A lib project with ziu
cnfapi-miniprogram是基于promise的小程序http库,可以用在微信小程序,支付宝小程序和百度小程序
Features
- 支持 promise API
- 拦截请求和响应
- 自动转换 JSON 数据
安装
npm install -S cnfapi-miniprogram
Example
import Api from 'cnfapi-miniprogram';
const api = new Api({
baseURL: 'https://prj1.demo.com',
env: 'weapp',
timeout: 10000,
headers: {
'Content-Type': 'application/json',
},
resSuccessCallback(data, next) {
if (data.code === 200) {
next(null, data.data, data.code);
} else {
next({
msg: data.msg,
retcode: data.code,
}, {}, data.code);
}
},
openResInterceptor(res) {
return true;
},
resInterceptor(next) {
next();
},
}, {
getList: {
interval: 2000,
retryTimes: 10,
apiName: '/test/prj/getList',
desc: '',
method: 'POST',
params: {
post: [{
param: 'param1',
isNeed: 1,
}],
get: [{
param: 'param2',
isNeed: 0,
}],
},
},
getOrders: {
interval: 2000,
retryTimes: 10,
apiName: '/test/prj/getOrders/{orderid}',
desc: '',
method: 'POST',
params: {},
},
});
api.getList({
data: {
param1: 'param1',
param2: 'param2',
}
})
.then(({ data }) => {
console.log(data);
})
.catch(err => {
console.log(err);
});
api.getOrders({
restful: {
orderid: 'param1',
}
})
.then(({ data }) => {
console.log(data);
})
.catch(err => {
console.log(err);
});
request拦截器
api._before = function _before(apiOpts, apiConf, next) {
next(apiOpts);
};