Created
May 8, 2019 20:12
-
-
Save sli/0a8d5fbd448c29a119127e629b38dd60 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const handler = { | |
get: (obj, prop) => { | |
// Call should be run when calling invoke(). | |
if (prop === 'invoke') { | |
return body => console.log(`Would call ${obj.route} with body: ${JSON.stringify(body)}`) | |
} | |
// First access sets the request method. | |
if (obj.route === null) { | |
return (new Proxy({ route: '', method: prop }, handler)) | |
} | |
// Handle everything else. | |
let newObj | |
if (typeof prop !== 'symbol') { | |
newObj = { ...obj, route: `${obj.route}/${prop}` } | |
} else { | |
newObj = { ...obj, route: obj.route } | |
} | |
return (new Proxy(newObj, handler)) | |
} | |
} | |
const api = new Proxy({ route: null }, handler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment