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

muhb

v3.2.1

Published

A set of functions for coding easy to read HTTP requests.

Downloads

2,842

Readme

MUHB

Method, URL, Headers and Body

  • Easy to read HTTP requets.
  • Run over promises.
  • Built-in assertion functionality.
  • Send many request over pooling strategy.
  • NO external dependencies

Usage

Install with: npm i muhb.

Getting NodeJS homepage:

const muhb = require('muhb');

var { status, headers, body } = await muhb('get', 'https://nodejs.org/en/');

Shorthands

MUHB exposes a short signature for all HTTP verbs:

method ( String url [, Object headers] [, String body] )

Posting a random form:

const { post } = require('muhb');

var { status, headers, body } = await post('https://nodejs.org/en/', 'key=value&key=value');

Sending headers:

const { put } = require('muhb');

var { status, headers, body } = await put(
    'https://nodejs.org/en/',
    { myHeader: 'example' },
    'key=value&key=value'
);

If you would like MUHB to not generate automatic content and date headers, send a ghost parameter like this:

const { put } = require('muhb');

var { status, headers, body } = await put(
    'https://nodejs.org/en/',
    { 'no-auto': true, myHeader: 'example' },
    'key=value&key=value'
);

Having all available muhb methods:

const muhb = require('muhb');

muhb.get //=> [function]
muhb.post //=> [function]
muhb.patch //=> [function]
muhb.del //=> [function]
muhb.put //=> [function]
muhb.head //=> [function]
muhb.options //=> [function]

If you need to access the nodejs res object, all muhb methods return it modified to have our status and body keys.

const { get } = require('muhb');
let res = await get('https://nodejs.org/en/');

Assertions

Testing response data:

var { assert } = await get('https://example.com');

// Assert about your reposnse body.
assert.body.exactly('foobar');
assert.body.contains('oba');
assert.body.match(/oo.a/);

// Mostly chainable.
assert.body.type('application/json').length(23);

// Test JSON bodies.
assert.body.json
    .hasKey('foo')
    .match('foo', 'bar')
    .empty(); // test for {}

// Test JSON array.
assert.body.json.array
    .match(1, 'bar')
    .includes('foo')
    .length(2)
    .empty();

// Assert about response status code
assert.status.is(200);
assert.status.not(400);
assert.status.in([ 200, 203, 404 ]);
assert.status.notIn([ 500, 403, 201 ]);
assert.status.type(2); // Test for 2xx
assert.status.notType(5) // Test for NOT 5xx

// Assert about response headers
assert.headers
    .has('authorization')
    .match('connection', 'close');

Authentication

As of now the only auth methods supported are Basic and MD5 Digest.

You must ensure your server responds with 401 and a WWW-Authenticate header so muhb knows to perform the auth.

Then just send your credentials in the headers object as follows:

let { body } = await post('http://example.com', {
    auth: { username: 'my-user', password: 'my-pass' }
});

Or use the user and password syntax (they will be stripped from the URL before being sent).

let { body } = await post('http://my-user:[email protected]');

Pooling

Define a pool with a max size of 10 requests and a timeout of 2 seconds:

const { Pool } = require('muhb');
let myPool = new Pool({ size: 10, timeout: 2000 });

Then run the pool over an array of say request bodies:

let bodies = 'abcdefghijklmnopqrstuvwxyz'.split('');
bodies.forEach( body => myPool.post('http://localhost/fail', body) );

Wait until all requests recive responses.

// With promises
let responses = await myPool.done();

// With events
myPool.on('finish', responses => console.log(responses));

Wait for a single request in the pool to finish.

let res = await myPool.post('http://localhost/fail');

Act on every request that is responded.

myPool.on('response', (req, res) => {
    console.log(res.status);
});

Contributing

We will be delighted to receive your issues and MRs.