Problem: Unify HTTP Client Responses
Problem statement
Your team uses two different HTTP clients in different services. Both expose an async get(url) method, but their response formats don’t match:
One client returns an object like
{ data, status }.The other returns just the raw response body.
When your application code consumes these responses, you end up with conditionals everywhere just to figure out how to read the data. You’ve been asked to unify them behind a single adapter interface so that the rest of the codebase can use one consistent API—regardless of which client is used underneath.
Goal
Implement an HttpClientAdapter class that wraps any HTTP client and exposes a single async method:
async get(url)
It should always return a normalized object in this shape:
{ body, statusCode }
Constraints
The adapter must work for both kinds of clients.
The adapter itself should handle the difference in response format—no conditionals in the calling code.
Do not modify the original clients.
You can use basic
ifortypeofchecks inside the adapter (for format detection).
Sample output
The examples below illustrate what the output should look like:
(async () => {const rawClient = new HttpClientAdapter(rawHttpClient);const structuredClient = new HttpClientAdapter(structuredHttpClient);console.log(await rawClient.get('example.com'));console.log(await structuredClient.get('example.com'));})();/* Expected output:{ body: 'RAW_RESPONSE_FROM_example.com', statusCode: 200 }{ body: 'BODY_FROM_example.com', statusCode: 200 }*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Unify HTTP Client Responses
Problem statement
Your team uses two different HTTP clients in different services. Both expose an async get(url) method, but their response formats don’t match:
One client returns an object like
{ data, status }.The other returns just the raw response body.
When your application code consumes these responses, you end up with conditionals everywhere just to figure out how to read the data. You’ve been asked to unify them behind a single adapter interface so that the rest of the codebase can use one consistent API—regardless of which client is used underneath.
Goal
Implement an HttpClientAdapter class that wraps any HTTP client and exposes a single async method:
async get(url)
It should always return a normalized object in this shape:
{ body, statusCode }
Constraints
The adapter must work for both kinds of clients.
The adapter itself should handle the difference in response format—no conditionals in the calling code.
Do not modify the original clients.
You can use basic
ifortypeofchecks inside the adapter (for format detection).
Sample output
The examples below illustrate what the output should look like:
(async () => {const rawClient = new HttpClientAdapter(rawHttpClient);const structuredClient = new HttpClientAdapter(structuredHttpClient);console.log(await rawClient.get('example.com'));console.log(await structuredClient.get('example.com'));})();/* Expected output:{ body: 'RAW_RESPONSE_FROM_example.com', statusCode: 200 }{ body: 'BODY_FROM_example.com', statusCode: 200 }*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.