Skip to content

Commit

Permalink
feat: add AllPages pagination helper
Browse files Browse the repository at this point in the history
  • Loading branch information
costela committed Feb 5, 2024
1 parent 997404b commit 9067982
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pagination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gitlab

// AllPages can be used to fetch all pages of a paginated resource, e.g.: (assuming gl is a gitlab client instance)
//
// allUsers, err := gitlab.AllPages(gl.Users.List, nil)
//
// It is also possible to specify additional pagination parameters:
//
// mrs, err := gitlab.AllPages(gl.MergeRequests.ListMergeRequests, &gitlab.ListMergeRequestsOptions{
// ListOptions: gitlab.ListOptions{
// PerPage: 100,
// Pagination: "keyset",
// OrderBy: "created_at",
// },
// })
func AllPages[O, T any](f Paginatable[O, T], opt *O) ([]*T, error) {
all := make([]*T, 0)
nextLink := ""
for {
page, resp, err := f(opt, WithKeysetPaginationParameters(nextLink))
if err != nil {
return nil, err
}
all = append(all, page...)
if resp.NextLink == "" {
break
}
nextLink = resp.NextLink
}
return all, nil
}

// Paginatable is the type that is implemented by all functions used to paginated content.
type Paginatable[O, T any] func(*O, ...RequestOptionFunc) ([]*T, *Response, error)

0 comments on commit 9067982

Please sign in to comment.