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

feat: support more than one digit of pagination metadata in listUsers() #793

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

makeusabrew
Copy link

What kind of change does this PR introduce?

Fixes an issue where the pagination metadata returned from listUsers() was inaccurate for page values >= 10.

What is the current behavior?

The current behaviour assumes the page value contained in the link string (each element of which looks a bit like this: </admin/users?page=7&per_page=10>; rel="last") is always a single digit, leading to truncated values for nextPage and lastPage being returned if either are 10 or more. For example, if nextPage is 10, the current behaviour truncates this to 1. If the client is relying on the value of nextPage, this could cause it to paginate up to page 9 correctly but then reset to page 1 rather than move on to page 10.

What is the new behavior?

The new behaviour correctly parses multi-digit values for nextPage and lastPage.

Additional context

I couldn't get the test suite running but didn't see any tests relating to this so I don't think any tests will break. I patched the proposed changes into the project I have which was being tripped up by this behaviour and they appear to work as expected. Feel free to refactor - wasn't sure if changing the various split() calls to a regex was a no-no.

Thanks!

links.forEach((link: string) => {
const page = parseInt(link.split(';')[0].split('=')[1].substring(0, 1))
Copy link
Author

Choose a reason for hiding this comment

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

The .substring(0, 1) here is the cause of the truncation

@hf hf changed the title fix: support more than one digit of pagination metadata in listUsers() feat: support more than one digit of pagination metadata in listUsers() Oct 25, 2023
hf
hf previously approved these changes Oct 25, 2023
Copy link
Contributor

@hf hf left a comment

Choose a reason for hiding this comment

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

Thanks for catching this! We've got an outstanding item to fix the pagination in GoTrue but it's going to be a while.

@hf hf dismissed their stale review October 25, 2023 09:52

Sorry one thing.

@@ -194,10 +194,14 @@ export default class GoTrueAdminApi {
const total = response.headers.get('x-total-count') ?? 0
const links = response.headers.get('link')?.split(',') ?? []
if (links.length > 0) {
const regex = /page=(\d+)(?=&)[^>]*>; rel="(\w+)"/
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually would you mind pulling this regex up to the top scope, so that a new object is not being created every time the function is called?

Also, is it possible to not use a regex here at all? Maybe use URLSearchParams instead?

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

2 participants