Rather than returning every document in a single response, the API uses a lightweight offset-based pagination system. A sharedDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/fredy-rizo/Ecommerce/llms.txt
Use this file to discover all available pages before exploring further.
Paginate middleware reads page and page-size parameters directly from the URL, injects calculated offsets into the request, and leaves the actual database query to each route handler. This keeps pagination logic in one place and makes every paginated endpoint behave consistently.
How the Paginate middleware works
The middleware lives at src/middleware/tools/Paginate.js and runs before the route handler on any endpoint that supports paging.
Read :perpage
If the
:perpage route param is present, that value is used as the page size. Otherwise it defaults to 10.Normalise :pag to a zero-based offset multiplier
Pages are 1-indexed in the URL. The middleware subtracts 1 so that page 1 → multiplier
0, page 2 → multiplier 1, and so on. Any value ≤ 1 is treated as 0 (first page).Calculate skippag and limit
skippag = pag × perpage — the number of documents to skip in the MongoDB query, where pag is already the zero-based offset multiplier from the previous step.limit = perpage — the maximum number of documents to return.Both values are written onto
req.body so the route handler can pass them directly to .skip() and .limit().Offset formula at a glance
URL page (:pag) | pag (after normalise) | skippag (perpage = 5) | Documents returned |
|---|---|---|---|
| 1 (or omitted) | 0 | 0 | 1 – 5 |
| 2 | 1 | 5 | 6 – 10 |
| 3 | 2 | 10 | 11 – 15 |
| 10 | 9 | 45 | 46 – 50 |
URL pattern
All paginated endpoints follow this structure::pag and :perpage are optional. When omitted the API returns the first page of 10 results.
Example: fetching page 2 with 5 results per page
RequestThe
pagination.pags value is the total number of pages calculated by the route handler as Math.ceil(totalDocuments / perpage). In the example above, 10 pages × 5 per page = 50 total products.Pagination response object
Present on every paginated response.
Paginated endpoints
All products
Search products
:query, paginated.My products (admin)
:userId.All users (admin)
My purchases (buyer)
My sales (admin)
Tips and edge cases
Page numbers below
1 are treated the same as page 1 by the middleware — the pag - 1 normalisation floors at 0, so there is no risk of a negative skip offset.The
:perpage value is not capped server-side. Implement a reasonable limit in your client (e.g., max 100) to avoid large unbounded queries.