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

Enhance pagination with parsed Link header #1538

Closed
1 task
AlbertKogan opened this issue Nov 24, 2020 · 3 comments · Fixed by #1667
Closed
1 task

Enhance pagination with parsed Link header #1538

AlbertKogan opened this issue Nov 24, 2020 · 3 comments · Fixed by #1667
Labels
enhancement This change will extend Got features ✭ help wanted ✭

Comments

@AlbertKogan
Copy link

What problem are you trying to solve?

Paginating some GitHub API endpoints with extra logic (logging etc) when reaching internal limit of fetched pages.

Describe the feature

Would be nice to have more control on pagination logic. For example, parse-link-header provides well structured format of parsed Link header.

Raw Link header:

<https://<api_url>?page=3&per_page=100>; rel="next",
<https://<api_url>?page=1&per_page=100>; rel="prev"; pet="cat",
<https://<api_url>?page=5&per_page=100>; rel="last"

Parsed result:

{
  next: {
    page: '3',
    per_page: '100',
    rel: 'next',
    url: '<api_url>?page=3&per_page=100',
  },
  prev: {
    page: '1',
    per_page: '100',
    rel: 'prev',
    pet: 'cat',
    url: '<api_url>?page=1&per_page=100',
  },
  last: {
    page: '5',
    per_page: '100',
    rel: 'last',
    url: '<api_url>?page=5&per_page=100',
  },
}

If this data will be accessible, for example from pagination.shouldContinue, it will help to add custom logic based on next or previous pages.

Example

const got = require('got');
const limit = 10;

got.paginate('https://example.com/items', {
  searchParams: {
    limit,
  },
  pagination: {
    shouldContinue: (item, allItems, currentItems, pagination) => {
      if (pagination.next.page > limit) {
        log.warn('Reached page limit');
        return false;
      }
      return true;
    },
  },
});

Checklist

  • I have read the documentation and made sure this feature doesn't already exist.
@AlbertKogan AlbertKogan changed the title Enchase pagination with parsed Link header Enhance pagination with parsed Link header Nov 24, 2020
@szmarczak szmarczak added enhancement This change will extend Got features ✭ help wanted ✭ labels Nov 24, 2020
@nrajkum2-uiuc
Copy link

@szmarczak I'm interested in doing this. I suppose we'd first have to agree with the metadata of the pagination would look like. I think the schema suggested by @AlbertKogan is a fine start. Although support for something like pagination tokens could be provided as well.

@szmarczak
Copy link
Collaborator

I would prefer something like:

[
	{url: URL, rel: 'next'},
	{url: URL, rel: 'prev', pet: 'cat'},
	{url: URL, rel: 'last'}
]

@szmarczak
Copy link
Collaborator

#1667 fixes this. Got will export a function calledparseLinkHeader:

p(`<https://one.example.com>; rel="preconnect", <https://two.example.com>; rel="preconnect", <https://three.example.com>; rel="preconnect"`)
[
  {
    reference: 'https://one.example.com',
    parameters: { rel: '"preconnect"' }
  },
  {
    reference: 'https://two.example.com',
    parameters: { rel: '"preconnect"' }
  },
  {
    reference: 'https://three.example.com',
    parameters: { rel: '"preconnect"' }
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This change will extend Got features ✭ help wanted ✭
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants