Skip to content

Commit 2688dac

Browse files
committed
Merge pull request #6 from intercom/BL/page
Basic pagination
2 parents 0c597a5 + 3f2d9ed commit 2688dac

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,24 @@ client.events.create({
109109
});
110110
```
111111

112+
## Pagination
113+
114+
When listing, the Intercom API may return a pagination object:
115+
116+
```json
117+
{
118+
"pages": {
119+
"next": "..."
120+
}
121+
}
122+
```
123+
124+
You can grab the next page of results using the client:
125+
126+
```
127+
client.nextPage(response.pages, callback);
128+
```
129+
112130
## License
113131

114132
Apache-2.0

lib/client.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ export class Client {
3434
.header('Accept', 'application/json')
3535
.end(r => f(r));
3636
}
37+
nextPage(paginationObject, f) {
38+
unirest.get(paginationObject.next)
39+
.auth(this.appId, this.appApiKey)
40+
.type('json')
41+
.header('Accept', 'application/json')
42+
.end(r => f(r));
43+
}
3744
delete(endpoint, data, f) {
3845
unirest.delete(`https://api.intercom.io${endpoint}`)
3946
.auth(this.appId, this.appApiKey)

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,13 @@ describe('clients', function () {
1616
done();
1717
});
1818
});
19+
it('paginate', function (done) {
20+
nock('https://api.intercom.io').get('/foo/bar/baz').query({ blue: 'red' }).reply(200, { foo: 'bar' });
21+
let client = new Client('foo', 'bar');
22+
let paginationObject = { next: 'https://api.intercom.io/foo/bar/baz?blue=red' };
23+
client.nextPage(paginationObject, function (r) {
24+
assert.deepEqual({foo: 'bar'}, r.body);
25+
done();
26+
});
27+
});
1928
});

0 commit comments

Comments
 (0)