summaryrefslogtreecommitdiff
path: root/node_modules/http-response-object/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/http-response-object/index.js')
-rw-r--r--node_modules/http-response-object/index.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/node_modules/http-response-object/index.js b/node_modules/http-response-object/index.js
new file mode 100644
index 0000000..70542d4
--- /dev/null
+++ b/node_modules/http-response-object/index.js
@@ -0,0 +1,43 @@
+'use strict';
+
+module.exports = Response;
+
+/**
+ * A response from a web request
+ *
+ * @param {Number} statusCode
+ * @param {Object} headers
+ * @param {Buffer} body
+ * @param {String} url
+ */
+function Response(statusCode, headers, body, url) {
+ if (typeof statusCode !== 'number') {
+ throw new TypeError('statusCode must be a number but was ' + (typeof statusCode));
+ }
+ if (headers === null) {
+ throw new TypeError('headers cannot be null');
+ }
+ if (typeof headers !== 'object') {
+ throw new TypeError('headers must be an object but was ' + (typeof headers));
+ }
+ this.statusCode = statusCode;
+ this.headers = {};
+ for (var key in headers) {
+ this.headers[key.toLowerCase()] = headers[key];
+ }
+ this.body = body;
+ this.url = url;
+}
+
+Response.prototype.getBody = function (encoding) {
+ if (this.statusCode >= 300) {
+ var err = new Error('Server responded with status code '
+ + this.statusCode + ':\n' + this.body.toString());
+ err.statusCode = this.statusCode;
+ err.headers = this.headers;
+ err.body = this.body;
+ err.url = this.url;
+ throw err;
+ }
+ return encoding ? this.body.toString(encoding) : this.body;
+};