cnfapi
author: bugszhou | Email:bugszhou@outlook.com
description: A lib project with ziu
cnfapi是基于axios的小程序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: 'browser',
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);
}
},
}, {
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);
};