...

/

Solution: Compose Nested Adapters

Solution: Compose Nested Adapters

Compose two adapters—one for response normalization and one for error unification—into a single, resilient analytics interface.

We'll cover the following...

Solution explanation

  • Lines 2–14: We simulate two analytics providers:

    • providerA throws an error for invalid input and returns { ok, payload } on success.

    • providerB returns success or failure objects instead of throwing.

    • These two incompatible interfaces form the motivation for adapter composition.

  • Lines 17–36: The ResponseAdapter converts both providers’ response formats to a unified shape.

    • If the response has ok, it maps { ok, payload } → { success, result }.

    • If the response has success, it maps { success, data } → { success, result }.

    • If the response indicates failure (success: ...