http-enhanced
Advanced tools
Comparing version 0.5.2 to 0.5.3
28
index.js
@@ -15,2 +15,24 @@ /*jslint node: true */ /*globals setImmediate */ | ||
var ErrorResult = function(name, message) { | ||
/** Error objects do not stringify well. This wrapper tries to look mostly | ||
like an error, but responds to toString() and toJSON() better. | ||
Every Error has a .name and a .message. Anything else is optional. | ||
*/ | ||
this.name = name; | ||
this.message = message; | ||
}; | ||
ErrorResult.fromError = function(error) { | ||
var error_result = new ErrorResult(error.name, error.message); | ||
for (var key in error) { | ||
if (error.hasOwnProperty(key)) { | ||
error_result[key] = error[key]; | ||
} | ||
} | ||
return error_result; | ||
}; | ||
ErrorResult.prototype.toString = function() { | ||
return this.name + ': ' + this.message; | ||
}; | ||
// Request | ||
@@ -124,2 +146,3 @@ http.IncomingMessage.prototype.readToEnd = function(encoding, callback) { | ||
this.setHeader('Content-Type', 'application/json'); | ||
// angular.js will trim it either with or without the comma. | ||
this.end(")]}',\n" + _serialize(object)); | ||
@@ -185,3 +208,3 @@ return this; | ||
} | ||
return this.adapt(error, request_headers); | ||
return this.adapt(ErrorResult.fromError(error), request_headers); | ||
}; | ||
@@ -201,2 +224,3 @@ http.ServerResponse.prototype.adapt = function(result, request_headers) { | ||
client's preferences) | ||
TODO: support other mime types | ||
*/ | ||
@@ -208,2 +232,3 @@ // accept is (should be) a comma-separated list of mime types | ||
} | ||
// prefer JSON, if the client accepts it (see TODO in docstring) | ||
@@ -213,3 +238,2 @@ if (accept.indexOf('application/json') > -1) { | ||
} | ||
// TODO: support other mime types | ||
return this.text(result.toString()); | ||
@@ -216,0 +240,0 @@ }; |
{ | ||
"name": "http-enhanced", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "description": "Drop-in replacement for Node.js standard `http` API with various helpers.", |
19809
324