rein

基于 fetch
的 http
客户端请求工具,适用于浏览器。(Inspired by axios and apiz)
用法
安装
yarn add rein-api
例子
初始化
import Rein from 'rein-api';
function getCookie(name) {
}
const instance = new Rein({
debug: true,
baseUrl: 'http://www.forchangefe.com/',
defaultPrefix: '/v1',
headers: {
'common': {
Authorization: `Bearer ${getCookie('token')}`
},
'GET': {
'Content-Type': 'application/json',
},
},
transformRequest: [function(data, headers) {
if (headers.common['X-Log-Data']) {
console.log(data);
}
return data;
}],
transformResponse: [function(response) {
if (!response.hasOwnProperty('errcode')) {
response.errcode = 0;
}
return response;
}],
})
发送请求
instance.request({
method: 'get',
params: {
foo: 1,
},
data: {
bar: 2
},
resource: {
id: 1
},
url: '/zoo/{id}',
restful: true,
prefix: '/v2',
meta: {
noAuth: true,
},
})
.then(res => {
console.log(res);
})
.catch(e => {
console.error(e);
})
问答
可以推荐点最佳实践吗?
rein
采用 typescript
来开发。强烈建议您使用 typescript
!我们还导出了相关的类型注解,文档不能覆盖的地方,类型注解都帮你解决了。
至于如何集成,后续我们会出一篇文章做介绍。
生产环境能不能用?
当然,ForChange Python 小课
日均 200 * 700 订单的报名系统基于这款请求库.
为什么叫 rein
?
"I've been called. I have to answer, always."
-- From OverWatch
License
MIT