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

habrok

v4.1.2

Published

Promises/A+, request-powered, boom-enabled, JSON-first HTTP API client with automatic retry

Downloads

5

Readme

habrok Build Status Coverage Status

Promises/A+, request-powered, boom-enabled, JSON-first HTTP API client with automatic retry

Quick start

Prerequisites

Installing

Install with npm:

npm install habrok --save

Example

const Habrok = require('habrok');

const habrok = new Habrok();

habrok.request({
  method: 'GET',
  uri: 'https://api.github.com/repositories'
})
.then(console.log);
{
  "statusCode": 200,
  "headers": {
    "content-length": "4477",
    "content-type": "application/json; charset=utf-8",
    "etag": "6ffc6a0dbbe2613e4d8b3f7444e5c604",
    ...
  },
  "body": [
    {
      "id": 1296269,
      "private": false,
      "owner": {
        "id": 1,
        "login": "octocat",
        "avatar_url": "https://github.com/images/error/octocat_happy.gif",
        ...
      },
      "name": "hello-World",
      "full_name": "octocat/hello-world",
      "description": "Octocat's first repo!",
      "url": "https://api.github.com/repos/octocat/hello-world",
      ...
    },
    ...
  ]
}

API

Habrok (Constructor)

Definition

Habrok([configuration])

Required arguments

None

Optional arguments

  • configuration: Object with one or more of the below properties:

| Property | Type | Description | Default | | :------- | :--- | :---------- | :------ | | disableAutomaticJson | Boolean | Disable JSON headers and request/response bodies | false | | disableCustomHeaders | Boolean | Disable request headers added by Habrok | false | | disableRetryEconnreset | Boolean | Disable retry for connection reset errors | false | | retries | Number | Number of times to retry a failed request | 5 | | retryMinDelay | Number | Minimum milliseconds to wait before retrying a request | 100 | | retryMaxDelay | Number | Maximum milliseconds to wait before retrying a request | None | | retryCodes | Array<Number> | HTTP status codes that trigger a retry | [429, 502, 503, 504] |

By default, each request includes the following headers (which can be prevented with disableCustomHeaders):

The retry logic follows exponential backoff, summarized as:

MINIMUM(retryMaxDelay, retryMinDelay * (attempt ** 2))

With the default configuration, a failing request observes a delay sequence of 100, 400, 900, and 1600 milliseconds before rejecting with an error.

Returns

A Habrok HTTP API Client instance.

Examples

Construct a default client:

const Habrok = require('habrok');

const habrok = new Habrok();

Construct a client with a minimum retry delay of 250 milliseconds:

const Habrok = require('habrok');

const habrok = new Habrok({ retryMinDelay: 250 });

Construct a client that does not send Habrok-generated headers:

const Habrok = require('habrok');

const habrok = new Habrok({ disableCustomHeaders: true });

Habrok#request

Definition

habrok.request(req[, options])

Required arguments

Optional arguments

  • options: Object with one or more of the below properties:

| Property | Type | Description | Default | | :------- | :--- | :---------- | :------ | | attempt | Number | Integer indicating current request sequence number | None |

Generally, attempt is not needed. The internal retry engine will pass the current attempt count into the next request. Override only as necessary – e.g. in cases where the retry logic should be bypassed.

Returns

A Promise that resolves to an Object with the following properties:

  • statusCode: Number, the HTTP status code provided in the response
  • headers: Object, HTTP headers (lower-cased) and their values provided in the response
  • body: Any, the JSON-parsed response body (or the raw body if disableAutomaticJson was set)

The Promise is rejected with a Boom-wrapped error if an HTTP error occurs. The Promise is rejected with a generic Error if an error is returned by the underlying request library (usually from http.ClientRequest).

Examples

Send a GET request:

habrok.request({
  method: 'GET',
  uri: 'https://api.viki.ng/longships'
})

Send a POST request:

habrok.request({
  method: 'POST',
  uri: 'https://api.viki.ng/longships',
  json: {
    name: 'Oseberg'
  }
})

Development

Debug

The debug module is used for runtime logging. Omit the DEBUG environment variable to squelch all logging. Set DEBUG to the desired level (e.g. DEBUG=habrok) to restrict logging to a desired service. Or, use DEBUG=* to get all debug output from everywhere, including dependencies.

DEBUG=habrok* node index

Tests

To run the unit tests:

npm test

This project maintains ~100% coverage of statements, branches, and functions. To determine unit test coverage:

npm run coverage

Contribute

PRs are welcome! PRs must pass unit tests and linting prior to merge. PRs must not reduce unit coverage. For bugs, please include a failing test which passes when your PR is applied. To enable a git hook that runs npm test prior to pushing, cd into your repo and run:

touch .git/hooks/pre-push
chmod +x .git/hooks/pre-push
echo "npm test" > .git/hooks/pre-push

Versioning

This project follows semantic versioning. See the changelog for release information.

License

Etymology

From Grímnismál, Stanza 44:

The best of trees must Yggdrasil be, Skithblathnir best of boats; Of all the gods is Odin the greatest, And Sleipnir the best of steeds; Bifrost of bridges, Bragi of skalds, Habrok of hawks, and Garm of hounds.

Habrok, data-in-flight at its best.