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

http-client-factory

v0.3.1

Published

Fluent and extensible HTTP[S] client for RESTful calls

Downloads

12

Readme

Http Client Factory

This library is aimed at improving the development experience for REST-like calls in NodeJS. It builds off the Node http library. It's inspired by the System.Net.Http.HttpClientFactory library in .NET.

##Basic Usage

var HttpClientFactory = require("http-client-factory")

//Issues a GET request to http://www.tempuri.org/my/endpoint?some=value&search=text
var promise = HttpClientFactory.getClient()
    .get("http://www.tempuri.org/my/endpoint", { some: "value", search: "text"})
  
promise.then(function (response) {
    //Do something with the response
})
.catch(function (error) {
    //Do something with the error
})

API

HttpClientFactory

  • .getClient(AgentOptions)
  • Returns HttpClient
  • AgentOptions is from http library
  • If AgentOptions is null, default settings are used (recommended)

HttpClient

  • .addHeader(headerKey, headerValue)
  • Adds a header to the request with the given key and value
  • .setAuthorization(scheme, parameter)
  • Sets the Authorization header to scheme parameter
  • Returns HttpClient
  • .setBasicAuth(username, password)
  • Sets the Authorization header to use basic authentication with the given credentials
  • Returns HttpClient
  • .addHandler(HttpClientHandler)
  • Adds an HttpClientHandler to run on requests and responses
  • Request handlers are run in the order they're added. The first handler added will run first
  • Response handlers are run in the reverse order. The last handler added will run first
  • Returns HttpClient
  • .get(url, RequestBody)
  • Returns Promise
  • .post(url, RequestBody)
  • Returns Promise
  • .put(url, RequestBody)
  • Returns Promise
  • .delete(url, RequestBody)
  • Returns Promise
  • .send(HttpRequest, RequestBody)
  • Sends a raw request
  • Returns Promise

HttpClientHandler

  • onRequest: function (HttpRequest, body)
  • Reads or modifies request and/or body as needed
  • Leave undefined if not needed
  • onResponse: function (HttpResponse)
  • Reads or modifies response if needed
  • Leave undefined if not needed

RequestBody

  • Normal JSON object
  • For .get(), .delete(), .head(), and .options() requests, this will be converted into query string parameters
  • For .put(), .post(), and .patch() requests, this will be sent as JSON content

##Reading the Response The response is a typical HTTP response as defined by the http library, with the addition of a body property which contains the response content (in string form).

##Handlers

Setting authorization headers

var handler = {
    onRequest: function (req) {
        req.headers.authorization = "sampleAuthScheme authValue";
    }   
}

//Issues a POST request to the URL 
//with the authorization header set to "sampleAuthScheme authValue"
//and a JSON payload  of { "postdata": "goes here" }
HttpClientFactory.getClient()
    .addHandler(handler)
    .post("http://www.tempuri.org/my/endpoint", { postdata: "goes here" })

As of v0.2.0, this can be accomplished by calling client.setAuthorization("sampleAuthScheme", "authValue")

Logging request/response data

var traceLogHandler = {
    onRequest: function (req, body) {
        var logger = require("myTraceLogger")
        logger.logRequest(req.href, req.headers, body)
    },
    onResponse: function (response) {
        var logger = require("myTraceLogger")
        logger.logResponse(response.headers, response.body)
    } 
}

//Logs both request and response data to myTraceLogger
HttpClientFactory.getClient()
    .addHandler(traceLogHandler)
    .post("http://www.tempuri.org/my/endpoint", { postdata: "goes here" })

##Promises

httpClientFactory uses the bluebird library for promises