Skip to content

Instantly share code, notes, and snippets.

@sli
Created May 8, 2019 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sli/0a8d5fbd448c29a119127e629b38dd60 to your computer and use it in GitHub Desktop.
Save sli/0a8d5fbd448c29a119127e629b38dd60 to your computer and use it in GitHub Desktop.
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