Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Expose a hasNextPage property to the list results object. #131

Merged
merged 3 commits into from
Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1389,31 +1389,22 @@ By default, all results of the first page are retrieved, and the default configu
```js
client.bucket("blog").collection("posts")
.listRecords({limit: 20})
.then(({data, next}) => {
.then(({data, hasNextPage, next}) => {
```

To retrieve the next page of results, just call `next()` from the result object obtained. If no next page is available, `next()` throws an error you can catch to exit the flow:
To check if a next page of results is available, you can check for the `hasNextPage` boolean property. To actually fetch the next page of results, call the `next()` function obtained:

```js
let getNextPage;

client.bucket("blog").collection("posts")
.listRecords({limit: 20})
.then(({data, next}) => {
console.log("Page 1", data);
getNextPage = next;
});
```

Later down the flow:

```js
getNextPage()
.then(({data, next}) => {
console.log("Page 2", data);
})
.catch(_ => {
console.log("No more pages.");
.then(({data, hasNextPage, next}) => {
if (hasNextPage) {
// resolve with data for page 2
return next().then(({data}) => data);
} else {
// resolve with data for page 1
return data;
}
});
```

Expand All @@ -1422,7 +1413,7 @@ Last, if you just want to retrieve and aggregate a given number of result pages,
```js
client.bucket("blog").collection("posts")
.listRecords({limit: 20, pages: 3})
.then(({data, next}) => ...); // A maximum of 60 results will be retrieved here
.then(({data, hasNextPage, next}) => ...); // A maximum of 60 results will be retrieved here
```

> ##### Notes
Expand Down
3 changes: 2 additions & 1 deletion src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ export default class KintoClientBase {
return {
last_modified: etag ? etag.replace(/"/g, "") : etag,
data: results,
next: next.bind(null, nextPage)
next: next.bind(null, nextPage),
hasNextPage: !!nextPage,
};
};

Expand Down
22 changes: 22 additions & 0 deletions test/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ describe("KintoClient", () => {
return api.paginatedList(path)
.should.eventually.have.property("last_modified").eql("42");
});

it("should resolve with the hasNextPage being set to false", () => {
return api.paginatedList(path)
.should.eventually.have.property("hasNextPage").eql(false);
});
});

describe("Filtering", () => {
Expand Down Expand Up @@ -693,6 +698,23 @@ describe("KintoClient", () => {
return api.paginatedList(path, {limit: 2, pages: 2})
.should.eventually.have.property("data").eql([1, 2, 3]);
});

it("should resolve with the hasNextPage being set to true", () => {
const { http } = api;
sandbox.stub(http, "request")
// settings retrieval
.onFirstCall().returns(Promise.resolve({
json: {settings: {}}
}))
// first page
.onSecondCall().returns(Promise.resolve({
headers: {get: () => "http://next-page/"},
json: {data: [1, 2]}
}));

return api.paginatedList(path)
.should.eventually.have.property("hasNextPage").eql(true);
});
});

describe("Batch mode", () => {
Expand Down
5 changes: 5 additions & 0 deletions test/integration_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ describe("Integration tests", function() {
.should.become(["b4", "b3"]);
});

it("should expose a hasNextPage boolean prop", () => {
return api.listBuckets({limit: 2})
.should.eventually.have.property("hasNextPage").eql(true);
});

it("should provide a next method to load next page", () => {
return api.listBuckets({limit: 2})
.then(res => res.next())
Expand Down