代码拉取完成,页面将自动刷新
npm install @voyo/http
uniapp中,使用 uni.request
进行请求。
非uniapp环境 使用 xmlHttpRequest
进行请求。
import {Http} from "@voyo/http";
const http=new Http();
//get
http.xhr("get","http://localhost:3000",{p:1})
.subscribe(
result=>{},
err=>{}
)
//post
http.xhr("post","http://localhost:3000",{p:1})
.subscribe(
result=>{},
err=>{}
)
If the request is progress.
The same request will prevent the previous Subscription
,
and only newly created subscription
are retained.
During this process. There is always only one HTTP request.
cacheXhr
cache request.Create a cache request. You can get data from cache, if the cache does not expire. API cacheXhr
(num:number):void
setAfterHandler
, retry function is called the most times.(key:string,value:string,priority?:boolean):void
false
(key:string):void
(v:boolean):void
usage
type AfterFn = (afterFnParams: AfterFnParams) => Promise<any>;
setAfterHandler=(fn: AfterFn)=>void;
example
This is an example of dealing with session expiration. Automatically fetching the session, and resending the request.
const getSession=http.xhr("post","/session");
const handleSession=()=>{}
http.setAfterHandler(
({result,retry})=>
result.content==="expire"?
getSession().pipe(
mergeMap(sessionResult=>{
handleSession(sessionResult)
return retry()
})).toPromise()
:Promise.resolve(result);
}
)
http.xhr(
HttpMethod,
relativeUrl,
Params,
Params2
)
usage
http.cacheXhr=(p:HttpCacheXhr)=>Observable;
url
request is triggered.retry request
http.xhr(...)
.pipe(retry(5))
.subscribe(...)
toPromise
http.xhr(...)
.toPromise()
.then(...)
delay request
http.xhr(...)
.pipe(delay(100))
.subscribe(...)
timeout request
http.xhr(...)
.pipe(timeout(4000))
.subscribe(()=>{},err=>{})
forkJoin multiple request
const a=http.xhr(...)
const b=http.xhr(...)
const c=http.xhr(...)
forkJoin({
a,b,c
}).subscribe(v=>console.log(v);
// Logs:
// {a:xxx,b:xxx,c:xxx}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。