coderoyalty / pseudonym-app

An anonymous messaging application
https://pseudonym-app.vercel.app
MIT License
2 stars 0 forks source link

[API] Incorrect Pagination Data in Response #22

Closed coderoyalty closed 8 months ago

coderoyalty commented 8 months ago

The current behavior of the API response includes pagination data that might lead to confusion. When querying messages for a specific user with an invalid page parameter, the response still provides pagination information that suggests the validity of the page. For example, querying /users/{userId}/messages?size=10&page=5 returns a response indicating a prev value of 4, even though the 4th page is also invalid. This inconsistency in the pagination data could lead to misunderstandings and incorrect assumptions about the available pages.

Proposed Solution: Update the API response logic to provide accurate pagination data. If the requested page is invalid, ensure that the pagination data reflects this by setting prev and next values appropriately.

coderoyalty commented 8 months ago

API endpoint: /users/{userId}/messages Pagination parameters: size=10&page=5 Response data:

{
   "data": [],
   "count": 0,
   "total": 9,
   "next": null,
   "prev": 4
}

The pagination information provided in the response suggests the validity of both the current and the previous pages.

Here's how to figure out if the pagination information is valid:

total = no. of messages
page = the provided previous page
size = the maximum number of messages to be received per page
no. of possible pages = Math.floor(total / size) + 1 (ranging from 1..infinity)

if no. of possible pages is greater than ${page}
  the page is valid
else
 the page is invalid

Proposed Resolution: When the current page is out of the page range, the API should gracefully resolve the problem by setting the prev attribute of the pagination information to the maximum page available. It'll also be helpful to indicate this decision in the response.