I have a service that must receive pagnation queries in offset format, receiving offset and limit parameters. For example, if I receive offset=5&limit=10, I would expect to receive items 5-14 back. I am able to enforce some validation against these parameters, for example to set a maximum value for limit.
My data source must receive pagination requests in page number format, receiving page_number and page_size parameters. For example, if I send page_number=0&page_size=20, I would receive items 0-19. The data source has a maximum page_size of 100.
I need to be able to take the offset pagination parameters I receive and use them to determine appropriate values for the page_number and page_size parameters, in order to return a range from the data source that includes all of the items I need. Additional items may be returned padding out the start and/or end of the range, which can then be filtered out to produce the requested range.
If possible, I should only make a single request to the data source. Optionally, performance can be improved by minimising the size of the range to be requested from the datasource (i.e. fetching 10 items to satisfy a request for 8 items is more efficient than requesting 100 items for the same).
This feels like it should be relatively simple to achieve, but my attempts at simple mathematical solutions don't address all of the edge cases, and my attempts at more robust solutions have started to head into the more complex space of calculating and iterating over factors etc.
Is there a simple way to calculate the appropriate values?