Overview
TheproductService module provides methods to interact with the product endpoints of the Villa Buena E-Commerce API. It handles fetching products, retrieving individual product details, and filtering products by category.
Base Configuration
All service methods use the configured Axios instance fromapi.js:
Methods
getAll()
Retrieves all products from the API with a limit of 200 items.Returns a promise that resolves to an array of product objects
API Endpoint
Usage Example
Response Structure
Unique identifier for the product
Product name
Product price in USD
Detailed product description
Product category name
URL to the product image
getById()
Retrieves a single product by its unique identifier.The unique identifier of the product to retrieve
Returns a promise that resolves to a single product object
API Endpoint
Usage Example
Response Structure
Returns a single product object with the same structure as described ingetAll().
If the product ID does not exist, the API will return a 404 error. Make sure to handle this case in your application.
getByCategory()
Retrieves all products belonging to a specific category.The category name to filter products by (e.g., βelectronicsβ, βjeweleryβ, βmenβs clothingβ, βwomenβs clothingβ)
Returns a promise that resolves to an array of product objects in the specified category
API Endpoint
Usage Example
Response Structure
Returns an array of product objects with the same structure as described ingetAll().
Category names are case-sensitive. Use the
categoryService.getAll() method to retrieve the list of valid category names.Implementation Details
Source Code
Location:src/services/productService.js
Key Features
- Async/Await Pattern: All methods use modern async/await syntax for cleaner asynchronous code
- Data Extraction: Destructures the
dataproperty from Axios responses automatically - Centralized Configuration: Uses the shared
apiinstance for consistent base URL and configuration - Simple Interface: Provides a clean, intuitive API for product operations
Error Handling
All methods can throw errors in the following scenarios:- Network failures
- API server errors (5xx)
- Invalid endpoints (4xx)
- Timeout errors
.catch() handlers.
Related Services
- Category Service - Manage product categories
- Base API Configuration (
src/services/api.js)
