Skip to content

Instantly share code, notes, and snippets.

@deecewan
Created February 17, 2017 01:19
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 deecewan/e1411a206428518b336cd8d545f5611a to your computer and use it in GitHub Desktop.
Save deecewan/e1411a206428518b336cd8d545f5611a to your computer and use it in GitHub Desktop.
Proxies to easily make requests to an API
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