Problem: Compose Nested Adapters
Problem statement
Your system integrates with two different analytics providers. Both send event data and return responses, but in inconsistent ways:
Provider A returns
{ ok: true, payload: {...} }.Provider B returns
{ success: true, data: {...} }.
Even worse, each one handles errors differently:
Provider A throws regular JavaScript
Errorobjects.Provider B returns an object like
{ success: false, message: "Invalid key" }instead of throwing.
You need to unify both the response format and the error handling so that all analytics calls look and behave consistently.
Goal
Implement two adapters:
ResponseAdapter: Normalizes different response shapes into{ success, result }.ErrorAdapter: Wraps another adapter and ensures all errors are thrown as standardErrorobjects.
Then, compose them into a single adapter that exposes:
async sendEvent(event)
Which always:
Returns
{ success, result }on success.Throws an
Erroron failure, regardless of provider.
Constraints
Do not modify the original providers.
The
ErrorAdaptershould wrap theResponseAdapter.Both adapters must be independent and composable.
The final composed adapter should be created explicitly (not via inheritance).
Sample output
The examples below illustrate what the output should look like:
(async () => {const unifiedA = new ErrorAdapter(new ResponseAdapter(providerA));const unifiedB = new ErrorAdapter(new ResponseAdapter(providerB));try {console.log(await unifiedA.sendEvent({ apiKey: 'abc' }));console.log(await unifiedB.sendEvent({ apiKey: 'abc' }));await unifiedA.sendEvent({});} catch (err) {console.error('Error:', err.message);}})();/* Expected output:{ success: true, result: { id: 1, name: 'A' } }{ success: true, result: { id: 2, name: 'B' } }Error: Missing API key*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Compose Nested Adapters
Problem statement
Your system integrates with two different analytics providers. Both send event data and return responses, but in inconsistent ways:
Provider A returns
{ ok: true, payload: {...} }.Provider B returns
{ success: true, data: {...} }.
Even worse, each one handles errors differently:
Provider A throws regular JavaScript
Errorobjects.Provider B returns an object like
{ success: false, message: "Invalid key" }instead of throwing.
You need to unify both the response format and the error handling so that all analytics calls look and behave consistently.
Goal
Implement two adapters:
ResponseAdapter: Normalizes different response shapes into{ success, result }.ErrorAdapter: Wraps another adapter and ensures all errors are thrown as standardErrorobjects.
Then, compose them into a single adapter that exposes:
async sendEvent(event)
Which always:
Returns
{ success, result }on success.Throws an
Erroron failure, regardless of provider.
Constraints
Do not modify the original providers.
The
ErrorAdaptershould wrap theResponseAdapter.Both adapters must be independent and composable.
The final composed adapter should be created explicitly (not via inheritance).
Sample output
The examples below illustrate what the output should look like:
(async () => {const unifiedA = new ErrorAdapter(new ResponseAdapter(providerA));const unifiedB = new ErrorAdapter(new ResponseAdapter(providerB));try {console.log(await unifiedA.sendEvent({ apiKey: 'abc' }));console.log(await unifiedB.sendEvent({ apiKey: 'abc' }));await unifiedA.sendEvent({});} catch (err) {console.error('Error:', err.message);}})();/* Expected output:{ success: true, result: { id: 1, name: 'A' } }{ success: true, result: { id: 2, name: 'B' } }Error: Missing API key*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.