npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tank-http-client.js

v1.0.4

Published

* tank-http-client.js a needle-based http client * Only supports node environment * Support chaining call

Downloads

9

Readme

tank-http-client

  • [x] tank-http-client.js a needle.js based http client
  • [x] Only supports node environment
  • [x] Support chaining call
  • [x] unit test

img.png

  • [x] document

install

npm install tank-http-client.js

import

//import
const thc = require("tank-http-client.js")

examples

//set base url 
thc.setBaseUrl("http://localhost:3008")
//get
thc.get("/test")
    .query({id: 1})
    .send()
    .then((res) => {
        console.log(res)
    }).catch(err => {
    console.error(err)
});
//output ->{code: 200, method: 'GET', data: 'get_test', search: '1'}

//await premise
async () => {
    const res = await thc.get("/test")
        .query({id: 1})
        .send()
    console.log(res)
}
//output ->{code: 200, method: 'GET', data: 'get_test', search: '1'}

more examples

use header


//use header
thc.get("/test").query({id: 1}).header({token: "token_8899"}).header("useAuth", true).send().then((res) => {
    //res:{code: 200, method: 'GET', data: 'get_test', search: '1',headerToken: "token_8899",useAuth:"true"}
})

//expressjs 
app.get('/test', function (req, res) {
    res.json({
        code: 200,
        method: req.method,
        data: "get_test",
        search: req.query["id"],
        headerToken: req.header("token"),
        useAuth: req.header("useAuth")
    })
})

delete

//delete
thc.delete("/test").query({id: 1}).send().then((res) => {
    //res: {code: 200, method: 'DELETE', data: 'delete_test', search: '1'}
})

post,put,patch

//post support put patch 
// thc.put("/test").xxx.send()
// thc.patch("/test").xxx.send()
thc.post("/test")
    .query({id: 1}).data({first: "1", nickname: "wind"}).data({
    name: "tank",
    nickname: "wind body"
}).send().then((res) => {
    // res:{
    //     code: 200,
    //     method: 'POST',
    //     data: 'post_test',
    //     search: '1',
    //     params: {first: '1', nickname: 'wind body', name: 'tank'}
    // }
})

file upload,file buffer upload

//upload
thc.post("/upload").query({id: 1})
    .file({
        file1: path.join(__dirname, "tank.png"),
        file2: path.join(__dirname, "tank.png")
    })
    .send()
    .then((res) => {

    })

//upload by buffer
thc.post("/upload").query({id: 1})
    .bufferFile({
        file1: path.join(__dirname, "tank.png"),
        file2: path.join(__dirname, "tank.png")
    })
    .send()
    .then((res) => {

    })

apis

HttpClient

declare class HttpClient {
  /**
   *
   * @param url
   */
  setBaseUrl(url: any): void;

  /**
   *
   * @param url
   * @return {Builder}
   */
  post(url: any): Builder;

  /**
   *
   * @param url
   * @return {Builder}
   */
  get(url: any): Builder;

  /**
   *
   * @param url
   * @return {Builder}
   */
  delete(url: any): Builder;

  /**
   *
   * @param url
   * @return {Builder}
   */
  patch(url: any): Builder;

  /**
   *
   * @param url
   * @return {Builder}
   */
  put(url: any): Builder;
}

Builder

declare class Builder {
  constructor(method: any, url: any, options: any);

  /**
   * @param queryData {{}}
   * @return {Builder}
   */
  searchParams(queryData: {}): Builder;

  /**
   *
   * @param queryData {{}}
   * @return {Builder}
   */
  query(queryData: {}): Builder;

  /**
   * set params
   * @param data {{string:*}}
   * @return {Builder}
   */
  data(data: {
    string: any;
  }): Builder;

  /**
   * set request to application/json
   * @param enable
   * @return {Builder}
   */
  applicationJson(enable?: boolean): Builder;

  /**
   * payload
   * @param data {{}}
   * @return {Builder}
   */
  payload(data: {}): Builder;

  /**
   * upload file/files
   * @param key {string|{}}
   * @param filename? {string}
   * @return {Builder}
   */
  file(key: string | {}, filename?: string): Builder;

  /**
   * upload buffer file
   * @param key {string|{}}
   * @param filename? {string}
   * @return {Builder}
   */
  bufferFile(key: string | {}, filename?: string): Builder;

  /**
   * set header |headers
   * @param key {string|{}}
   * @param value? {string}
   */
  header(key: string | {}, value: any): Builder;

  /**
   *
   * @return { Promise<*>}
   */
  send(): Promise<any>;

  /**
   *
   * @example
   * const { HttpsProxyAgent } = require('hpagent');
   * setAgent(new HttpsProxyAgent({
   *     keepAlive: true,
   *     keepAliveMsecs: 1000,
   *     maxSockets: 256,
   *     maxFreeSockets: 256,
   *     scheduling: 'lifo',
   *     proxy: 'https://localhost:8080'
   *   }))
   * @param agent {*}
   */
  setAgent(agent: any): Builder;

  /**
   * @param timeout {number} ms
   */
  setOpenTimeout(timeout: number): Builder;

  /**
   * @param timeout {number} ms
   */
  setReadTimeout(timeout: number): Builder;

  /**
   * @param timeout {number} ms
   */
  setResponseTimeout(timeout: number): Builder;

  setCompressed(enable?: boolean): Builder;

  setFollowMax(max?: number): Builder;

  setVerifySSLCertificate(enable?: boolean): Builder;

  setMultipart(enable?: boolean): Builder;

  /**
   *
   * @example setProxy('http://user:[email protected]:3128')
   * @param proxy
   * @return {Builder}
   */
  setProxy(proxy?: string): Builder;

  /**
   *
   * @param type {'auto'|'basic'|'digest'}
   * @return {Builder}
   */
  setAuth(type?: 'auto' | 'basic' | 'digest'): Builder;

  /**
   *   Content-Length len
   * @param len
   * @return {Builder}
   */
  setStreamLength(len?: number): Builder;

  /**
   * @param localAddress {string}
   * @return {Builder}
   */
  setLocalAddress(localAddress: string): Builder;

  /**
   * @param uri_modifier
   */
  setUriModifier(uri_modifier?: any): void;

  /**
   *
   * @param enable {boolean}
   * @return {Builder}
   */
  responseDecodeUTF8(enable?: boolean): Builder;

  /**
   * auto parse to xml/json default enable
   * @param enable
   * @return {Builder}
   */
  responseAutoParse(enable?: boolean): Builder;

  /**
   * response write to file at after parse and  decode
   * @param filepath {string}
   * @return {Builder}
   */
  responseOutput(filepath: string): Builder;

  /**
   *
   * @param options {{pfx?:string,key?:string,passphrase?:string,cert?:string,ca?:[],ciphers?:string,rejectUnauthorized?:boolean,secureProtocol?:string,family?:string}}
   * @return {Builder}
   */
  setHttpsOptions(options?: {
    pfx?: string;
    key?: string;
    passphrase?: string;
    cert?: string;
    ca?: [];
    ciphers?: string;
    rejectUnauthorized?: boolean;
    secureProtocol?: string;
    family?: string;
  }): Builder;
}