@rill/batch

0.4.1 • Public • Published

Rill
@rill/batch
API stability Standard NPM version Downloads Gitter Chat

Batch API requests with Rill.

If you need to perform several different requests to your API simultaneously, you could combine them all together (in one querystring or body) and send only one request to the server, improving latency in the browser.

Currently, only GET requests can be batched.

Installation

npm install @rill/batch

Example

app.js

const rill = require('rill')
const app = rill()
const batch = require('@rill/batch')
const loadUser = require('./load-user.js')

// Setup the middleware.
app.get('/batch', batch())

// Example routes.
app.get("/page1", ({ res })=> {
	res.body = { name: 'page1' }
})

app.get("/page2", ({ res })=> {
	res.body = { name: 'page2' }
})

somewhere.js

fetch('myapi.com/batch?a=/page1&b=/page2')
	.then((res)=> res.json())
	.then((data)=> {
		/**
		 * {
		 * 	a: { status: 200, headers: {...}, body: { name: 'page1' } }
		 * 	b: { status: 200, headers: {...}, body: { name: 'page2' } }
		 * }
		 */
	})

API

  • batch({ from: 'query' || 'body', concurrency: Infinity, forwardIP: true }) : Creates a middleware that will batch api requests.
// Handle batch requests at 'GET /batch' using request query.
app.get('/batch', batch({ from: 'query' }))

// Handle batch requests at 'POST /patch' using request body.
app.post('/batch', batch({ from: 'body' }))

// Limit concurrent batch request to 5 (default is infinity).
app.get('/batch', batch({ concurrency: 5 }))

// Disable automatic `X-Forwarded-For` header.
app.get('/batch', batch({ forwardIP: false }))

Contributions

  • Use npm test to run tests.

Please feel free to create a PR!

/@rill/batch/

    Package Sidebar

    Install

    npm i @rill/batch

    Weekly Downloads

    1

    Version

    0.4.1

    License

    MIT

    Last publish

    Collaborators

    • dylanpiercey