diff options
| author | 8cy <[email protected]> | 2020-04-03 02:37:42 -0700 |
|---|---|---|
| committer | 8cy <[email protected]> | 2020-04-03 02:37:42 -0700 |
| commit | 60867fb030bae582082340ead7dbc7efdc2f5398 (patch) | |
| tree | 4c6a7356351be2e4914e15c4703172597c45656e /node_modules/http-response-object/index.js | |
| parent | commenting (diff) | |
| download | s5nical-60867fb030bae582082340ead7dbc7efdc2f5398.tar.xz s5nical-60867fb030bae582082340ead7dbc7efdc2f5398.zip | |
2020/04/03, 02:34, v1.2.0
Diffstat (limited to 'node_modules/http-response-object/index.js')
| -rw-r--r-- | node_modules/http-response-object/index.js | 43 |
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; +}; |