Hone logo
Hone
Problems

Implementing the useFactory Provider in Angular

Angular's dependency injection system offers several ways to provide services. The useFactory provider allows you to create a service instance using a factory function, offering flexibility when the service creation logic is complex or requires external dependencies. This challenge will guide you through implementing a service using useFactory to demonstrate its capabilities.

Problem Description

You need to create an Angular service called ApiService that fetches data from a mock API endpoint. The ApiService should have a method getData() that returns a Promise resolving to a string. The factory function should accept a token representing an environment configuration object (containing the API endpoint URL) and return an instance of ApiService configured with that URL. The environment configuration object will be injected into the factory function.

Key Requirements:

  • Create an ApiService class with a getData() method.
  • Implement a factory function that takes an environment configuration object as an argument.
  • The factory function should instantiate ApiService with the URL from the environment configuration.
  • The getData() method should simulate fetching data from the provided URL (using setTimeout for demonstration purposes).
  • The environment configuration object should have a apiUrl property.

Expected Behavior:

When getData() is called on an instance of ApiService created using useFactory, it should resolve with a string containing the URL used to create the service.

Edge Cases to Consider:

  • What happens if the environment configuration object is missing the apiUrl property? (The factory function should handle this gracefully, perhaps by providing a default URL or throwing an error). For this challenge, provide a default URL if apiUrl is missing.

Examples

Example 1:

Input:
- Environment Configuration: { apiUrl: 'https://api.example.com/data' }
- Call to ApiService.getData()
Output:
Promise resolving to "https://api.example.com/data"
Explanation: The factory function uses the provided apiUrl to create ApiService. getData() returns the apiUrl.

Example 2:

Input:
- Environment Configuration: { apiUrl: null }
- Call to ApiService.getData()
Output:
Promise resolving to "https://default-api.com/data"
Explanation: The factory function uses the default apiUrl because the provided apiUrl is null. getData() returns the apiUrl.

Example 3: (Edge Case)

Input:
- Environment Configuration: {} (empty object)
- Call to ApiService.getData()
Output:
Promise resolving to "https://default-api.com/data"
Explanation: The factory function uses the default apiUrl because the provided apiUrl is missing. getData() returns the apiUrl.

Constraints

  • The getData() method should simulate an asynchronous operation using setTimeout.
  • The factory function must accept a single argument (the environment configuration object).
  • The apiUrl property in the environment configuration object should be a string.
  • The default API URL should be "https://default-api.com/data".
  • The solution must be written in TypeScript and compatible with Angular.

Notes

  • Consider using a simple class for ApiService to keep the focus on the useFactory provider.
  • The environment configuration object can be injected using Angular's dependency injection system.
  • Focus on the factory function's logic for creating and configuring the service instance.
  • Error handling for missing apiUrl is important for robustness. Providing a default is sufficient for this challenge.
  • This challenge tests your understanding of how to use useFactory to create services with dynamic configurations.
Loading editor...
typescript