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

@onepunya/xontol

v1.0.5-beta

Published

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

Downloads

8

Readme

XONTOL Xtensible, Optimized, No-dependencies, Transparent, Organized, Lightweight

Simple and readable HTTP requests. Promises-based operation. Integrated assertion features. Support for request pooling. Zero external dependencies. Installation Install with: npm install xontol.

Example Usage

Fetching the NodeJS homepage:

const xontol = require('@onepunya/xontol');

const { status, headers, body, assert } = await xontol('GET', 'https://nodejs.org/en/');

Shorthands XONTOL provides shorthand methods for all HTTP verbs:

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

Posting form data:

const { post } = require('@onepunya/xontol');

const { status, headers, body, assert } = await post('https://example.com/', 'key=value&key=value');

Including headers:

const { put } = require('@onepunya/xontol');

const { status, headers, body, assert } = await put(
    'https://example.com/',
    { 'Custom-Header': 'example' },
    'key=value&key=value'
);

To disable automatic content and date headers:

const { put } = require('@onepunya/xontol');

const { status, headers, body } = await put(
    'https://example.com/',
    { 'no-auto': true, 'Custom-Header': 'example' },
    'key=value&key=value'
);

Available HTTP methods in XONTOL:

const xontol = require('@onepunya/xontol');

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

Accessing the raw NodeJS res object, with additional status and body keys:

const { get } = require('@onepunya/xontol');
const res = await get('https://nodejs.org/en/');

Assertions Testing response content:

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

// Asserting response body content
assert.body.exactly('expectedContent');
assert.body.contains('expectedContentPart');
assert.body.match(/expectedPattern/);

// Chainable assertions
assert.body.type('application/json').length(42);

// JSON body assertions
assert.body.json
    .hasKey('key')
    .match('key', 'value')
    .empty(); // checks for an empty object

// JSON array assertions
assert.body.json.array
    .match(0, 'expectedValue')
    .includes('expectedValue')
    .length(3)
    .empty(); // checks for an empty array

// Status code assertions
assert.status.is(200);
assert.status.not(404);
assert.status.in([200, 201, 202]);
assert.status.notIn([500, 501, 502]);
assert.status.type(2); // checks for 2xx codes
assert.status.notType(5); // checks for not 5xx codes

// Header assertions
assert.headers
    .has('authorization')
    .match('connection', 'keep-alive');
Authentication

Currently supports Basic and MD5 Digest authentication.

Ensure the server responds with 401 and a WWW-Authenticate header for authentication to work.

Send credentials via the headers object:

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

Alternatively, include the credentials in the URL:

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

Pooling Define a request pool with a max size of 10 requests and a 2-second timeout:

const { Pool } = require('@onepunya/xontol');
const myPool = new Pool({ size: 10, timeout: 2000 });

Execute the pool over a set of request bodies:

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

Wait for all requests to complete:

// Using promises
const responses = await myPool.done();

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

Wait for a single request in the pool to complete:

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

Handle each response individually:

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

Contributing mubh mr.one and all