Angular Data Fetching and Caching Service
This challenge asks you to create a reusable Angular service responsible for fetching data from a mock API and implementing a simple caching mechanism. Building such a service is a common task in Angular development, enabling efficient data management and reducing unnecessary API calls, which is crucial for performance and user experience.
Problem Description
You need to create an Angular service named DataService that fetches data from a mock API endpoint and caches the results. The service should provide a method to retrieve data, which should first check the cache. If the data is not in the cache, it should fetch it from the API, store it in the cache, and then return it. The API endpoint will return a JSON array of objects. Each object will have an id (number) and a name (string) property.
Key Requirements:
- Data Fetching: The service must be able to fetch data from a predefined mock API endpoint.
- Caching: The service must implement a simple in-memory cache to store fetched data.
- Data Retrieval: The service must provide a method to retrieve data, checking the cache first.
- Error Handling: The service should handle potential errors during API calls (e.g., network errors) and return an appropriate error message.
- Type Safety: Use TypeScript to ensure type safety throughout the service.
Expected Behavior:
- The first time
getData()is called, it should fetch data from the API. - Subsequent calls to
getData()should return the cached data without making another API call. - If an error occurs during the API call,
getData()should return an error message.
Edge Cases to Consider:
- What happens if the API endpoint is unavailable?
- How should the cache be managed (e.g., size limits, expiration)? (For this challenge, a simple in-memory cache is sufficient; no expiration is needed.)
- How to handle potential errors during the API call?
Examples
Example 1:
Input: Initial call to getData()
Output: [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' } ]
Explanation: The API is called, and the data is fetched and cached.
Example 2:
Input: Second call to getData() (after the first call)
Output: [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' } ]
Explanation: The cached data is returned without making an API call.
Example 3: (Error Handling)
Input: API endpoint is unavailable
Output: "Error: Could not fetch data from the API."
Explanation: The API call fails, and an error message is returned.
Constraints
- API Endpoint: The mock API endpoint is
https://jsonplaceholder.typicode.com/todos. - Cache Size: The cache should be able to store at least 10 items. (This is a practical consideration, not a strict limit for this exercise.)
- Performance: The service should be reasonably performant. Avoid unnecessary computations.
- Error Handling: Handle network errors gracefully.
Notes
- You can use the
HttpClientmodule in Angular to make API calls. - Consider using an object (e.g., a JavaScript object or a
Map) to implement the cache. - Focus on the core functionality of fetching and caching data. Advanced features like cache expiration or eviction policies are not required for this challenge.
- Ensure your service is injectable into other Angular components.
- Use Observables to handle the asynchronous nature of the API calls.
- Remember to import necessary modules from
@angular/common/http.