-
-
Notifications
You must be signed in to change notification settings - Fork 935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pagination support #722
Comments
What about: const options = {
hooks: {
pagination: (response, options) => {
if (!response) {
// First request
options.searchParams = {page: 0};
}
options.searchParams.page++;
}
}
};
for await (const response of got.paginate(url, options)) {
// ...
} |
Shouldn't the hook be called |
I have no idea. Maybe it should be an option (
The response could give a link to the next page.
Sure, that would be nice. |
It should at least be called
Yeah, maybe drop the hook aspect from it altogether? I wonder if we could make it |
That sounds good. Let's do it that way :) |
Some other use-cases:
|
I would also include a |
const arrayOfResponses = await got.pagination(url, {
...options,
pagination: {
paginate: (previousResponse, options) => {
// By default this would be the Link-header thing...
},
from: 1, // Default = 1
to: 4 // If specified, it will return an array.
}
}); I'm not sure if it should be implemented. You can achieve the same in a few lines of code: const arrayOfResponses = await Promise.all([
got(url, {searchParams: {page: 1}}),
got(url, {searchParams: {page: 2}}),
got(url, {searchParams: {page: 3}}),
got(url, {searchParams: {page: 4}})
]);
Not possible. The Got logic is async. |
For my primary use case, I simply want all paginated results with a single got command, e.g. something like For my secondary use case, I would like to add a limit to how many results are returned, e.g. maximum 1000. If you prefer that the caller calculate this instead using number of pages returned, it's also OK. But I think users usually think in terms of number of results, not number of pages. The number of returned entries per page is usually an endpoint restriction and users ultimately care about total entries, not total pages. The only downside might be if the number of entries per page is not divisible by the total and hence you need to crop the results. |
👍 This is my experience too. I take back wanting to specify pages. What I actually want is to be able to specify item count. |
Sure, but you can achieve pagination in Got today too, it's just verbose. The point of this issue is figuring out the most common use-case, and making those super easy, while still giving user's the power to do custom logic. |
I don't understand this statement. Got being async is exactly what makes this possible. Got would handle getting the required pages by using got(…, {
pagination: {
itemCount: 100,
filter: item => item.isUnicorn
}
}); In the above example, Got would fetch items and paginate until it had gathered 100 items with |
Can you elaborate on:
What do you mean specifically? |
Instead of: const results = [];
for await (const response of got.paginate(…)) {
results.concat(response.items);
} I could just do something like this: const results = await got(…, {
pagination: true
}); And it would gather all the items for me. |
Oh. I thought that by That just returns an array, right? |
Yes |
@IssueHunt has funded $80.00 to this issue.
|
Unfortunately my use cases typically revolve around querying slow services, so I benefit from processing each paged result as they're received instead of waiting for I strongly agree with the switch to thinking about results in terms of items and not pages. All I care about is receiving all results that match my URL query. Whether the server uses pagination or not, doesn't really matter to me. |
@hutson We could have a |
@sindresorhus has rewarded $72.00 to @szmarczak. See it on IssueHunt
|
What would you like to discuss?
With the recent enhancements such as create/extend and hooks, is there any clean way to implement pagination with the current approach?
Note: there exists a discussion in
gh-got
already but I want to discuss it generically here.Checklist
IssueHunt Summary
szmarczak has been rewarded.
Backers (Total: $80.00)
Submitted pull Requests
Tips
IssueHunt has been backed by the following sponsors. Become a sponsor
The text was updated successfully, but these errors were encountered: