amrnn90 / laravel-cursor-paginator

Cursor pagination for Laravel
MIT License
18 stars 6 forks source link

Support for base64 encoded cursor #5

Closed jaiselrahman closed 4 years ago

jaiselrahman commented 4 years ago

Allow encoded cursor #4 by setting 'encode_cursor' => true in config file.

Then we can use url query like http://127.0.0.1:8000/api/posts?cursor=eyJhZnRlcl9pIjoiMjAxOS0xMC0yMiAwODowMDo1NSJ9 which will provide following response:

  {
       "data": [
            {
               ...
            }
        ],
        "per_page": 1,
        "total": 2,
        "next_item": {
           ...
        },
        "current_page": "eyJhZnRlcl9pIjoiMjAxOS0xMC0yMiAwODowMDo1NSJ9",
        "first_page": "eyJhZnRlcl9pIjoiMjAxOS0xMC0yMiAwODowMDo1NSJ9",
        "last_page": "eyJiZWZvcmVfaSI6IjIwMTktMTAtMjIgMDg6MDA6NTIifQ",
        "next_page": "eyJhZnRlciI6IjIwMTktMTAtMjIgMDg6MDA6NTUifQ",
        "previous_page": null,
        "first_page_url": "http://127.0.0.1:8000/api/posts?cursor=eyJhZnRlcl9pIjoiMjAxOS0xMC0yMiAwODowMDo1NSJ9",
        "last_page_url": "http://127.0.0.1:8000/api/posts?cursor=eyJiZWZvcmVfaSI6IjIwMTktMTAtMjIgMDg6MDA6NTIifQ",
        "next_page_url": "http://127.0.0.1:8000/api/posts?cursor=eyJhZnRlciI6IjIwMTktMTAtMjIgMDg6MDA6NTUifQ",
        "prev_page_url": null,
        "path": "http://127.0.0.1:8000/api/posts"
  }

We can change the url query parameter by setting 'encoded_cursor_name' => 'page-id' which produces the url: http://127.0.0.1:8000/api/posts?page-id=eyJhZnRlcl9pIjoiMjAxOS0xMC0yMiAwODowMDo1NSJ9

amrnn90 commented 4 years ago

Looking sweet :) Can you please add a couple of tests in MacrotTest.php asserting that the output is correct when setting encode_cursor => true or when changing encoded_cursor_name in config? Something like this existing test:

https://github.com/amrnn90/laravel-cursor-paginator/blob/e17425aa1b4922a258de92baeecd0c0b3e3f19e9/tests/MacroTest.php#L101-L120

jaiselrahman commented 4 years ago

I have added tests in both MacroTest.php and CursorPaginatorTest.php. Please check whether it is enough.

amrnn90 commented 4 years ago

Thank you @jaiselrahman