shred.js | |
---|---|
Shred is an HTTP client library intended to simplify the use of Node's built-in HTTP library. In particular, we wanted to make it easier to interact with HTTP-based APIs. See the examples for more details. | |
Ax is a nice logging library we wrote. You can use any logger, providing it
has | var Ax = require("ax")
, CookieJarLib = require( "cookiejar" )
, CookieJar = CookieJarLib.CookieJar
; |
Shred takes some options, including a logger and request defaults. | var Shred = function(options) {
options = (options||{});
this.agent = options.agent;
this.defaults = options.defaults||{};
this.log = options.logger||(new Ax({ level: "info" }));
this._sharedCookieJar = new CookieJar();
this.logCurl = options.logCurl || false;
}; |
Most of the real work is done in the request and reponse classes. |
Shred.Request = require("./shred/request");
Shred.Response = require("./shred/response");
Shred.registerProcessor = require("./shred/content").registerProcessor; |
The | Shred.prototype = {
request: function(options) {
options.logger = options.logger || this.log;
options.logCurl = options.logCurl || this.logCurl; |
allow users to set cookieJar = null | options.cookieJar = ( 'cookieJar' in options ) ? options.cookieJar : this._sharedCookieJar;
options.agent = options.agent || this.agent; |
fill in default options | for (var key in this.defaults) {
if (this.defaults.hasOwnProperty(key) && !options[key]) {
options[key] = this.defaults[key]
}
}
return new Shred.Request(options);
}
}; |
Define a bunch of convenience methods so that you don't have to include
a | "get put post delete".split(" ").forEach(function(method) {
Shred.prototype[method] = function(options) {
options.method = method;
return this.request(options);
};
});
module.exports = Shred;
|