Skip to content

Commit 3f2d9ed

Browse files
committed
Basic pagination
1 parent eb4c9d2 commit 3f2d9ed

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
@@ -80,6 +80,24 @@ client.events.create({
8080
});
8181
```
8282

83+
## Pagination
84+
85+
When listing, the Intercom API may return a pagination object:
86+
87+
```json
88+
{
89+
"pages": {
90+
"next": "..."
91+
}
92+
}
93+
```
94+
95+
You can grab the next page of results using the client:
96+
97+
```
98+
client.nextPage(response.pages, callback);
99+
```
100+
83101
## License
84102

85103
Apache-2.0

lib/client.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export class Client {
3232
.header('Accept', 'application/json')
3333
.end(r => f(r));
3434
}
35+
nextPage(paginationObject, f) {
36+
unirest.get(paginationObject.next)
37+
.auth(this.appId, this.appApiKey)
38+
.type('json')
39+
.header('Accept', 'application/json')
40+
.end(r => f(r));
41+
}
3542
delete(endpoint, data, f) {
3643
unirest.delete(`https://api.intercom.io${endpoint}`)
3744
.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)