Search⌘ K
AI Features

Solution: Adapt Configuration Sources

Understand how to create a ConfigAdapter that bridges different configuration sources by normalizing synchronous and asynchronous methods. Learn to access environment variables, JSON files, and remote APIs through a unified async interface, improving code consistency and scalability.

Solution explanation

  • Lines 2–6: We define an envProvider that reads environment variables.

    • It uses getValue(key) to return from process.env.

    • The method is synchronous but returns immediately.

  • Lines 9–14: We define a fileProvider that reads from a JSON object.

    • It uses a read(key) method that directly returns from its internal config.

    • Again, it’s synchronous.

  • Lines 17–26: We define a remoteProvider that simulates an async API.

    • It exposes fetch(key) ...