API Integration
The Iquea frontend communicates with the backend through a type-safe REST API client located insrc/api/. All API modules use a shared client with automatic JWT authentication.
API Architecture
Base Client
Shared HTTP client with auth headers
Service Modules
Domain-specific API functions
Type Safety
TypeScript interfaces for all requests/responses
JWT Auth
Automatic token injection from localStorage
Base API Client
The foundation of all API calls is theapiFetch function in client.ts:
src/api/client.ts
Key Features
- Automatic Auth
- Generic Types
- Error Handling
The client automatically includes JWT tokens from localStorage:If a token exists, it’s added as
Authorization: Bearer <token>.Authentication API
Handles user login and registration.src/api/auth.ts
Type Definitions
src/types/index.ts
Usage Example
src/pages/Login.tsx
Products API
Fetches product data with various filtering options.src/api/productos.ts
API Endpoints
| Function | Method | Endpoint | Description |
|---|---|---|---|
getProductos() | GET | /productos | Get all products |
getProducto(id) | GET | /productos/{id} | Get single product |
getDestacados() | GET | /productos/destacados | Get featured products |
buscarProductos(texto) | GET | /productos/buscar?nombre={texto} | Search by name |
getProductosPorCategoria(id) | GET | /productos/categoria/{id} | Filter by category |
getProductosPorPrecio(min, max) | GET | /productos/precio?min={min}&max={max} | Filter by price range |
Product Type
src/types/index.ts
Usage Example
src/pages/ProductList.tsx
Categories API
Fetches product categories.src/api/categorias.ts
Orders API
Handles order creation and management (requires authentication).src/api/pedidos.ts
Order Types
src/types/index.ts
Checkout Flow Example
src/pages/Cart.tsx
Error Handling
All API functions can throw errors that should be caught:- Try-Catch Pattern
- With Loading State
- Auth Error Handling
JWT Token Flow
The authentication flow works as follows:Token Storage
API Best Practices
Always Use Type Parameters
Always Use Type Parameters
Specify the expected return type for type safety:
URL Encoding
URL Encoding
Always encode user input in URLs:
Handle All States
Handle All States
Manage loading, error, and success states:
Avoid Race Conditions
Avoid Race Conditions
Use cleanup functions in useEffect:
Configuration
The base API URL is hardcoded inclient.ts:
Related Documentation
State Management
See how AuthContext manages JWT tokens
Backend API
Full backend API endpoint documentation