1 Star 0 Fork 0

conomi / ztwx-http

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Install

npm install @voyo/http

Info

  • uniapp中,使用 uni.request 进行请求。

  • 非uniapp环境 使用 xmlHttpRequest 进行请求。

Usage

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=>{}
    )

Capacity

  • Auto prevent duplicate request.

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

API

Http

  • setTicketKey
    • Set one global http header as a ticket key.
  • setTicketValue
  • setHost
    • Set a fixed host url,so that you can use relative paths.
  • setBeforeHandler
    • handle params before request.
  • setAfterHandler
    • handle result after request.
  • setMaxRetry (num:number):void
    • In the setAfterHandler, retry function is called the most times.
  • setGlobalHeader (key:string,value:string,priority?:boolean):void
    • set the global request header
    • @priority Overrides the header for current request if true.default false
  • clearGlobalHeader (key:string):void
    • clear the global request header
  • setWithCredentials (v:boolean):void
setAfterHandler

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

http.xhr(
    HttpMethod,
    relativeUrl,
    Params,
    Params2
)

Http.cacheXhr

usage

http.cacheXhr=(p:HttpCacheXhr)=>Observable;
HttpCacheXhr
  • method: HttpMethod
  • relativeUrl: string
  • cacheKey?: string
  • params?: Params
  • params2?: Params2
  • expires?: number
    • Destroy the cache when it expires
  • destroyOnXhr?: Array<string |RegExp>
    • Destroy the cache when a matching url request is triggered.

RXJS

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}

空文件

简介

暂无描述 展开 收起
JavaScript 等 2 种语言
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/conomi/ztwx-http.git
git@gitee.com:conomi/ztwx-http.git
conomi
ztwx-http
ztwx-http
master

搜索帮助