then-request
Advanced tools
Comparing version 2.0.0 to 2.0.1
14
index.js
'use strict'; | ||
var assert = require('assert'); | ||
var parseUrl = require('url').parse; | ||
@@ -49,6 +50,13 @@ var Promise = require('promise'); | ||
var body = options.body ? options.body : new Buffer(0); | ||
if (typeof body === 'string') body = new Buffer(body); | ||
assert(Buffer.isBuffer(body), 'body should be a Buffer or a String'); | ||
if (!Object.keys(options.headers).some(function (name) { return name.toLowerCase() === 'content-length'; })) { | ||
options.headers['content-length'] = body.length; | ||
} | ||
var req = module.exports._request(method, url, { | ||
headers: options.headers, | ||
followRedirects: true, | ||
gzip: true, | ||
followRedirects: options.followRedirects !== false, | ||
gzip: options.gzip !== false, | ||
cache: options.cache | ||
@@ -64,3 +72,3 @@ }, function (err, res) { | ||
if (req) { | ||
req.end(options.body ? options.body : new Buffer(0)); | ||
req.end(body); | ||
} | ||
@@ -67,0 +75,0 @@ }); |
{ | ||
"name": "then-request", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "A request library that returns promises, inspired by request", | ||
@@ -8,5 +8,5 @@ "keywords": [], | ||
"dependencies": { | ||
"promise": "^5.0.0", | ||
"concat-stream": "^1.4.6", | ||
"qs": "^1.1.0", | ||
"promise": "^6.0.1", | ||
"concat-stream": "^1.4.7", | ||
"qs": "^2.3.3", | ||
"http-response-object": "^1.0.1", | ||
@@ -17,3 +17,3 @@ "http-basic": "^2.0.0" | ||
"testit": "^1.2.0", | ||
"istanbul": "^0.3.0" | ||
"istanbul": "^0.3.5" | ||
}, | ||
@@ -29,2 +29,2 @@ "scripts": { | ||
"license": "MIT" | ||
} | ||
} |
@@ -40,6 +40,8 @@ # then-request | ||
- `cache` - only used in node.js (browsers already have their own caches) Can be `'memory'`, `'file'` or your own custom implementaton (see https://github.com/ForbesLindesay/http-basic#implementing-a-cache). | ||
- `followRedirects` - defaults to `true` but can be explicitly set to `false` on node.js to prevent then-request following redirects automatically. | ||
- `gzip` - defaults to `true` but can be explicitly set to `false` on node.js to prevent then-request automatically supporting the gzip encoding on responses. | ||
**Callback / Returns:** | ||
If a callback is provided it is called with `err` and `res`. If no callback is provided, a [Promise](https://www.promisejs.org/) is returned that eventually resolves to `res`. The resulting Promise also has an additional `.getBody(encoding?)` method that is equivallent to calling `.then(function (res) { return res.getBody(); })`. | ||
If a callback is provided it is called with `err` and `res`. If no callback is provided, a [Promise](https://www.promisejs.org/) is returned that eventually resolves to `res`. The resulting Promise also has an additional `.getBody(encoding?)` method that is equivallent to calling `.then(function (res) { return res.getBody(encoding?); })`. | ||
@@ -46,0 +48,0 @@ ### Response |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var test = require('testit'); | ||
var Promise = require('promise'); | ||
@@ -10,2 +11,4 @@ test('./lib/handle-qs.js', function () { | ||
assert(handleQs('http://example.com/', {}) === 'http://example.com/'); | ||
assert(handleQs('http://example.com/?foo=bar', {}) === 'http://example.com/?foo=bar'); | ||
assert(handleQs('http://example.com/', {foo: 'bar'}) === 'http://example.com/?foo=bar'); | ||
@@ -72,4 +75,26 @@ assert(handleQs('http://example.com/', {foo: {bar: 'baz'}}) === 'http://example.com/?foo%5Bbar%5D=baz'); | ||
}); | ||
test(env + ' - Callbacks', function () { | ||
return new Promise(function (resolve, reject) { | ||
return request('GET', 'http://example.com', function (err, res) { | ||
if (err) return reject(err); | ||
assert(res.statusCode === 200); | ||
assert(res.headers['foo'] === 'bar'); | ||
assert(res.body.toString() === 'body'); | ||
resolve(null); | ||
}); | ||
}); | ||
}); | ||
test(env + ' - Callbacks', function () { | ||
return new Promise(function (resolve, reject) { | ||
return request('GET', 'http://example.com', {}, function (err, res) { | ||
if (err) return reject(err); | ||
assert(res.statusCode === 200); | ||
assert(res.headers['foo'] === 'bar'); | ||
assert(res.body.toString() === 'body'); | ||
resolve(null); | ||
}); | ||
}); | ||
}); | ||
} | ||
testEnv('browser'); | ||
testEnv('server'); |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
18234
13
336
0
73
+ Addedpromise@6.1.0(transitive)
+ Addedqs@2.4.2(transitive)
- Removedpromise@5.0.0(transitive)
- Removedqs@1.2.2(transitive)
Updatedconcat-stream@^1.4.7
Updatedpromise@^6.0.1
Updatedqs@^2.3.3