Building a Simple Product Listing API Client in Vue.js with TypeScript
This challenge focuses on creating a Vue.js component that acts as a client for a hypothetical product listing API. You'll be fetching data from a mock API endpoint and displaying a list of products, handling loading states and potential errors gracefully. This exercise reinforces component creation, asynchronous data fetching, and error handling within a Vue.js application using TypeScript.
Problem Description
You are tasked with building a Vue.js component named ProductList that fetches and displays a list of products from a mock API. The component should:
- Fetch Data: Use the
fetchAPI (or a library like Axios, thoughfetchis preferred for simplicity) to retrieve product data from the following endpoint:https://fakestoreapi.com/products. - Loading State: Display a loading indicator (e.g., a simple "Loading..." message) while the data is being fetched.
- Error Handling: Display an error message if the API request fails.
- Product Display: Once the data is successfully fetched, display a list of products. Each product should show its image, title, and price.
- TypeScript: Utilize TypeScript to ensure type safety throughout the component. Define appropriate types for the product data and component state.
Expected Behavior:
- Upon component mount, the component should immediately start fetching data.
- While fetching, a loading indicator should be visible.
- If the fetch is successful, the product list should be displayed.
- If the fetch fails (e.g., network error, server error), an error message should be displayed.
- The component should be reusable and adaptable to different API endpoints (though this challenge uses the provided mock endpoint).
Examples
Example 1:
Input: Initial component mount with a successful API response containing 3 products.
Output: A list displaying the image, title, and price of each of the 3 products. A loading indicator is briefly shown before the list appears.
Explanation: The component successfully fetches data and renders the product list.
Example 2:
Input: Initial component mount with a failed API request (e.g., network error).
Output: An error message indicating that the products could not be loaded. The loading indicator may briefly appear before the error message.
Explanation: The component handles the error gracefully and informs the user about the failure.
Example 3:
Input: Initial component mount with an API response containing an empty array of products.
Output: A message indicating that no products are available. The loading indicator briefly appears before the "no products" message.
Explanation: The component handles the case where the API returns an empty dataset.
Constraints
- API Endpoint: Use
https://fakestoreapi.com/productsas the API endpoint. - Data Structure: The API returns an array of product objects. Each product object has the following properties:
id(number),title(string),price(number),description(string),image(string). - Component Structure: The component should be a single, self-contained Vue.js component.
- Error Handling: Provide a user-friendly error message. Avoid displaying raw error details to the user.
- Performance: While not a primary focus, avoid unnecessary re-renders.
Notes
- Consider using
async/awaitfor cleaner asynchronous code. - Think about how to structure your component's data and methods to keep the code organized and readable.
- You can use any styling you prefer (inline styles, CSS classes, etc.). Focus on functionality over elaborate styling.
- The loading indicator can be as simple as a text message.
- This challenge is designed to test your understanding of Vue.js component creation, asynchronous data fetching, and error handling in TypeScript. Don't overcomplicate the solution; focus on meeting the core requirements.