node-vkontakte
node-vkontakte copied to clipboard
Use OOP instead of closures.
I see your lib utilizes closures heavily just in order to make syntax shorter. But I think that instantiating a class could give cleaner code, additional easy to implement things like I mentioned in https://github.com/stevebest/node-vkontakte/pull/1#issuecomment-11366871 and compliance with the official API that uses VK.api().
When in Rome, do as Romans do. I used closures not for syntax, but because it's the way Node modules are expected to be. Well, at least Roman emper---I mean @substack writes them that way.
The substack pattern —
module.exports = function () {}
. So module should be so simple that it can be described by one function.
That said, if I wanted to try something masochistic and port official VK API to Node, I'd do it. But I don't wanna. :)
Also, https://github.com/stevebest/vk-client
Also, https://github.com/stevebest/vk-client
Thanks! That seems more convenient for me.
Aaaaand, I'm probably not going to support it. It's twice as long and doesn't even speak Node streams, bleh! :)
Regarding the style, supporting 2 api versions (full and shorthand) is still achievable:
// shorthand api
module.exports = function(key, secret) {
var instance = new Client(key, secret);
return function() {
instance.api.apply(instance, arguments);
}
}
// full api
var Client = module.exports.Client = function(key, secret) {};
Client.prototype.setRequest(req) { this.request = req; };
Client.prototype.api = function() { ... };
Is this idea clear to you? This method will preserve external simplicity but also is able expose details for those are looking for them.
This will give 2 call styles: the old one
var vk = vkontakte('', '');
vk('method', ...);
and the new:
var vk = new vkontakte.Client('', '');
vk.setRequest(request.defaults({proxy: ...}));
vk.api('method', ...);
I was thinking about making it more request
-compatible in terms of API, like
vk.defaults({proxy: ...}) // delegate `request` stuff to `request`
vk.get('method') // when you actually need a GET
vk.post(...) // support streams for e.g. uploading videos and shit
vk('method') // maybe even be smart and pick GET or POST depending on `method`?