Skip to main content

Documentation 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.

Rather than returning every document in a single response, the API uses a lightweight offset-based pagination system. A shared 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.
// src/middleware/tools/Paginate.js
export const Paginate = (req, res, next) => {
  let perpage = req.params.perpage ? req.params.perpage : 10;
  let pag     = req.params.pag > 1 ? req.params.pag - 1 : 0;

  req.body.skippag = pag * perpage;
  req.body.limit   = perpage;

  next();
};
1

Read :perpage

If the :perpage route param is present, that value is used as the page size. Otherwise it defaults to 10.
2

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).
3

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().
4

Call next()

Control passes to the route handler, which runs the database query and appends a pagination object to the response.

Offset formula at a glance

URL page (:pag)pag (after normalise)skippag (perpage = 5)Documents returned
1 (or omitted)001 – 5
2156 – 10
321011 – 15
1094546 – 50

URL pattern

All paginated endpoints follow this structure:
POST /api/<resource>/<action>/:pag?/:perpage?
Both :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

Request
POST /api/product/get-all-product/2/5
Content-Type: application/json

{}
Response
{
  "status": 200,
  "products": [
    { "_id": "64a1f...", "title": "Slim Fit Linen Shirt", "price": 89000, "..." },
    { "_id": "64a2b...", "title": "Classic Oxford Button-Down", "price": 75000, "..." },
    { "_id": "64a3c...", "title": "Relaxed Camp Collar Shirt", "price": 95000, "..." },
    { "_id": "64a4d...", "title": "Striped Resort Shirt", "price": 82000, "..." },
    { "_id": "64a5e...", "title": "Merino Wool Dress Shirt", "price": 110000, "..." }
  ],
  "pagination": {
    "pag": 2,
    "perpage": 5,
    "pags": 10
  }
}
The 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

pagination
object
Present on every paginated response.

Paginated endpoints

All products

POST /api/product/get-all-product/:pag?/:perpage?
Returns the full product catalog, newest first.

Search products

POST /api/product/search-product/:query/:pag?/:perpage?
Full-text search filtered by :query, paginated.

My products (admin)

POST /api/product/my-products/:userId/:pag?/:perpage?
Returns products created by the admin with :userId.

All users (admin)

POST /api/user/getAll/:adminId/:pag?/:perpage?
Returns all registered user accounts. Requires admin role.

My purchases (buyer)

POST /api/carts/list-my-buy/:pag?/:perpage?
Returns the authenticated buyer’s cart/order history.

My sales (admin)

POST /api/carts/list-my-sale/:pag?/:perpage?
Returns all carts containing products owned by the authenticated admin.

Tips and edge cases

To load all results on a single page without pagination, pass a large :perpage value (e.g., 9999). Keep in mind this can be slow for large catalogs — prefer progressive loading in production UIs.
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.

Build docs developers (and LLMs) love