Hone logo
Hone
Problems

Fetching Data with HttpClientModule in Angular

This challenge focuses on integrating and utilizing Angular's HttpClientModule to fetch data from a public API. Understanding how to make HTTP requests is fundamental to building dynamic and data-driven Angular applications. You'll be creating a simple component that retrieves and displays data from a REST API.

Problem Description

You are tasked with creating an Angular component that fetches a list of public repositories from the GitHub API and displays them on the screen. The component should:

  1. Import and Configure HttpClientModule: Ensure HttpClientModule is imported into your Angular module (e.g., AppModule) and available for use.
  2. Inject HttpClient: Inject the HttpClient service into your component.
  3. Make an HTTP GET Request: Use the HttpClient to make a GET request to the following endpoint: https://api.github.com/repositories.
  4. Handle the Response: Successfully parse the JSON response from the API. The response is an array of repository objects. Each repository object has properties like name, description, html_url, etc.
  5. Display the Data: Display the name and description of each repository in a list format within the component's template. If a repository doesn't have a description, display "No description available."
  6. Handle Errors: Implement error handling to gracefully manage potential network errors or invalid responses from the API. Display an appropriate error message to the user if something goes wrong.

Expected Behavior:

Upon component initialization, the component should make the API request. Once the data is successfully retrieved, the component should render a list of repositories, each showing its name and description. If an error occurs during the request, an error message should be displayed.

Examples

Example 1:

Input: Successful API response containing an array of repository objects.
Output: A list displaying the name and description of each repository.
Explanation: The component iterates through the array of repositories and displays the `name` and `description` properties for each.

Example 2:

Input: API response with an empty array.
Output: An empty list.
Explanation: The component iterates through the empty array and doesn't display any repositories.

Example 3:

Input: Network error or invalid API response.
Output: An error message displayed to the user, e.g., "Failed to fetch repositories. Please try again later."
Explanation: The error handling mechanism catches the error and displays a user-friendly message.

Constraints

  • API Endpoint: You must use the provided GitHub API endpoint: https://api.github.com/repositories.
  • Data Display: Only display the name and description properties of each repository.
  • Error Handling: Implement a basic error handling mechanism to catch and display errors.
  • Angular Version: Assume you are using a recent version of Angular (v14 or later).
  • Performance: The solution should be reasonably performant. Avoid unnecessary computations or DOM manipulations.

Notes

  • Remember to import HttpClientModule in your module.
  • Use the subscribe() method to handle the asynchronous HTTP response.
  • Consider using the map() operator to transform the response data if needed.
  • Error handling can be implemented using the catchError() operator.
  • The GitHub API is public and doesn't require authentication for this specific endpoint.
  • Focus on the core functionality of fetching and displaying data. Styling is not required for this challenge.
Loading editor...
typescript