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

rail

v0.9.1

Published

An enhanced HTTP/RESTful API Client

Downloads

52

Readme

rail

NPM version Build Status

An enhanced HTTP/RESTful API Client

rail is an io.js/node.js HTTP client supporting https, http and http2.

A set of built-in plugins, currently featuring buffer, cookies, json, redirect, retry, timeout & validate simplify making requests, and a powerful event-driven plugin interface aids in the implementation of complex automated RESTful API calls.

The concept of a single request is extended to a possible series of requests further referenced as a call. This allows a seamless integration of redirect and authentication mechanisms that require multiple requests to satisfy the original one.

The API is mostly compatible with https.request() and allows rail to be used as a drop-in replacement. A completely transparent plugin integration enables scenarios with automated retries on upload stream errors, while still exhibiting a behavior similar to https.request().

rail works with io.js, node.js 0.10.x & 4.1.

Table of Contents

Installation

$ npm install rail --save-exact

Examples

URL only

Directly pass an URL that gets parsed into proto, host, port & path.

var RAIL = require('rail');

RAIL.call('https://www.github.com/skenqbx/rail', function(response) {
  // consume response
}).end();

back to top

URL & Plugin Options

Again, only pass an URL, but this time as a property to allow passing of plugin options.

var RAIL = require('rail');

RAIL.call({
  url: 'https://www.github.com/skenqbx/rail',
  buffer: true
}, function(response) {
  if (response.buffer) {
    console.log(response.buffer.toString());
  }
}).end();

back to top

Request & Plugin Options

The usual way of supplying every parameter separately.

var RAIL = require('rail');

RAIL.call({
  host: 'www.github.com',
  path: '/skenqbx/rail',
  buffer: true
}, function(response) {
  if (response.buffer) {
    console.log(response.buffer.toString());
  }
}).end();

back to top

Custom Client

A custom client allows to define default options and configure a set of plugins for all calls made with that client.

var RAIL = require('rail');

var client = new RAIL({
  // set default request options
  request: {
    host: 'github.com'  // set default host
  },
  // load & configure the buffer plugin
  buffer: {
    default: true // buffer all responses by default
  },
  // load & configure the json plugin
  json: {
    auto: true // try to parse all responses with content-type equal to application/json
  },
  // load & configure the redirect plugin
  redirect: {
    limit: 3 // allow a maximum of three redirects for each call
  }
});

// load custom "my" plugin
client.use('my', MyPlugin/*, pluginOptions */);

Now use the custom client the same way as the globalClient above

var call = client.call({
  path: '/skenqbx/rail'
}, function(response) {
  // check if we got a json response
  if (response.json) {
    console.log(response.json);

  // alternatively use the raw response body
  } else if (response.buffer) {
    console.log(response.buffer.toString());

  // ... or if a bailout happened (buffer max size exceeded)
  } else if (response.buffer !== null) {
    // consume the response
    response.on('readable', function() { /* ... */ });
    response.on('end', function() { /* ... */ });
  }
});

call.on('error', function(err) { /* ... */ });

call.end();

back to top

Use as a drop-in replacement

rail does not support the hostname, auth, localAddress & socketPath options, see rail.call() for more information.

When not using https, make sure to set the correct default protocol

var RAIL = require('rail');
RAIL.globalClient.proto = 'http';

... and then replace every call to http.request with RAIL.call.

Alternatively create a custom client with defaults & plugins configured to your needs.

back to top

Tests

  • npm run lint Lint the code using eslint and these rules
  • npm test Lint the code, run tests with mocha & collect coverage with istanbul
    • detailed coverage report is available at coverage/lcov-report/index.html
  • npm run update Dependency update tests performed by next-update
  • run ./tools/cross-test.sh to test all relevant io.js/node.js versions, uses nvm

Coverage

Statements   : 98.17% ( 803/818 )
Branches     : 91.53% ( 378/413 )
Functions    : 100% ( 109/109 )
Lines        : 98.17% ( 803/818 )

back to top