Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/astrxnomo/shop-microservers/llms.txt

Use this file to discover all available pages before exploring further.

The Cart service gives each authenticated user an isolated shopping cart backed by Redis. Every cart operation — reading, adding items, removing items, and clearing — is scoped to the user identified by the JWT submitted in the request. Redis stores each cart as a JSON-serialized array under a key equal to the user’s ID, with a rolling 7-day TTL that resets on every write. Because Redis lives entirely in memory, cart operations are fast and stateless from the perspective of the service itself.

Overview

Runtime

Express on port 3002 inside Docker. Gateway prefix: /api/cart/.

Storage

Redis — cart data stored under key userId as a JSON array. TTL: 7 days (604800 seconds).

Authentication

All endpoints require a valid JWT. The userId is extracted from the sub claim of the decoded token.

Merge Behavior

Adding an item that already exists in the cart increments the quantity rather than creating a duplicate entry.

Authentication

Every request to the Cart service must carry the JWT issued by the Auth service:
Authorization: Bearer <token>
The middleware decodes the token, extracts sub (the user ID), and uses it as the Redis key for all cart operations. Requests without a valid token receive 401 Unauthorized.

Endpoints

MethodPathAuthDescription
GET/RequiredGet current cart with items and total
POST/itemsRequiredAdd or increment an item
DELETE/items/:productIdRequiredRemove a specific item from the cart
DELETE/RequiredClear the entire cart

Cart Item Shape

When adding an item (POST /items), the request body is validated against the AddItemSchema:
export const AddItemSchema = z.object({
  productId: z.string(),
  name: z.string(),
  price: z.number().positive(),
  imageUrl: z.string().url(),
  quantity: z.number().int().positive().default(1),
});
productId
string
required
The catalog product ID. Used as the merge key — duplicate entries increment quantity instead of creating a new row.
name
string
required
Display name of the product, stored in the cart for rendering without a catalog lookup.
price
number
required
Unit price. Must be a positive number. Stored in the cart so the total can be computed independently of the catalog.
imageUrl
string
required
A valid URL for the product image.
quantity
number
default:"1"
Number of units to add. Must be a positive integer. Defaults to 1 if omitted.

Storage and TTL

Cart data is persisted in Redis with the following semantics:
const TTL_SECONDS = 60 * 60 * 24 * 7; // 7 days

await redis.set(sessionId, JSON.stringify(items), "EX", TTL_SECONDS);
  • Key: the user’s ID (extracted from JWT sub)
  • Value: JSON-serialized CartItem[]
  • TTL: reset to 7 days on every addItem or removeItem call
  • On clear: the Redis key is deleted entirely (redis.del)
A cart that has not been modified for 7 days will expire automatically. The user’s next visit will begin with an empty cart.

Merge Behavior

If a POST /items request references a productId that already exists in the cart, the quantities are summed rather than creating a duplicate entry:
const idx = items.findIndex((i) => i.productId === newItem.productId);
if (idx >= 0) {
  items[idx].quantity += newItem.quantity;
} else {
  items.push(newItem);
}

Total Calculation

The cart total is computed on read by the service layer, rounded to two decimal places:
export function calcTotal(items: CartItem[]): number {
  return (
    Math.round(items.reduce((s, i) => s + i.price * i.quantity, 0) * 100) / 100
  );
}

Response Shapes

GET / — cart with total:
{
  "success": true,
  "data": {
    "items": [
      {
        "productId": "clxyz123",
        "name": "Auriculares Inalámbricos Pro",
        "price": 89.99,
        "imageUrl": "https://images.unsplash.com/...",
        "quantity": 2
      }
    ],
    "total": 179.98
  }
}
POST /items, DELETE /items/:productId — updated items array:
{
  "success": true,
  "data": [
    {
      "productId": "clxyz123",
      "name": "Auriculares Inalámbricos Pro",
      "price": 89.99,
      "imageUrl": "https://images.unsplash.com/...",
      "quantity": 2
    }
  ]
}
DELETE / — empty cart confirmation:
{
  "success": true,
  "data": []
}

Error Cases

HTTP StatusCondition
400Request body fails Zod validation
401Missing or invalid JWT in the Authorization header

API Reference

GET /

Retrieve the current user’s cart.

POST /items

Add or increment a cart item.

DELETE /items/:productId

Remove a specific item from the cart.

DELETE /

Clear the entire cart.

Build docs developers (and LLMs) love