Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Pagination helper for sync scripts #856

Closed
bastienbeurier opened this issue Jul 28, 2023 · 3 comments · Fixed by #1103
Closed

Pagination helper for sync scripts #856

bastienbeurier opened this issue Jul 28, 2023 · 3 comments · Fixed by #1103
Assignees
Labels
enhancement New feature or request

Comments

@bastienbeurier
Copy link
Member

No description provided.

@bastienbeurier bastienbeurier added the enhancement New feature or request label Jul 28, 2023
@bastienbeurier bastienbeurier moved this to Soon in Nango Roadmap Jul 28, 2023
khaliqgant added a commit that referenced this issue Aug 3, 2023
@jstorm31
Copy link

Github's octokit has nice interface for pagination including iterators. That would really improve the DX 📈

for await (const response of octokit.paginate.iterator(
  octokit.rest.issues.listForRepo,
  {
    owner: "octokit",
    repo: "rest.js",
  },
)) {
  // do whatever you want with each response, break out of the loop, etc.
}

@rguldener
Copy link
Member

Pretty elegant approach, thanks for sharing!
I like that it lets us define a default pagination strategy for the API. But I wonder of the structure could be further simplified?

If we want to keep it wrapping we could do something like this:

const requestParams = {
   endpoint: '/xyz',
   params: { 'abc': 'def' }
};
for await (const response of nango.paginate(nango.get(requestParams))) {
  // do whatever you want with each response, break out of the loop, etc.
}

But this might make changing the pagination strategy for a specific API request more difficult.

Another approach could be to build it entirely into nango.get, which could also act as an iterator if paginate is set to true:

const requestParams = {
   endpoint: '/xyz',
   params: { 'abc': 'def' },
   paginate: true
};
for await (const response of nango.get(requestParams)) {
  // do whatever you want with each response, break out of the loop, etc.
}

This last one would have the advantage that overwriting the pagination strategy for a specific endpoint is pretty easy, e.g.

const requestParams = {
   endpoint: '/xyz',
   params: { 'abc': 'def' },
   paginate: {
       strategy: nango.paginationStrategy.Cursor,
       nextCursorParamName: 'abc',
       nextCursorResponsePath: 'pages.next'
   }
};
for await (const response of nango.get(requestParams)) {
  // do whatever you want with each response, break out of the loop, etc.
}

I think this would be important because many APIs have different pagination strategies for some of their endpoints.

@jstorm31
Copy link

You're right there's a need for different pagination strategies (page, cursor,..), which Github doesn't need. Personaly, I prefer the composition approach, but without a strong opinion.

You could also add pagination params to the paginate function

const requestParams = {
   endpoint: '/xyz',
   params: { 'abc': 'def' }
};
for await (const response of nango.paginate(nango.get(requestParams), {
          strategy: nango.paginationStrategy.Cursor,
           nextCursorParamName: 'abc',
           nextCursorResponsePath: 'pages.next'
})) {
  // do whatever you want with each response, break out of the loop, etc.
}

@motnyk motnyk self-assigned this Oct 9, 2023
@bastienbeurier bastienbeurier moved this from Soon to Now in Nango Roadmap Oct 18, 2023
@khaliqgant khaliqgant assigned khaliqgant and unassigned motnyk Oct 19, 2023
@github-project-automation github-project-automation bot moved this from Now to Done in Nango Roadmap Oct 20, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants