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...
We'll cover the following...
Solution explanation
Lines 2–14: We simulate two analytics providers:
providerAthrows an error for invalid input and returns{ ok, payload }on success.providerBreturns success or failure objects instead of throwing.These two incompatible interfaces form the motivation for adapter composition.
Lines 17–36: The
ResponseAdapterconverts 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:...