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

Missing pagination information in response #94

Closed
subashsn opened this issue May 24, 2018 · 24 comments
Closed

Missing pagination information in response #94

subashsn opened this issue May 24, 2018 · 24 comments

Comments

@subashsn
Copy link

subashsn commented May 24, 2018

When I use the Projects.all() call to list projects it by default lists up to 30 projects. But if there are more than perPage number of projects, there is no way to know based on the data returned by the function. This makes it difficult to implement pagination in UI.

The Gitlab API returns this information as part of the headers

Example

X-Next-Page: 3
X-Page: 2
X-Per-Page: 3
X-Prev-Page: 1
X-Total: 8
X-Total-Pages: 3

Am I missing something? What is the best way to get the pagination information along with the response?

Thank you :)

@jdalrymple
Copy link
Owner

The library should return all the projects, as the pagination is handled in the background. We changed it in the past to not just return 30 results since it was misleading. 30 != all lol. If it is only returning 30, then something is probably broken :( I can look into this?

@jdalrymple jdalrymple changed the title missing pagination information in response Missing pagination information in response May 24, 2018
@jdalrymple
Copy link
Owner

So i just tested this, it still returns all the results which is good. Would you like more information when using the paging options?

@subashsn
Copy link
Author

subashsn commented May 28, 2018

Hey, thanks for the reponse!

I am using the api like this

var client = new Gitlab({url: process.env.GITLAB_URL, oauthToken: accessToken})
client.Projects.all({page:page,owned:true}).then(function(projects){
...

I get a maximum of 20 per page by default(I guess limited because I am using page option). But I have no way to know if there are more projects. Due to that I can't have pagination in the application.

So it would be great if you could add another field in the response along with projects, lets say pagination which includes data at minimum from X-Total-Pages header. But it might be useful for others too to include all of the pagination headers that are returned by Gitlab.

Thanks again!

@jdalrymple
Copy link
Owner

Very doable. I'll add in an option to also retrieve the pagination information

@jdalrymple
Copy link
Owner

In this example,

var client = new Gitlab({url: process.env.GITLAB_URL, oauthToken: accessToken})
client.Projects.all({page:page,owned:true}).then(function(projects){
...

page is variable right? You can also get more results if you supply the perPage option which will give you more results per page.

@jdalrymple jdalrymple added this to the 3.4.x milestone May 28, 2018
@subashsn
Copy link
Author

Yep. page, perPage options are passed to GitLab and I am able to control the page and number of results per page.

@jdalrymple
Copy link
Owner

So ive been thinking about the cleanest way to do this but havent come up with the best solution :s. pretty much just an expanded response:

{
   data: []
   pagination: {
   }
}

Thoughts?

@subashsn
Copy link
Author

subashsn commented Jun 1, 2018

I think that should work!

So I can access the pagination data by doing something like this?

client.Projects.all({page:page,owned:true}).then((projects, pagination) => {
    var totalPages = pagination['X-Total']

It doesnt't break anyones code either. So I think this is a pretty good way to add this.

Thanks :)

@jdalrymple
Copy link
Owner

jdalrymple commented Jun 1, 2018

client.Projects.all({ page:page, owned:true, pagination: true})
.then((projects, pagination) => {
    var totalPages = pagination['X-Total']

Should I abstract out the pagination keys too?

Like:

pagination: {
  next: 4,
  page: 2,
  per: 3,
  prev: 1,
  total: 3,
}

OR

pagination: {
  nextPage: 4,
  page: 2,
  perPage: 3,
  prevPage: 1,
  totaPages: 3,
}

@subashsn
Copy link
Author

subashsn commented Jun 1, 2018

Yep, that would make it really clean!

pagination: {
  nextPage: 4,
  currentPage: 2,
  itemsPerPage: 3,
  previousPage: 1,
  totalPages: 3
}

Maybe too verbose :P But anything works, really.

Thanks :D

@jdalrymple
Copy link
Owner

Just added the functionality to the pagination branch, using this syntax:

pagination: {
  next: 4,
  current: 2,
  perPage: 3,
  previous: 1,
  total: 3,
}

@jdalrymple
Copy link
Owner

just published! Tell me if it works for you

@subashsn
Copy link
Author

subashsn commented Jun 2, 2018

Awesome! I'll test and let you know soon. Thanks a ton

@subashsn
Copy link
Author

subashsn commented Jun 5, 2018

I was not able to get this working.

This is what I tried

    client.Projects.all({page:page,owned:true,pagination:true}).then((projects,pagination) => {
        console.log(pagination)

But the pagination variable is still printing undefined :(

Thanks

@jdalrymple
Copy link
Owner

Why projects.paginatiom ?

@jdalrymple
Copy link
Owner

The return would be ({ data, pagination}) => { console.log(pagination)}

@jdalrymple
Copy link
Owner

And the option is showPagination : true. Changed it for clarity

@subashsn
Copy link
Author

subashsn commented Jun 5, 2018

Thanks, missed the showPagination part. It works now, but only when a page isn't specified.

If the page option is also specified, it just returns the data directly and doesn't respect the showPagination option.

I think this is because of !queryOptions.page && showPagination in

src/infrastructure/RequestHelper.js

  if (!queryOptions.page && showPagination) {
    return {
      data,
      pagination: {
    ....

@jdalrymple
Copy link
Owner

Perks of coding while tired smh. I'll fix it tonight sorry

@jdalrymple jdalrymple reopened this Jun 5, 2018
@subashsn
Copy link
Author

subashsn commented Jun 5, 2018

No problem! And thanks a lot :)

@jdalrymple
Copy link
Owner

Fixing it now, and adding tests so i dont make the same mistake in the future :P

@jdalrymple
Copy link
Owner

Will release today! Added tests as well.

@jdalrymple
Copy link
Owner

Oh and also note the properties have changed a bit. Check the readme :)

@subashsn
Copy link
Author

subashsn commented Jun 7, 2018

Awesome, will try once it's released. Thank you 👍

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

No branches or pull requests

2 participants