Skip to content
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

Pass allItems and currentItems to _pagination.paginate() #1100

Merged
merged 28 commits into from Mar 9, 2020

Conversation

jaulz
Copy link
Contributor

@jaulz jaulz commented Mar 3, 2020

Sometimes you need alltems or the items from the last response in the paginate function (e.g. to compare if current length of items are less than requested size).

Checklist

  • I have read the documentation.
  • I have included a pull request description of my changes.
  • I have included some tests.
  • If it's a new feature, I have included documentation updates.

@szmarczak
Copy link
Collaborator

e.g. to compare if current length of items are less than requested size

You can do that in a shouldContinue function. I don't know why you duplicate the all array (it's named current in your code).

@jaulz
Copy link
Contributor Author

jaulz commented Mar 3, 2020

No, I do not duplicate the all array but instead create a current array per iteration (i.e. per request). Maybe it's a bit misleading but with "current" I mean the items of the current request.

Think about this scenario:
I have an API where I can set a limit and offset but that does not return any information about whether the response contains all items or not. If I simply compare the passed limit with the returned items I can decide if a next request should be triggered or not.

@szmarczak
Copy link
Collaborator

If I simply compare the passed limit with the returned items I can decide if a next request should be triggered or not.

I don't get it. Why don't just modify the offset in _pagination.paginate and if the response contains no items, stop?

@jaulz
Copy link
Contributor Author

jaulz commented Mar 3, 2020

But that would trigger an unnecessary request wouldn't it? If the last response contains only 10 of 20 items, it would anyway try another time with an updated offset.

@szmarczak
Copy link
Collaborator

Oh, I get you now! :D

source/types.ts Outdated Show resolved Hide resolved
source/types.ts Outdated Show resolved Hide resolved
@szmarczak szmarczak changed the title feat: pass allItems and currentItems to paginate Pass allItems and currentItems to _pagination.paginate() Mar 3, 2020
jaulz and others added 3 commits March 3, 2020 15:22
Co-Authored-By: Szymon Marczak <36894700+szmarczak@users.noreply.github.com>
Co-Authored-By: Szymon Marczak <36894700+szmarczak@users.noreply.github.com>
@jaulz
Copy link
Contributor Author

jaulz commented Mar 3, 2020

Sorry, maybe I was not clear enough before 😊

@szmarczak
Copy link
Collaborator

You have nothing to be sorry for, that's the point of a conversation - to make things clear :D

test/pagination.ts Outdated Show resolved Hide resolved
test/pagination.ts Outdated Show resolved Hide resolved
jaulz and others added 3 commits March 3, 2020 15:54
Co-Authored-By: Szymon Marczak <36894700+szmarczak@users.noreply.github.com>
Co-Authored-By: Szymon Marczak <36894700+szmarczak@users.noreply.github.com>
test/pagination.ts Outdated Show resolved Hide resolved
@szmarczak
Copy link
Collaborator

Don't forget to update the docs :)

@jaulz
Copy link
Contributor Author

jaulz commented Mar 4, 2020

Here we go 😊

readme.md Outdated Show resolved Hide resolved
@jaulz
Copy link
Contributor Author

jaulz commented Mar 4, 2020

Good idea. I just added the example to the paginate function part.

readme.md Outdated
@@ -680,25 +680,27 @@ A function that transform [`Response`](#response) into an array of items. This i
###### \_pagination.paginate

Type: `Function`\
Default: [`Link` header logic](source/index.ts)
Default: `(response, allItems, currentItems) => getLinkFromHeaders(response)` (find details [here](source/index.ts))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I restored it but changed the sentence afterwards to make clear how the signature of the function should be. This should make it easier for users without diving into the code first.

readme.md Outdated

A function that returns an object representing Got options pointing to the next page. If there are no more pages, `false` should be returned.

For example, if you want to stop when the response contains less items that expected, you should use `(response, allItems, currentItems) => currentItems.length < LIMIT ? false : { url: getNextLink(response, allItems, currentItems) }`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the LIMIT should be a response.request.options.searchParams.entriesPerPage

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that expected -> than expected

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, it's updated.

@szmarczak
Copy link
Collaborator

tests are failing somehow

@jaulz
Copy link
Contributor Author

jaulz commented Mar 5, 2020

I think the issue with the tests is independently since it first arose after documentation changes. Also, it also fails for Node 13 only...

readme.md Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
test/pagination.ts Outdated Show resolved Hide resolved
@jaulz
Copy link
Contributor Author

jaulz commented Mar 5, 2020

Thanks for pushing it forward! 😊

readme.md Outdated
For example, if you want to stop when the response contains less items than expected, you can use something like this:

```js
(response, allItems, currentItems) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer the example to be fully runnable, meaning that it includes the import statement and got(…).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just extended the example.

@sindresorhus sindresorhus merged commit 1cddd52 into sindresorhus:master Mar 9, 2020
@jaulz jaulz deleted the patch-2 branch March 9, 2020 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants