Problem: Shape Legacy API Responses Without Touching the Source
Problem statement
Your application consumes data from an old API that still uses inconsistent field names, such as fname and lname, instead of modern conventions like firstName and lastName.
You can’t modify the legacy source object directly because another system owns it, but you want the rest of your codebase to access it using consistent naming. You’ll use a Proxy to act as a translation layer—intercepting property reads and returning the correct value regardless of which version of the property name is used.
Goal
Create a function createApiAdapter(legacyObj) that returns a Proxy. The Proxy should map modern property names (firstName, lastName) to their legacy equivalents (fname, lname) transparently.
Constraints
You must use the
gettrap.The original object must remain unchanged.
Unmapped properties should still return their normal values.
Access through either naming style (
fnameorfirstName) should work.
Sample output
The examples below illustrate what the output should look like:
const user = createApiAdapter(legacyUser);console.log(user.firstName); // should return 'Alice'console.log(user.lastName); // should return 'Johnson'console.log(user.age); // should still return 30/* Expected output:AliceJohnson30*/// Accessing legacy fields directly should also still workconsole.log(user.fname); // 'Alice'console.log(user.lname); // 'Johnson'/* Expected output:AliceJohnson*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Shape Legacy API Responses Without Touching the Source
Problem statement
Your application consumes data from an old API that still uses inconsistent field names, such as fname and lname, instead of modern conventions like firstName and lastName.
You can’t modify the legacy source object directly because another system owns it, but you want the rest of your codebase to access it using consistent naming. You’ll use a Proxy to act as a translation layer—intercepting property reads and returning the correct value regardless of which version of the property name is used.
Goal
Create a function createApiAdapter(legacyObj) that returns a Proxy. The Proxy should map modern property names (firstName, lastName) to their legacy equivalents (fname, lname) transparently.
Constraints
You must use the
gettrap.The original object must remain unchanged.
Unmapped properties should still return their normal values.
Access through either naming style (
fnameorfirstName) should work.
Sample output
The examples below illustrate what the output should look like:
const user = createApiAdapter(legacyUser);console.log(user.firstName); // should return 'Alice'console.log(user.lastName); // should return 'Johnson'console.log(user.age); // should still return 30/* Expected output:AliceJohnson30*/// Accessing legacy fields directly should also still workconsole.log(user.fname); // 'Alice'console.log(user.lname); // 'Johnson'/* Expected output:AliceJohnson*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.