Comparing version 0.0.1 to 0.0.2
79
index.js
@@ -26,2 +26,26 @@ // The MIT License | ||
/** | ||
* Mix two objects together. | ||
* | ||
* @param {Object} target | ||
* @param {Object} source | ||
* @param {Boolean} non_agg: Aggressively mixin? | ||
*/ | ||
var mixin = function (target, source, non_agg) { | ||
var keys = Object.keys(source), | ||
key; | ||
for (var i = 0, il = keys.length; i < il; i++) { | ||
key = keys[i]; | ||
if (non_agg) { | ||
if (!Object.hasOwnProperty(target, key)) { | ||
target[key] = source[key]; | ||
} | ||
} else { | ||
target[key] = source[key]; | ||
} | ||
} | ||
}; | ||
/** | ||
* The HTTP REST Client prototype, the main export. | ||
@@ -33,8 +57,13 @@ * | ||
var Client = exports.Client = function (options) { | ||
this.secure = options.secure || false; | ||
this.host = options.host; | ||
this.port = options.port || (this.secure ? 443 : 80); | ||
this.path = options.path || '/'; | ||
this.type = options.type || ''; | ||
options = options || {}; | ||
this.host = options.host; | ||
this.secure = options.secure || false; | ||
this.port = options.port || (this.secure ? 443 : 80); | ||
this.path = options.path || '/'; | ||
this.headers = options.headers || {}; | ||
this.params = options.params; | ||
this.type = options.type; | ||
this.response = options.response; | ||
this._http = this.secure ? require('https') : require('http'); | ||
@@ -61,9 +90,16 @@ }; | ||
options.path = this.path + options.path; | ||
options.headers = options.headers || {}; | ||
options.headers = options.headers || this.headers; | ||
options.encoding = 'undefined' !== typeof options.encoding ? | ||
options.encoding : 'utf8'; | ||
options.response = options.response || this.response; | ||
if (options.params || this.params) { | ||
if (!options.params) { | ||
options.params = this.params; | ||
} | ||
if (options.params) { | ||
if ('object' === typeof options.params) { | ||
if (this.params) { | ||
mixin(options.params, this.params); | ||
} | ||
options.path += '?' + querystring.stringify(options.params); | ||
@@ -88,2 +124,6 @@ } else { | ||
options.headers['Content-Length'] = Buffer.byteLength(options.body); | ||
} else if ('DELETE' === method || | ||
'PUT' === method || | ||
'POST' === method) { | ||
options.headers['Content-Length'] = '0'; | ||
} | ||
@@ -100,2 +140,9 @@ | ||
response.setEncoding(options.encoding); | ||
} | ||
if (!options.encoding || options.stream) { | ||
if (callback) { | ||
callback(null, response, null); | ||
} | ||
} else { | ||
var body = ''; | ||
@@ -120,6 +167,2 @@ | ||
}); | ||
} else { | ||
if (callback) { | ||
callback(null, response, null); | ||
} | ||
} | ||
@@ -138,3 +181,6 @@ }); | ||
request.end(); | ||
options.end = 'undefined' === typeof options.end ? true : options.end; | ||
if (options.end) { | ||
request.end(); | ||
} | ||
@@ -145,2 +191,4 @@ return request; | ||
['GET', 'POST', 'PUT', 'DELETE'].forEach(function (verb) { | ||
var lower_verb = verb.toLowerCase(); | ||
/** | ||
@@ -153,3 +201,8 @@ * Wrappers around _request. | ||
*/ | ||
Client.prototype[verb.toLowerCase()] = function (path, options, callback) { | ||
Client.prototype[lower_verb] = function (path, options, callback) { | ||
if ('function' === typeof options) { | ||
callback = options; | ||
options = null; | ||
} | ||
options = options || {}; | ||
@@ -156,0 +209,0 @@ options.path = path; |
{ | ||
"name": "nest", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A Node HTTP client aimed at REST API's.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -28,3 +28,4 @@ var assert = require('assert'); | ||
console.log(body); | ||
assert.ok(body.repository); | ||
done(); | ||
@@ -31,0 +32,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7017
208