0.4.1 • Published 8 years ago

@rill/batch v0.4.1

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

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!

0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago