@deot/http-core
v1.1.5
Published
可适配任何端的网络控制,具体的请求实现`provider`控制
Downloads
3
Readme
@deot/http-core
可适配任何端的网络控制,具体的请求实现provider
控制
在Request/MDN基础上新增api
可全局注册或单次注册
provider
: 适配任何端的网络控制localData
: 本地mocktimeout
: 超时控制maxTries
: 最大尝试次数interval
: maxTries > 1时,间隔时间shared
: 共享请求(多个相同发起,等待同一个请求)onStart
: 发起回调onFinish
: 结束回调onRequest
: 请求拦截onReponse
: 响应拦截[custom]
: 自行定义的api, 可以在on*
的回调中做特殊处理
import { HTTPController, HTTPRequest, HTTPResponse } from '@deot/http-core';
const baseURL = 'https://xxx.com';
const apis = {
A_GET: '/api.json'
};
const Network = new HTTPController({
provider: (request: HTTPRequest) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(new HTTPResponse({ body: request.body }));
}, 300);
});
},
onRequest(leaf) {
const request = new HTTPRequest(leaf.request);
if (request.url && !/[a-zA-z]+:\/\/[^\s]*/.test(request.url)) {
const [key, query] = request.url.split('?'); // 避免before带上?token=*之类
request.url = `${apis[key] ? `${baseURL}${apis[key]}` : ''}${query ? `?${query}` : ''}`;
}
return request;
}
});
await Network.http(`A_GET`);
await Network.http(`https://xxx.com/api.json`);
await Network.http({
url: `https://xxx.com/api.json`
});
await Network.http({
url: `A_GET`,
body: {}
});
// cancel 1
const leaf = Network.http(`A_GET`);
await leaf.cancel();
// cancel 2
const shell = Network.custom(`A_GET`);
shell.send();
await shell.cancel();