Hone logo
Hone
Problems

Building a Robust HTTP Client Service in Angular

This challenge focuses on creating a reusable HTTP client service in Angular. A well-designed HTTP client service encapsulates the logic for making HTTP requests, handling responses, and managing errors, promoting code reusability and maintainability across your application. You'll be building a service that can handle GET requests, and includes error handling and response transformation.

Problem Description

You are tasked with creating an Angular service named HttpClientService that simplifies making HTTP GET requests to a specified URL. The service should:

  1. Make GET Requests: Accept a URL as input and make a GET request to that URL using Angular's HttpClient.
  2. Handle Responses: Successfully retrieve data from the API and return it. The service should transform the response data into a JSON object.
  3. Error Handling: Gracefully handle HTTP errors (e.g., 404 Not Found, 500 Internal Server Error). The service should return an error message string in case of failure.
  4. Type Safety: Utilize TypeScript to ensure type safety throughout the service.
  5. Dependency Injection: The service should be injectable into other Angular components or services.

Expected Behavior:

  • When a valid URL is provided, the service should make a GET request, parse the JSON response, and return the parsed JSON object.
  • If the request fails (e.g., network error, server error), the service should catch the error, extract an appropriate error message, and return that message as a string.
  • The service should be reusable and easily integrated into different parts of the Angular application.

Examples

Example 1:

Input: URL = 'https://jsonplaceholder.typicode.com/todos/1'
Output: { userId: 1, id: 1, title: 'delectus aut autem', completed: false }
Explanation: A successful GET request to the provided URL returns a JSON object representing a todo item. The service parses this JSON and returns it.

Example 2:

Input: URL = 'https://jsonplaceholder.typicode.com/nonexistent'
Output: "Error: 404 Not Found"
Explanation: The URL does not exist, resulting in a 404 error. The service catches this error and returns a user-friendly error message.

Example 3:

Input: URL = 'https://httpstat.us/500'
Output: "Error: 500 Internal Server Error"
Explanation: The URL returns a 500 error. The service catches this error and returns a user-friendly error message.

Constraints

  • The service must use Angular's HttpClient for making HTTP requests.
  • The service must handle both successful responses and HTTP errors.
  • The service must return a JSON object on success and an error message string on failure.
  • The service must be written in TypeScript.
  • The service should be designed to be easily testable.
  • Assume the API always returns JSON, even on error (though the content might be an error message in JSON format).

Notes

  • Consider using the catchError operator from RxJS to handle errors effectively.
  • Think about how to provide informative error messages to the user. Extracting the status code from the error response can be helpful.
  • This challenge focuses on GET requests. Extending the service to support other HTTP methods (POST, PUT, DELETE) is a potential next step.
  • Remember to import the necessary modules from Angular (HttpClient, HttpClientModule) and RxJS. You'll need to inject HttpClient into your service.
Loading editor...
typescript