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

sonofabatch

v1.3.1

Published

Instantly add a batch endpoint to your ExpressJS API

Downloads

17

Readme

sonofabatch

Quickly add a batch endpoint to your ExpressJS app or provide a batch facade to an existing JSON REST service.

usage

Attach the batch endpoint.

const express = require('express')
const app = express();
const router = express.Router();
const SonOfABatch = require('sonofabatch');
const batch = new SonOfABatch();
const bodyParser = require('body-parser');

router.post('/batch', batch.call); // make sure you use .post()

app.use(bodyParser.json());
app.use('/', router);

app.listen(3000, () => console.log(`Batch-O-Matic is listening on port 3000!`));

All other endpoints can now be called via the /batch endpoint by posting to it with a body such as the following

{ 
  requests : 
    [
      {
        method : "GET",
        path :  "/accounts",
        query : { hello : "world" }
      },
      {
        method : "POST",
        path   : "/accounts",
        body   : { name : "Important Account" }
      }
    ]
}

You should always expect the response to return the results in the order which they were requested.

control flow

By default the calls with execute on the server in parallel. However, if the calls need to be run in order an additional param is supported called 'execution'.

{ 
  execution : "series", // defaults to "parallel"
  requests : 
  [
    {
      method : "GET",
      path :  "/accounts",
      query : { hello : "world" },
    },
    {
      method : "POST",
      path   : "/accounts",
      body   : { name : "Important Account" }
    }
  ]
};

as a batch proxy

sonofabatch can be used as batch middleware to proxy calls to existing JSON REST API services. Below is a diagram of the use-case: sonofabatch proxy example

To configure sonofabatch as a batch proxy simply provide a serviceUrl in one of 3 places

  1. on instantiation of the middleware new SonOfABatch({serviceUrl: 'http://myserviceurl:8080'});
  2. in the root of the request made to the /batch endpoint. This applies to all calls defined in the request and also overrides 'serviceUrl' passed as a javascript option to new SonOfABatch();
```
{ 
  serviceUrl: "http://myserviceurl:8080"
  requests : 
    [
      {
        method : "GET",
        path :  "/accounts",
        query : { hello : "world" }
      },
      {
        method : "POST",
        path   : "/accounts",
        body   : { name : "Important Account" }
      }
    ]
}
```
  1. in each individual request (overrides serviceUrl passed via methods 1 and 2
```
{ 
  requests : 
    [
      {
        serviceUrl: "http://myserviceurl1",
        method : "GET",
        path :  "/accounts",
        query : { hello : "world" }
      },
      {
        serviceUrl: "http://myserviceurl2",
        method : "POST",
        path   : "/accounts",
        body   : { name : "Important Account" }
      }
    ]
}
```

params

  • serviceUrl: should include protocol:hostname:port
  • mergeHeaders: will take the headers from the batch request and add them to the headers of each request defined in the payload
  • protocol: only useful when not passing serviceUrl. will set the protocol used when defaulting to 127.0.0.1
  • port: only useful when not passing serviceUrl. will set the port used when defaulting to 127.0.0.1

TODO

  • add tests
  • more meaningful error messaging