@voyo/http
v3.7.0
Published
frontend request library dependency rxjs
Downloads
7
Readme
Install
npm install @voyo/http
Usage
import {VoyoHttp} from "@voyo/http";
const http=new VoyoHttp({});
http.initPlugin();
http.xhr({
method:"get",
url:"http://localhost:3000/hello"
}).subscribe(
result=>console.log(result),
err=>console.error(err)
)
for weixin-mp
import {VoyoHttp,Weixin} from "@voyo/http";
const http=new VoyoHttp({
transmitter: new Weixin()
});
http.initPlugin();
Request Example
JSON
http.xhr({
method:"post",
url,
json:{
"findId":"xxx"
}
})
queryParams
http.xhr({
method:"get",
url,
query:{
"findId":"xxx"
}
})
download
http.xhr({
method:"post",
url,
responseType:"ArrayBuffer",
})
upload formData
http.xhr({
method:"post",
url,
formData
})
upload blob
http.xhr({
method:"post",
url,
blob
})
In Project
example
import {VoyoHttp} from "@voyo/http";
const http=new VoyoHttp({});
http.initPlugin();
// configure the global requested domain.
http.setHost("http://localhost:3000");
// login example
http.xhr({
method:"post",
path:"/login",
json:{
account:"xx",
pswd:"xx"
}
})
.toPromise()
.then(({result,statusCode})=>{
if(statusCode===200){
http.setGlobalHeader("user-auth-token",result.token);
}
})
// query example
http.xhr({
method: "get",
path: "/query",
query: {id:"01"}
})
.toPromise()
.then(({result})=>{
console.log(result);
})
API
- xhr
- addPlugin
- addPluginDynamic
- initPlugin
- setTransmitter
Plugin
import {VoyoHttp} from "@voyo/http";
const http=new VoyoHttp({});
http.addPlugin({
name: PLUGIN_NAME,
priority: PLUGIN_PRIORITY
})
http.initPlugin();
- VoyoHttpPlugin
export interface VoyoHttpPlugin {
priority: number; //Sort order
name: string;
patchCall?(self: any): void; //Rewrite http.
before?(params: HttpBeforeParams): Promise<void>; // Http hook.
registryHooks?(params: HttpApplyParams): void; // Http basic life hooks.
after?(params: HttpAfterParams): Promise<void>; // Http hook
wrapper?(params: HttpWrapperParams): Observable<HttpSuccessResult>; // Observer hook;
}
cachePlugin
import { VoyoHttp, VoyoCachePlugin } from "../package/index";
const http = new VoyoHttp({});
http.addPlugin(new VoyoCachePlugin({}));
http.initPlugin();
http
.xhr({
method: "post",
path: "/path",
json: {
xx: "xxxx",
},
cacheOpts: {
key: "cache-key",
expireSeconds: 60,
},
})
/* Add destroy cache function. on version 3.7.0
*/
// Now. You have an order submission request.
http.xhr({method:"put",path:"/payOrder/submit"},json:{...});
// You have a cached order list, but you want to destroy the cache after the order is submitted
http
.xhr({
method: "post",
path: "/payOrder/queryList",
json: {id}
cacheOpts:{
"key": "query-"+id,
onDestroy(httpParams){
// Destroy the list data cache when submitting an order.
return !!httpParams.path?.match("payOrder/submit");
}
}
})