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

apirequests

v0.4.2

Published

Simple multiple configurable API requests.

Downloads

17

Readme

apirequests

Calls several API resources in a simple way with JSON defined rules.
Can test several backends if the resources are response like expected.

NPM

How to use

const apirequests = require('apirequests');

const rules = [...];

const apitest = apirequests();
apitest.run(rules);

Use rules from a JSON file.

const apirequests = require('apirequests');

const apitest = apirequests();
apitest.run('rules.json');

Use YAML file with OpenAPI-Specification, needs examples to work!

const apirequests = require('apirequests');

const apitest = apirequests();
apitest.run('api.yaml');

Use a store engine, like MongoDB, for storing the rules.

const apirequests = require('apirequests');
const MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://127.0.0.1:27017/apirequests', (err, db) => {
    if(err) throw err;
    let apitest = apirequests();
    db.collection('urls').find().toArray((err, results) => {
        apitest.run(results);
        db.close();
    });
});

Options

All options are optional or have set default values.

output

The default value is print, other possible values are html, xml, db and ci.

  • html - writes a HTML file (reports.html)
  • xml - writes a XML file (reports.xml)
  • db - writes to a MongoDB collection (results)
  • ci - print the output and writes a XML file
printOnlyFailure

When this flag is set true only the failures are printed out.

outputFile and outputPath

The default values are reports.html and ./, will be used when output is set to html.
When output is set to xml or to ci then the outputFile will be named reports.xml.

const apirequests = require('apirequests');

let apitest = apirequests({output: 'html', outputFile: 'report.html'});
apitest.run('rules.json');
loop

When the requests should run in a loop with a timeout value.

require('apirequests')({loop: 2000}).run('rules.json');
connectionurl and collection

The default values are mongodb://127.0.0.1:27017/apirequests and results, will be used when output is set to db.

const apirequests = require('apirequests');
const MongoClient = require('mongodb').MongoClient;

const connectionUrl = 'mongodb://127.0.0.1:27017/requests';
const opts = {output: 'db', connectionurl: connectionUrl};

MongoClient.connect(connectionUrl, function(err, db) {
    if(err) throw err;

    let apitest = apirequests(opts);

    db.collection('urls').find().toArray((err, results) => {
        if(err) throw err;
        apitest.run(results);
        db.close();
    });
});

How to define rules

A rule takes basically an uri to run, the method is optional, GET is the default value.
To send custom headers use a headers object and define a form object inside of the rule to send data.
To test the response, define inside of the rule a response object. The response object can have a statuscode, host, time, data, regex and a headers object, this headers object can check contenttype, contentlength, cachecontrol and server.

Some examples how to define rules.

[{
    method: 'get',
    uri: 'http://webservice-point.appspot.com/test'
},
{
    method: 'post',
    uri: 'http://webservice-point.appspot.com/test',
    form: {
        name: "apirequests",
        test: "post"
    },
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    response: {
        statuscode: 200,
        data: '{"data": [{"test": "post"}, {"name": "apirequests"}], "response": "POST"}'
    }
},
{
    method: 'put',
    uri: 'http://webservice-point.appspot.com/test/123',
    response: {
        statuscode: 404,
        headers: {
            contenttype: 'text/html; charset=UTF-8',
            contentlength: '285'           
        }
    }
},
{
    method: 'patch',
    uri: 'http://webservice-point.appspot.com/test/123',
    response: {
        statuscode: 404,
        headers: {
            contenttype: 'text/html; charset=UTF-8',
            contentlength: '285'
        }
    }
},
{
    method: 'delete',
    uri: 'http://webservice-point.appspot.com/test',
    response: {
        statuscode: 404,
        headers: {
            contenttype: 'text/html; charset=UTF-8'
        },
        data: 'error',
        regex: true
    }
}]

Response validation supports joi when schema flag is set.

{
    ...
    response: {
        statuscode: 200,
        data: Joi.object()
        .keys({
            response: Joi.string().alphanum().min(3).max(30).required()
        }),
        schema: true
    }
}

To send JSON data it's needed to define a body and send the specific header.

{
    ...
    body: JSON.stringify({apirequest: 'post'}),
    headers: {
        'Content-Type': 'application/json'
    },
    response: {
        statuscode: 200,
        data: {apirequest: 'post'}
    }
}

It's possible to define groups to have depending requests or test CRUD functionality. The group requests are executed syncronous in order. key defines the field to find in the response like the id for the created entry to use this as reference for following calls.

{
    name: 'Group Test',
    key: '_id',
    group: [
        {            
            method: 'post',
            uri: 'http://localhost:3000/users',            
            body: JSON.stringify({email: '[email protected]'}),
            headers: {
                'Content-Type': 'application/json'
            },
            response: {
                statuscode: 201
            }
        },
        {
            method: 'get',
            uri: 'http://localhost:3000/users',
            response: {
                statuscode: 200,
                data: '"email":"[email protected]"',
                regex: true
            }
        },
        {
            method: 'put',
            uri: 'http://localhost:3000/users',
            body: JSON.stringify({email: '[email protected]'}),
            headers: {
                'Content-Type': 'application/json'
            },
            response: {
                statuscode: 200
            }
        },
        {
            method: 'get',
            uri: 'http://localhost:3000/users',
            response: {
                statuscode: 200,
                data: '"email":"[email protected]"',
                regex: true
            }
        },
        {
            method: 'delete',
            uri: 'http://localhost:3000/users',
            response: {
                statuscode: 200
            }
        }
    ]
}

Results

Per default the result be print out and looks like the picture below.

Console

The HTML file which gets created. {output: 'html'}

HTML

Feedback

Star this repo if you found it useful. Use the github issue tracker to give feedback on this repo.