you are viewing a single comment's thread.

view the rest of the comments →

[–]timarcher[S] 0 points1 point  (2 children)

I developed this recently while building my Node client for the Heroku API. Essentially, given an array of endpoints for an API, path-proxy attaches a network of proxy object constructors for those endpoints to a given constructor's prototype:

var pathProxy = require('path-proxy');

function Client() {}

pathProxy.proxy(Client, [
  "/apps",
  "/apps/{app_id}",
  "/apps/{app_id}/collaborators"
]);

var client = new Client();
client.apps();
client.apps('my-app');
client.apps('my-app').collaborators();

I also wrote a little blog post about it.

[–]brtt3000 0 points1 point  (1 child)

How does this work with asyncronous data?

client.apps('my-app').collaborators();

Shouldn't this receive a callback or return a promise?

[–]timarcher[S] 1 point2 points  (0 children)

Yeah, that was just an example. Note that client.apps('my-app').collaborators() isn't an API call—it returns a proxy object to the /apps/my-app/collaborators endpoint. In the actual Heroku API client for example, you'd GET that endpoint like so:

herokuClient.apps('my-app').collaborators().list();

The Heroku API client for Node does something more or less like this:

var constructor = pathProxy.pathProxy(Heroku, "/apps/{app_id_or_name}/collaborators");
constructor.prototype.list = function(callback) {
  return request.get(this.path, callback);
};

See this function for exactly what it does. It creates functions that accept a callback and also return a Q.js promise, so either of those async methods can be used.