Created
February 17, 2017 01:19
-
-
Save deecewan/e1411a206428518b336cd8d545f5611a to your computer and use it in GitHub Desktop.
Proxies to easily make requests to an API
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
function wrapInProxy(target) { | |
return new Proxy(target, { | |
get: (target, name) => { | |
target.endpoint = target.endpoint || []; | |
const fn = (data) => dispatch => dispatch(makeRequest(name, target.endpoint.join('/'), data)); | |
fn.endpoint = [...target.endpoint, name]; | |
return wrapInProxy(fn); | |
} | |
}); | |
} | |
// TODO: There has *got* to be a better way of doing this...is it too late at night? | |
export default new Proxy({}, { | |
get: (target, name) => wrapInProxy({ endpoint: [name] }) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment