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

@salesflare/batchelor

v2.0.4

Published

A lovely little Node.js module to perform batch requests with the Google REST API

Downloads

154

Readme

Batchelor

A lovely little Node.js module to perform batch requests with the Google REST API. Simply does it.

Google API Batch Requests

This is a project to solve a need for a missing feature in the wonderfully epic and useful google/google-api-nodejs-client. Currently, this cannot be used to post media and has not been tested with posting anything but JSON.

In theory this library could be used with other APIs, but has only been tested with Google's APIs as that's what we need it for.

Feel free to get involved in development.

Upgrading to 2.0

batch.run(...) now takes a node style callback, with an error as the first parameter and the result as the second.

INCORRECT (old, v1.0-style):

batch.run(function(result){});

CORRECT (new, v2.0-style):

batch.run(function(err, result){});

Upgrading to 1.0

The API was changed in 1.0 to move from a singleton instance to a constructor. So before where you used Batchelor directly:

var Batchelor = require('batchelor')
Batchelor.init(...)
Batchelor.add(...)
Batchelor.run(...)

You now need to create an instance of Batchelor:

var Batchelor = require('batchelor')
var batch = new Batchelor(...)
batch.add(...)
batch.run(...)

See https://github.com/wapisasa/batchelor/issues/4 for why this change was made.

Installation

This library has also been distributed on npm. Install it with the following command:

$ npm install batchelor --save

How to Use

GET Requests

var Batchelor = require('batchelor');

Once the module has been included, we initialise it with all our default options:

var batch = new Batchelor({
	// Any batch uri endpoint in the form: https://www.googleapis.com/batch/<api>/<version>
	'uri':'https://www.googleapis.com/batch/gmail/v1/',
	'method':'POST',
	'auth': {
		'bearer': [... Google API Token ...]
	},
	'headers': {
		'Content-Type': 'multipart/mixed'
	}
});

We can then start adding requests to our batch. This can be done 2 ways:

As a one-off object:

batch.add({
	'method':'GET',
	'path':'/gmail/v1/users/me/messages/123'
})

Or an Array of objects:

batch.add([
	{
		'method':'GET',
		'path':'/gmail/v1/users/me/messages/123'
	},
	{
		'method':'GET',
		'path':'/gmail/v1/users/me/messages/456'
	},
	{
		'method':'GET',
		'path':/gmail/v1/users/me/messages/789'
	}
]);

Once you have added all of the requests you need, call .run():

batch.run(function(err, response){
	if (err){
		console.log("Error: " + err.toString());
	} else {
		res.json(response);
	}
});

POST Requests

The above examples show GET requests. To perform a POST requires a few more settings:

batch.add({
	'method':'POST',
	'path':'/gmail/v1/users/me/messages/',
	'parameters':{
		'Content-Type':'application/json;',
		'body':{'object':{'originalContent': 'A wonderful batch post!'},'access': {'items': [{'type': 'domain'}],'domainRestricted': true}}
	}
});

Callbacks

By default, all responses are returned through the callback function in the batch.run() call. Alternatively, a callback can be supplied for each individual calls:

batch.add({
	'method':'POST',
	'path':'/gmail/v1/users/me/messages/',
	'parameters':{
		'Content-Type':'application/json;',
		'body':{'object':{'originalContent': 'Another wonderful batch post with callback!'},'access': {'items': [{'type': 'domain'}],'domainRestricted': true}}
	},
	'callback':function(response){
		console.log(response);
	}
});

Request and Response IDs

The module will assign a request a randomly generated unique Content-ID by default, but this can be supplied as part of the options to supply batch.add():

batch.add({
	'method':'GET',
	'path':'/gmail/v1/users/me/messages/123',
	'requestId':'Batch_UniqueID_1'
})

A Couple of Little Gifts

Method Chaining

All methods return the Batchelor instance. So you can chain calls together.

batch.add([
	...
]).run(function(err, data){
	...
});
Data Pass-through

When passing options to the .add() you can include an Object called extend. In the case of providing a callback, this will be passed back as a second parameter. When using the default callback on the .run() call, an array of all data passed through will be added as a second parameter with the requestId as the key:

batch.add({
	...
	'extend':{
		...
	},
	'callback':function(response, extendObj){
		console.log(response, extendObj);
	}
});

This could be required, for example, when making multiple requests with different Auth data and then needing to make further requests with the same Auth data.

Resetting and Re-using

Once Batchelor has been run, there are certain use-cases for running futher batch requests on response. This requires the variables in the instance to be reset. This can be done using the .reset() call:

batch.run(function(err, response){

	//	Reset Batchelor for further use
	batch.reset();
	...

});

To Do List-ish

These might get done if we end up needing them/have time:

  • Limit requests per batch request
  • Handle Media in API calls (no need for it here, feel free to write it)

Release History

  • 2.0.2 Added error handler and made headers optional
  • 2.0.1 Include response status code in error
  • 2.0.0 Bug Fixes and Improvements
  • 1.0.0 The API was changed in 1.0 to move from a singleton instance to a constructor (Thanks again to @bradvogel)
  • 0.0.8 Added support for DELETE requests (Thanks to @bradvogel)
  • 0.0.7 Reset function for when you are using batchelor more than once in a script (ability for nested requests too)
  • 0.0.6 Bug fixes introduced in the last update and clean up was happening too soon. Moved it.
  • 0.0.5 Added the ability to passthrough specific information from the .add() options to the response object
  • 0.0.4 Authorization can now be set on each request (currently Bearer [...] only)
  • 0.0.3 More bug fixes: New contentId for every request
  • 0.0.2 Bug fixes
  • 0.0.1 Initial release

Acknowledgement

Built on the clock by @jamesmhaley as an internal project of wapisasa C.I.C.