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

multirest

v1.0.2

Published

Combine REST requests

Downloads

1

Readme

multirest

NPM Version Dependencies Build Status License Maintenance GitHub stars

Connect/express middleware to serve combined REST requests in one REST call over one HTTP(S) connection.

Inspiration

The REST APIs are meant to be the generic servers for different clients (my own Angular, my own mobile client, but also third party client or other REST based integration). But if my Angular page needs to load 10 different resources to build the page, those may result in 10 HTTP requests to the server.

Or worse: I may be tempted to create a client-specific route to return the 10 resources.

The idea is that between concepts like multirest and GraphQL the client implementations should be able to specify what data is needed and to which extent, without affecting the general design of the REST server.

MultiREST allows the client (or client library, possibly working behind the scene) request several REST resources at once, without changing the generic behavior of the REST server by adding one single line to the server setup.

Install

npm install -S multirest

Setup

Declare it and use it

var multirest = require("multirest");
// Assuming 'app' is a connect/express object
app.use('/api/multirest', multirest(app));

Yes, I know, it is three lines... Making it one line is left as homework to the reader :-)

multirest(app, options)

Create a new multirest middleware with the provided options, at the specific route mount. Recommended mount point is '/api/multirest'.

The multirest middleware will parse the combined request, execute the individual requests as if they were passes in one by one, collect their responses, combine and send back to the caller/client.

It is client's responsibility to combine the individual REST requests into the combined request (client libraries coming soon!), as well as splitting the response data (client libraries really coming soon!).

app

The multirest middleware needs a reference to the app object to be able to send the individual requests internally, without opening additional http connections to this very server.

options

Multirest middleware accepts these properties in the options object.

options.concurrency

How many individual subrequest are allowed to be in-flight at the same time.

Default = 5

REST API

(Prototype of one approach for a client-side is in the test.js: rest request method that returns a promise of the rest data (as if it were doing it directly), but you can call it multiple times, before firing it off. When the multirest request is done, the many individual promises get fulfilled with their data.)

  • POST
    • url: configured route (e.g. '/api/multirest')
    • body: JSON Array of individual requests
      • method: 'GET', 'POST', 'PUT', 'PATCH' or 'DELETE'
      • url: individual request URL including query part
      • body: JSON with POST-like data
    • returns: JSON Array with individual responses:
      • status: http status of the individual response
      • body: JSON body of the individual response

Example:

POST /api/multirest
[{
  "method": "GET",
  "url": "/api/things/1"
},
{
  "method": "POST",
  "url": "/api/things",
  "body": { "name": "TWO"}
},
{
  "method": "GET",
  "url": "/api/thongs"
}]

returns

[{
  "status": "200",
  "body": { "id": "1", "name": "ONE"}
},
{
  "status": "200",
  "body": { "id": "2", "name": "TWO"}
},
{
  "status": "404",
  "body": {}
}]

Other supported behavior

  • cookies are passed in and out
  • header fields are passed in

Usage Limitations

Local requests only

At this time, only locally served REST requests are supported.

No multi-part, etc

Regular JSON-based requests (GET/POST/PUT/PATCH/DELETE) only.

License

MIT