Search⌘ K
AI Features

Solution: Unify HTTP Client Responses

Explore how to implement an HttpClientAdapter in Node.js that unifies different HTTP client response formats into a consistent structure. Understand how to bridge incompatible interfaces to simplify downstream code and avoid conditional complexity when handling asynchronous client calls.

Solution explanation

  • Lines 2–12: We simulate two HTTP clients:

    • rawHttpClient.get(url) returns only a string (the raw body).

    • structuredHttpClient.get(url) returns an object with { data, status }.

    • Both have the same method name (get) but incompatible response shapes.

  • Lines 15–17: The HttpClientAdapter constructor accepts a client instance.

    • It stores the client reference.

    • This allows the same adapter class ...