UNPKG

caravan

Version:

throttle REST request client

164 lines (148 loc) 4.63 kB
// Generated by CoffeeScript 1.10.0 var Caravan, Q, merge, superagent, throat, verbs, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; Q = require('q'); throat = (require('throat'))(Q.Promise); superagent = require('superagent'); merge = require('merge'); verbs = ['checkout', 'connect', 'copy', 'delete', 'get', 'head', 'lock', 'merge', 'mkactivity', 'mkcol', 'move', 'm-search', 'notify', 'options', 'patch', 'post', 'propfind', 'proppatch', 'purge', 'put', 'report', 'search', 'subscribe', 'trace', 'unlock', 'unsubscribe']; Caravan = (function() { function Caravan() { this.getBody = bind(this.getBody, this); this.settle = bind(this.settle, this); this.requestAsync = bind(this.requestAsync, this); this.request = bind(this.request, this); this.verb = bind(this.verb, this); var i, len, verb; for (i = 0, len = verbs.length; i < len; i++) { verb = verbs[i]; this[verb] = this.verb.bind(this, verb.toUpperCase()); } } Caravan.prototype.verb = function(verb, urls, options) { if (options == null) { options = {}; } options = JSON.parse(JSON.stringify(options)); options.method = verb; return this.request(urls, options); }; Caravan.prototype.request = function(urls, options) { var concurrency, promises, url; if (options == null) { options = {}; } options = JSON.parse(JSON.stringify(options)); if (options.concurrency == null) { options.concurrency = 1; } concurrency = throat(options.concurrency); if (!(urls instanceof Array)) { urls = [urls]; } promises = (function() { var i, len, results1; results1 = []; for (i = 0, len = urls.length; i < len; i++) { url = urls[i]; results1.push((function(_this) { return function(url) { return concurrency(function() { return _this.requestAsync(url, options); }); }; })(this)(url)); } return results1; }).call(this); return this.settle(promises, options); }; Caravan.prototype.requestAsync = function(url, options) { var ref; if (options == null) { options = {}; } options = JSON.parse(JSON.stringify(options)); if (options.method == null) { options.method = 'GET'; } if (typeof url === 'object') { options = merge(options, url); url = (ref = options.url) != null ? ref : options.uri; } options.delay = options.delay | 0; if (!url) { return Q.reject(new TypeError('url/uri is not defined')); } return new Q.Promise((function(_this) { return function(resolve, reject, notify) { var key, request, value; request = superagent(options.method, url); for (key in options) { value = options[key]; if (key === 'headers') { key = 'set'; } if (key === 'data') { key = 'send'; } if (typeof request[key] !== 'function') { continue; } request[key](value); } return request.end(function(error, response) { notify(error != null ? error : _this.getBody(response, options)); return setTimeout(function() { if (!error) { return resolve(response); } else { return reject(error); } }, options.delay); }); }; })(this)); }; Caravan.prototype.settle = function(promises, options) { if (options == null) { options = {}; } if (options.raw == null) { options.raw = false; } return Q.allSettled(promises).then((function(_this) { return function(results) { var i, len, result, results1; results1 = []; for (i = 0, len = results.length; i < len; i++) { result = results[i]; if (result.state === 'fulfilled') { results1.push(_this.getBody(result.value, options)); } else { results1.push(result.reason); } } return results1; }; })(this)); }; Caravan.prototype.getBody = function(response, options) { var hasKeys; if (options == null) { options = {}; } hasKeys = (Object.keys(response.body)).length > 0; switch (false) { case !options.raw: return response; case !hasKeys: return response.body; default: return response.text; } }; return Caravan; })(); module.exports = new Caravan; module.exports.Caravan = Caravan;