3.6.2 • Published 1 year ago

@voyo/http v3.6.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

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,
    },
  })
3.6.2

1 year ago

3.6.1

1 year ago

3.6.0

2 years ago

3.5.2

2 years ago

3.5.1

2 years ago

3.4.0

3 years ago

3.3.4

3 years ago

3.3.3

3 years ago

3.5.0

3 years ago

3.3.2

3 years ago

2.6.0

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.1.3

3 years ago

3.1.2

3 years ago

3.2.0

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

3.0.0-alpha.1

3 years ago

2.5.1

3 years ago

2.5.0

4 years ago

2.4.0

4 years ago

2.3.0

4 years ago

2.2.5

4 years ago

2.2.4

4 years ago

2.2.3

4 years ago

2.2.1

4 years ago

2.2.2

4 years ago

2.2.0

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.0

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago