Skip to content

Commit dcf9606

Browse files
author
James Haley
committed
Merge pull request #6 from wapisasa/v1
v1.0
2 parents e9d5da7 + 79438da commit dcf9606

File tree

2 files changed

+236
-217
lines changed

2 files changed

+236
-217
lines changed

README.md

+37-17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@ Feel free to get involved in development.
1313
## Issues, Features and Bugs
1414
This has been created for an internal project of [wapisasa](https://github.com/wapisasa) so it might not fit everyone's requirements. It might also be buggy as it's an alpha release. Any issues, feature requests or bugs that need attention please [let us know](https://github.com/wapisasa/batchelor/issues).
1515

16+
## Upgrading to 1.0
17+
18+
The API was changed in 1.0 to move from a singleton instance to a constructor. So before where you used `Batchelor` directly:
19+
20+
``` node
21+
var Batchelor = require('batchelor')
22+
Batchelor.init(...)
23+
Batchelor.add(...)
24+
Batchelor.run(...)
25+
```
26+
27+
You now need to create an instance of Batchelor:
28+
29+
``` node
30+
var Batchelor = require('batchelor')
31+
var batch = new Batchelor(...)
32+
batch.add(...)
33+
batch.run(...)
34+
```
35+
1636
## Installation
1737

1838
This library has also been distributed on `npm`. Install it with the following command:
@@ -21,14 +41,16 @@ This library has also been distributed on `npm`. Install it with the following c
2141
$ npm install batchelor --save
2242
```
2343

44+
See <https://github.com/wapisasa/batchelor/issues/4> for why this change was made.
45+
2446
## How to Use
2547
#### GET Requests
2648
``` node
2749
var Batchelor = require('batchelor');
2850
```
2951
Once the module has been included, we initialise it with all our default options:
3052
``` node
31-
Batchelor.init({
53+
var batch = new Batchelor({
3254
'uri':'https://www.googleapis.com/batch',
3355
'method':'POST',
3456
'auth': {
@@ -43,14 +65,14 @@ We can then start adding requests to our batch. This can be done 2 ways:
4365

4466
As a one-off object:
4567
``` node
46-
Batchelor.add({
68+
batch.add({
4769
'method':'GET',
4870
'path':'/plusDomains/v1/people/me/activities/user'
4971
})
5072
```
5173
Or an Array of objects:
5274
``` node
53-
Batchelor.add([
75+
batch.add([
5476
{
5577
'method':'GET',
5678
'path':'/plusDomains/v1/people/me/activities/user'
@@ -67,14 +89,14 @@ Batchelor.add([
6789
```
6890
Once you have added all of the requests you need, call `.run()`:
6991
``` node
70-
Batchelor.run(function(response){
92+
batch.run(function(response){
7193
res.json(response);
7294
});
7395
```
7496
#### POST Requests
7597
The above examples show `GET` requests. To perform a `POST` requires a few more settings:
7698
``` node
77-
Batchelor.add({
99+
batch.add({
78100
'method':'POST',
79101
'path':'/plusDomains/v1/people/me/activities',
80102
'parameters':{
@@ -84,9 +106,9 @@ Batchelor.add({
84106
});
85107
```
86108
#### Callbacks
87-
By default, all responses are returned through the callback function in the `Batchelor.run()` call. Alternatively, a callback can be supplied for each individual calls:
109+
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:
88110
``` node
89-
Batchelor.add({
111+
batch.add({
90112
'method':'POST',
91113
'path':'/plusDomains/v1/people/me/activities',
92114
'parameters':{
@@ -99,22 +121,20 @@ Batchelor.add({
99121
});
100122
```
101123
#### Request and Response IDs
102-
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 `Batchelor.add()`:
124+
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()`:
103125
``` node
104-
Batchelor.add({
126+
batch.add({
105127
'method':'GET',
106128
'path':'/plusDomains/v1/people/me/activities/user',
107129
'requestId':'Batch_UniqueID_1'
108130
})
109131
```
110132
#### A Couple of Little Gifts
111133
###### Method Chaining
112-
All methods return the `Batchelor` object. So you can chain calls together.
134+
All methods return the `Batchelor` instance. So you can chain calls together.
113135

114136
``` node
115-
Batchelor.init({
116-
...
117-
}).add([
137+
batch.add([
118138
...
119139
]).run(function(data){
120140
...
@@ -123,7 +143,7 @@ Batchelor.init({
123143
###### Data Pass-through
124144
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:
125145
``` node
126-
Batchelor.add({
146+
batch.add({
127147
...
128148
'extend':{
129149
...
@@ -136,12 +156,12 @@ Batchelor.add({
136156
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.
137157

138158
###### Resetting and Re-using
139-
Once Batchelor has been run, there are certain use-cases for running futher batch requests on response. This requires the variables in the module to be reset. This can be done using the `.reset()` call:
159+
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:
140160
``` node
141-
Batchelor.run(function(response){
161+
batch.run(function(response){
142162

143163
// Reset Batchelor for further use
144-
Batchelor.reset();
164+
batch.reset();
145165
...
146166

147167
});

0 commit comments

Comments
 (0)