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

go-fetch

v3.1.1

Published

A pluggable HTTP client.

Downloads

119

Readme

go-fetch

Build Status

A pluggable HTTP client for Node.JS.

go-fetch boasts a simple API but supports many features through plugins.

Features:

  • Support for HTTP and HTTPS protocols
  • Support for streaming
  • Pluggable API with plugins for:
    • following redirects
    • compression
    • authentication
    • working with JSON
    • ...and lots more

Installation

npm install --save go-fetch

Usage

GET


const Client = require('go-fetch');
const json = require('go-fetch-json');

new Client()
  .use(json())
  .get('http://httpbin.org/get')
    .then(res => {
      console.log(res.toString());
      return res.json();
    })
    .then(json => console.log(json))
    .catch(err => console.error(err.stack))
;

POST


const Client = require('go-fetch');
const json = require('go-fetch-json');

new Client()
  .use(json())
  .post('http://httpbin.org/post', {msg: 'Go fetch!'})
    .then(res => {
      console.log(res.toString());
      return res.json();
    })
    .then(json => console.log(json))
    .catch(err => console.error(err.stack))
;

API

Client

A HTTP client.

new Client([options : object])

Create a new HTTP client.

Options:

.use(plugin : function) : Client

Extend the functionality with a plugin.

Parameters:

  • plugin Required. A plugin function.

Returns:

The client.

.before(middleware : function) : Client

Extend the functionality with a middleware function which is run before a request is sent.

Parameters:

  • middleware Required. A middleware function.

Returns:

The client.

.after(middleware : function) : Client

Extend the functionality with a middleware function which is run after a request is sent.

Parameters:

  • middleware Required. A middleware function.

Returns:

The client.

.get(url : string, [headers : object]) : Promise

Send a HTTP GET request.

Parameters:

  • url Required. The request URL.
  • headers Optional. The request headers. An object containing key-value pairs.

Returns:

A Promise. Resolves with a Response. Rejects with an Error.

.post(url : string, [headers : object], [body : *]) : Promise

Send a HTTP POST request.

Parameters:

  • url Required. The request URL.
  • headers Optional. The request headers. An object containing key-value pairs.
  • body Optional. The request body. May be a string or a stream.

Returns:

A Promise. Resolves with a Response. Rejects with an Error.

.put(url : string, [headers : object], [body : *]) : Promise

Send a HTTP PUT request.

Parameters:

  • url Required. The request URL.
  • headers Optional. The request headers. An object containing key-value pairs.
  • body Optional. The request body. May be a string or a stream.

Returns:

A Promise. Resolves with a Response. Rejects with an Error.

.delete(url : string, [headers : object]) : Promise

Send a HTTP DELETE request.

Parameters:

  • url Required. The request URL.
  • headers Optional. The request headers. An object containing key-value pairs.

Returns:

A Promise. Resolves with a Response. Rejects with an Error.

.request(method : string, url : string, [headers : object], [body : *]) : Promise

Send a HTTP request.

Parameters:

  • method Required. The request method.
  • url Required. The request URL.
  • headers Optional. The request headers. An object containing key-value pairs.
  • body Optional. The request body. May be a string or a stream.

Returns:

A Promise. Resolves with a Response. Rejects with an Error.

Request

A HTTP request.

new Request([options : object])

Create a new request.

Options:

  • method Required. The request method.
  • url Required. The request URL.
  • headers Optional. The request headers. An object containing key-value pairs.
  • body Optional. The request body. May be a string or a stream.
.method : string

The request method.

.url : string

The request URL.

.headers : object

The request headers. An object containing key-value pairs.

.body : *

The request body. May be a string or a stream.

Response

A HTTP response.

new Response([options : object])

Create a new request.

Options:

  • status Required. The request method.
  • url Required. The request URL.
  • headers Optional. The request headers. An object containing key-value pairs.
  • body Optional. The request body. May be a string or a stream.
.status : number

The response stats.

.reason : string

The response reason.

.headers : object

The response headers. An object containing key-value pairs.

.body : *

The response body. May be a string or a stream. Usually a stream.

.text(encoding : string) : Promise

Read the response body into a string.

Returns:

A Promise. Resolves with a string. Rejects with an Error.

Plugins and Middleware

Plugin functions are simple functions that take a client instance and do something with it. Plugin functions are called when they are .use()d.

Middleware functions are simple functions that take a Request or Response object and a next() callback as parameters, and does something with them. e.g. add helper methods to the Request or Response objects, modify the headers or body sent or retreived from the server.

Example

Here's an example plugin that adds a .error() method to the Response for asserting whether an error occurred with the request.

function plugin(client) {
  client.after((res, next) => {
    res.error = () =>
      this.status >= 400 && this.status < 600
    ;
    next(null, res);
  });
}

prefix-url

Prefix each request URL with another URL.

content-type

Parse the Content-Type header.

parse-body

Concatenate and parse the response stream.

auth

Basic HTTP auth.

oauth1

OAuth v1 authentication.

follow-redirects

Automatically follow redirects.

decompress

Decompress response bodies compressed with gzip.

useragent

Add a User-Agent header to every request.

Changelog

v3.1.0

  • add: middleware can short-circuit the request to return a staged response
  • break: middleware can no longer be synchronous, they must call next() - don't think anyone else will be using sync (its a bit ambiguous) but the tests were

v3.0.0

Almost a total rewrite.

  • break: use promises instead of events and callbacks
  • break: use middleware instead of events for plugins
  • break: use simplified Request and Response objects

v2.0.0

  • moved prefixUrl, contentType and body plugins into their own repositories
  • changed the arguments passed to the before and after event handlers - handlers now receive a formal event object that allows propagation to be stopped and the request to be prevented
  • adding some tests
  • cleaning up documentation

To do

  • Moar tests
  • Plugins:
    • Cookie Jar
  • Support for XMLHttpRequest/fetch in the browser

License

The MIT License (MIT)

Copyright (c) 2016 James Newell