Problem: Shape Legacy API Responses Without Touching the Source

med
30 min
Transform property names dynamically when accessing data from a legacy API object.

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 (firstNamelastName) to their legacy equivalents (fnamelname) transparently.

Constraints

  • You must use the get trap.

  • The original object must remain unchanged.

  • Unmapped properties should still return their normal values.

  • Access through either naming style (fname or firstName) 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:
Alice
Johnson
30
*/
// Accessing legacy fields directly should also still work
console.log(user.fname); // 'Alice'
console.log(user.lname); // 'Johnson'
/* Expected output:
Alice
Johnson
*/

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

med
30 min
Transform property names dynamically when accessing data from a legacy API object.

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 (firstNamelastName) to their legacy equivalents (fnamelname) transparently.

Constraints

  • You must use the get trap.

  • The original object must remain unchanged.

  • Unmapped properties should still return their normal values.

  • Access through either naming style (fname or firstName) 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:
Alice
Johnson
30
*/
// Accessing legacy fields directly should also still work
console.log(user.fname); // 'Alice'
console.log(user.lname); // 'Johnson'
/* Expected output:
Alice
Johnson
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
// Legacy API object
const legacyUser = {
fname: 'Alice',
lname: 'Johnson',
age: 30
};
// Your code here
// Example usage
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:
Alice
Johnson
30
*/
// Accessing legacy fields directly should also still work
console.log(user.fname); // 'Alice'
console.log(user.lname); // 'Johnson'
/* Expected output:
Alice
Johnson
*/