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

@zhangfuxing/http

v4.4.3

Published

Promise based HTTP client for node.js

Downloads

2

Readme

http

Promise based HTTP client for node.js

Install

$ npm i @zhangfuxing/http

Useage

const http = require('@zhangfuxing/http');
const assert = require('assert');

(async () => {
  // http
  const httpRes = await http.get('http://baidu.com');
  assert(httpRes.includes('<html>'));

  // https
  const httpsRes = await http.get('https://cnodejs.org/api/v1/topics?limit=1&mdrender=false');
  assert(httpsRes.success === true);

  // download file: use pipe
  const fs = require('fs');
  const res = await http.get('http://localhost:3000', {
    responseType: "stream"
  })
  res.pipe(require('fs').createWriteStream('zfx.txt'))
  // or use pipeline
  const stream = require('stream');
  const util = require('util');
  const pipeline = util.promisify(stream.pipeline);
  const res = await http.get(`${url}/stream`, {
    responseType: "stream"
  });
  await pipeline(res, fs.createWriteStream('zfx.txt'));

  // post Buffer
  const res = await http.post('http://localhost/upload', Buffer.from('abc'));
  assert(res.success === true);

  // post Stream
  const fs = require('fs');
  const readStream = fs.createReadStream('./index.js');
  const res = await http.post('http://localhost/upload', readStream);
  assert(res.success === true);

  // post json
  const data = {
    username: 'zfx',
    password: 'password'
  };
  const res = await http.post('http://localhost/upload', data);
  assert(res.success === true);
  
  // post application/x-www-form-urlencoded
  const data = {
    username: 'zfx',
    password: 'password'
  };
  const options = {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  };
  const res = await http.post('http://localhost/upload', data, options);
  assert(res.success === true);

  // post FormData
  const FormData = require('form-data');
  const form = new FormData();
  const fs = require('fs');
  const readStream = fs.createReadStream('./index.js');
  form.append('my_field', 'my value');
  form.append('my_buffer', Buffer.from('abc'));
  form.append('my_file', readStream);
  // Set filename by providing a string for options
  form.append('my_file', readStream, '1.js' );
  // provide an object.
  form.append('my_file', readStream, { 
    filename: 'bar.jpg', 
    contentType: 'image/jpeg', 
    knownLength: 19806
  });
  const formHeaders = form.getHeaders();
  const res = await http.post('http://localhost/upload', form, {
    headers: {
      ...formHeaders,
    },
  });
  assert(res.success === true);

  // head
  const res = await http.head(url);
  assert(res.statusCode === 200);
  assert(res.statusMessage === 'OK');
  assert(res.headers && typeof res.headers === 'object');
  assert(res.statusCode === 200);
  assert(res.data === '');

  // options
  const res = await http.options(url);
  assert(res === 'GET,HEAD,POST,PUT,PATCH,DELETE'); 
})().catch(console.error);

More examples in the test folder.

Type definitions

import http = require('http');
import https = require('https');

interface Options {
  // `responseType` indicates the type of data that the server will respond with
  // options are: 'buffer', 'json', 'text', 'stream'
  responseType: 'json', // default

  // `responseEncoding` indicates encoding to use for decoding responses
  // Note: Ignored for `responseType` of 'stream' or client-side requests
  responseEncoding: 'utf8', // default

  // `timeout` specifies the number of milliseconds before the request times out.
  // If the request takes longer than `timeout`, the request will be aborted.
  timeout?: number; // Default: Node.js default value

  // Controls Agent behavior
  agent?: http.Agent | https.Agent | boolean;

  // Basic authentication i.e. 'user:password' to compute an Authorization header.
  auth?: string;

  // A function that produces a socket/stream to use for the request when the agent option is not used. 
  // This can be used to avoid creating a custom Agent class just to override the default createConnection function
  createConnection?: Function;

  // IP address family to use when resolving host or hostname. Valid values are 4 or 6. 
  // When unspecified, both IP v4 and v6 will be used.
  family?: number;
  
  // An object containing request headers.
  headers?: object;

  // Use an insecure HTTP parser that accepts invalid HTTP headers when true. 
  // Using the insecure parser should be avoided. See --insecure-http-parser for more information. 
  insecureHTTPParser?: boolean; // Default: false

  // Local interface to bind for network connections.
  localAddress?: number;

  // Custom lookup function. Default: dns.lookup().
  lookup?: Function;

  // Optionally overrides the value of --max-http-header-size for requests received from the server, i.e. the maximum length of response headers in bytes. Default: 8192 (8KB).
  maxHeaderSize?: number;

  // Specifies whether or not to automatically add the Host header. Defaults to true.
  setHost: boolean;

  // only include body data
  onlyData?: boolean; // Default: method === 'HEAD' ? false : true;

  // if onlyData is false, withRequest is true can add request to res data
  withRequest?: boolean;
  
  // if onlyData is false, withResponse is true can add response to res data
  withResponse?: boolean;
}

interface request {
  /**
   * head 
   *   - `url` the server URL that will be used for the request
   *   - `options` optional parameters
   */
  head(url: string, options?: Options): Promise<any>;

  /**
   * options 
   *   - `url` the server URL that will be used for the request
   *   - `options` optional parameters
   */
  options(url: string, options?: Options): Promise<any>;

  /**
   * get 
   *   - `url` the server URL that will be used for the request
   *   - `options` optional parameters
   */
  get(url: string, options?: Options): Promise<any>;

  /**
   * post 
   *   - `url` the server URL that will be used for the request
   *   - `data` the data to be sent as the request body
   *   - `options` optional parameters
   */
  post(url: string, data: object, options?: Options): Promise<any>;

  /**
   * put 
   *   - `url` the server URL that will be used for the request
   *   - `data` the data to be sent as the request body
   *   - `options` optional parameters
   */
  put(url: string, data: object, options?: Options): Promise<any>;

  /**
   * patch 
   *   - `url` the server URL that will be used for the request
   *   - `data` the data to be sent as the request body
   *   - `options` optional parameters
   */
  patch(url: string, data: object, options?: Options): Promise<any>;

  /**
   * delete 
   *   - `url` the server URL that will be used for the request
   *   - `options` optional parameters
   */
  delete(url: string, options?: Options): Promise<any>;
}

declare const request: request;

export = request;

test

$ cd ./test
$ node test