...

/

Solution: Shape Legacy API Responses Without Touching the Source

Solution: Shape Legacy API Responses Without Touching the Source

Use the get trap to dynamically translate property names from legacy to modern conventions.

We'll cover the following...

Solution explanation

  • Lines 2–6: We define the legacyUser object that simulates a response from an outdated API.

    • Its keys use old naming conventions (fnamelname).

    • We’ll adapt these keys to modern equivalents without altering this object.

  • Lines 9–23: The createApiAdapter() function constructs a Proxy that provides this translation layer.

    • mapping object defines how modern names map to legacy keys.

    • The handler object contains a get() trap to intercept property access. ...