request-promise
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -1,1 +0,3 @@ | ||
module.exports = require('./lib/errors.js'); | ||
'use strict'; | ||
module.exports = require('@request/promise-core/errors'); |
180
lib/rp.js
'use strict'; | ||
var Bluebird = require('bluebird'), | ||
assign = require('lodash/assign'), | ||
forEach = require('lodash/forEach'), | ||
isFunction = require('lodash/isFunction'), | ||
isPlainObject = require('lodash/isPlainObject'), | ||
isString = require('lodash/isString'), | ||
isUndefined = require('lodash/isUndefined'), | ||
keys = require('lodash/keys'), | ||
errors = require('./errors.js'); | ||
configure = require('@request/promise-core/configure/request2'), | ||
stealthyRequire = require('stealthy-require')(require); | ||
try { | ||
// Load Request freshly - so that users can require an unaltered request instance! | ||
var request = (function () { | ||
// Load Request freshly - so that users can require an unaltered request instance! | ||
var request = stealthyRequire('request'); | ||
function clearCache() { | ||
forEach(keys(require.cache), function (key) { | ||
delete require.cache[key]; | ||
}); | ||
} | ||
var temp = assign({}, require.cache); | ||
clearCache(); | ||
var freshRequest = require('request'); | ||
clearCache(); | ||
assign(require.cache, temp); | ||
return freshRequest; | ||
})(); | ||
var defaultTransformations = { | ||
HEAD: function (body, response, resolveWithFullResponse) { | ||
return resolveWithFullResponse ? response : response.headers; | ||
} | ||
}; | ||
function RP$callback(err, response, body) { | ||
/* jshint validthis:true */ | ||
var self = this; | ||
var origCallbackThrewException = false, thrownException; | ||
if (isFunction(self._rp_callbackOrig)) { | ||
try { | ||
self._rp_callbackOrig.apply(self, arguments); | ||
} catch (e) { | ||
origCallbackThrewException = true; | ||
thrownException = e; | ||
} | ||
} | ||
if (err) { | ||
self._rp_reject(new errors.RequestError(err, self._rp_options, response)); | ||
} else if (self._rp_options.simple && !(/^2/.test('' + response.statusCode))) { | ||
if (isFunction(self._rp_options.transform)) { | ||
(new Bluebird(function (resolve) { | ||
resolve(self._rp_options.transform(body, response, self._rp_options.resolveWithFullResponse)); // transform may return a Promise | ||
})) | ||
.then(function (transformedResponse) { | ||
self._rp_reject(new errors.StatusCodeError(response.statusCode, body, self._rp_options, transformedResponse)); | ||
}) | ||
.catch(function (err) { | ||
self._rp_reject(new errors.TransformError(err, self._rp_options, response)); | ||
}); | ||
} else { | ||
self._rp_reject(new errors.StatusCodeError(response.statusCode, body, self._rp_options, response)); | ||
} | ||
} else { | ||
if (isFunction(self._rp_options.transform)) { | ||
(new Bluebird(function (resolve) { | ||
resolve(self._rp_options.transform(body, response, self._rp_options.resolveWithFullResponse)); // transform may return a Promise | ||
})) | ||
.then(function (transformedResponse) { | ||
self._rp_resolve(transformedResponse); | ||
}) | ||
.catch(function (err) { | ||
self._rp_reject(new errors.TransformError(err, self._rp_options, response)); | ||
}); | ||
} else if (self._rp_options.resolveWithFullResponse) { | ||
self._rp_resolve(response); | ||
} else { | ||
self._rp_resolve(body); | ||
} | ||
} | ||
if (origCallbackThrewException) { | ||
throw thrownException; | ||
} | ||
} catch (err) { | ||
/* istanbul ignore next */ | ||
var EOL = require('os').EOL; | ||
/* istanbul ignore next */ | ||
console.error(EOL + '###' + EOL + '### The "request" library is not installed automatically anymore.' + EOL + '### But required by "request-promise".' + EOL + '###' + EOL + '### npm install request --save' + EOL + '###' + EOL); | ||
/* istanbul ignore next */ | ||
throw err; | ||
} | ||
var originalInit = request.Request.prototype.init; | ||
request.Request.prototype.init = function RP$initInterceptor(options) { | ||
configure({ | ||
request: request, | ||
PromiseImpl: Bluebird, | ||
expose: [ | ||
'then', | ||
'catch', | ||
'finally', | ||
'promise' | ||
] | ||
}); | ||
var self = this; | ||
// Init may be called again - currently in case of redirects | ||
if (isPlainObject(options) && self._callback === undefined && self._rp_promise === undefined) { | ||
self._rp_promise = new Bluebird(function (resolve, reject) { | ||
self._rp_resolve = resolve; | ||
self._rp_reject = reject; | ||
}); | ||
self._rp_callbackOrig = self.callback; | ||
self.callback = RP$callback; | ||
if (isString(options.method)) { | ||
options.method = options.method.toUpperCase(); | ||
} | ||
options.transform = options.transform || defaultTransformations[options.method]; | ||
self._rp_options = options; | ||
self._rp_options.simple = options.simple === false ? false : true; | ||
self._rp_options.resolveWithFullResponse = options.resolveWithFullResponse === true ? true : false; | ||
} | ||
return originalInit.apply(self, arguments); | ||
request.bindCLS = function RP$bindCLS() { | ||
throw new Error('CLS support was dropped. To get it back read: https://github.com/request/request-promise/wiki/Getting-Back-Support-for-Continuation-Local-Storage'); | ||
}; | ||
function expose(methodToExpose, exposeAs) { | ||
exposeAs = exposeAs || methodToExpose; | ||
/* istanbul ignore if */ | ||
if (!isUndefined(request.Request.prototype[exposeAs])) { | ||
throw new Error('Unable to expose method "' + exposeAs + '". It is already implemented by Request. Please visit https://github.com/request/request-promise/wiki/Troubleshooting'); | ||
} | ||
request.Request.prototype[exposeAs] = function RP$exposed() { | ||
return this._rp_promise[methodToExpose].apply(this._rp_promise, arguments); | ||
}; | ||
} | ||
expose('then'); | ||
expose('catch'); | ||
expose('finally'); | ||
request.Request.prototype.promise = function RP$promise() { | ||
return this._rp_promise; | ||
}; | ||
/* istanbul ignore next */ // Function covered but not seen by Instanbul. | ||
request.bindCLS = function RP$bindCLS(ns) { | ||
require('cls-bluebird')(ns); | ||
}; | ||
module.exports = request; |
{ | ||
"name": "request-promise", | ||
"version": "3.0.0", | ||
"description": "The world-famous HTTP client 'Request' now Promises/A+ compliant. Powered by Bluebird.", | ||
"version": "4.0.0", | ||
"description": "The simplified HTTP request client 'request' with Promise support. Powered by Bluebird.", | ||
"keywords": [ | ||
@@ -12,48 +12,48 @@ "xhr", | ||
"then", | ||
"thenable" | ||
"thenable", | ||
"bluebird" | ||
], | ||
"homepage": "https://github.com/request/request-promise", | ||
"bugs": { | ||
"url": "https://github.com/request/request-promise/issues" | ||
"main": "./lib/rp.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/gulp ci", | ||
"test-publish": "./node_modules/.bin/gulp ci-no-cov", | ||
"publish-please": "publish-please", | ||
"prepublish": "publish-please guard" | ||
}, | ||
"license": "MIT", | ||
"author": "Ty Abonil", | ||
"contributors": [ | ||
"Nicolai Kamenzky (https://github.com/analog-nico)" | ||
], | ||
"main": "./lib/rp.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/request/request-promise.git" | ||
"url": "git+https://github.com/request/request-promise.git" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/.bin/gulp ci" | ||
"author": "Nicolai Kamenzky (https://github.com/analog-nico)", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/request/request-promise/issues" | ||
}, | ||
"homepage": "https://github.com/request/request-promise#readme", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"dependencies": { | ||
"bluebird": "^3.3", | ||
"lodash": "^4.6.1", | ||
"@request/promise-core": "^1.0.0", | ||
"bluebird": "^3.4.1", | ||
"stealthy-require": "^0.1.0" | ||
}, | ||
"peerDependencies": { | ||
"request": "^2.34" | ||
}, | ||
"devDependencies": { | ||
"body-parser": "1.15.x", | ||
"chai": "3.5.x", | ||
"chai-as-promised": "5.2.x", | ||
"chalk": "1.1.x", | ||
"cheerio": "0.20.x", | ||
"cls-bluebird": "^1.0.1", | ||
"continuation-local-storage": "^3.1.4", | ||
"event-stream": "3.3.x", | ||
"gulp": "3.9.x", | ||
"gulp-coveralls": "0.1.x", | ||
"gulp-istanbul": "0.10.x", | ||
"gulp-jshint": "2.0.x", | ||
"gulp-mocha": "2.2.x", | ||
"jshint": "2.9.x", | ||
"jshint-stylish": "2.1.x", | ||
"rimraf": "2.5.x", | ||
"run-sequence": "1.1.x" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"body-parser": "~1.15.2", | ||
"chai": "~3.5.0", | ||
"chalk": "~1.1.3", | ||
"gulp": "~3.9.1", | ||
"gulp-coveralls": "~0.1.4", | ||
"gulp-eslint": "~2.1.0", | ||
"gulp-istanbul": "~1.0.0", | ||
"gulp-mocha": "~2.2.0", | ||
"lodash": "~4.13.1", | ||
"publish-please": "~2.1.4", | ||
"request": "^2.34.0", | ||
"rimraf": "~2.5.3", | ||
"run-sequence": "~1.2.2" | ||
} | ||
} |
<a href="http://promisesaplus.com/"> | ||
<img src="https://promises-aplus.github.io/promises-spec/assets/logo-small.png" | ||
align="right" valign="top" alt="Promises/A+ logo" /> | ||
<img src="https://promises-aplus.github.io/promises-spec/assets/logo-small.png" align="right" alt="Promises/A+ logo" /> | ||
</a> | ||
@@ -9,14 +8,23 @@ | ||
[![Gitter](https://img.shields.io/badge/gitter-join_chat-blue.svg?style=flat-square)](https://gitter.im/request/request-promise?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
| ||
[![Build Status](https://img.shields.io/travis/request/request-promise/master.svg?style=flat-square)](https://travis-ci.org/request/request-promise) | ||
[![Coverage Status](https://img.shields.io/coveralls/request/request-promise.svg?style=flat-square)](https://coveralls.io/r/request/request-promise) | ||
[![Dependency Status](https://img.shields.io/david/request/request-promise.svg?style=flat-square)](https://david-dm.org/request/request-promise) | ||
[![Dependency Status](https://img.shields.io/gemnasium/request/request-promise.svg?style=flat-square)](https://gemnasium.com/github.com/request/request-promise) | ||
[![Known Vulnerabilities](https://snyk.io/test/npm/request-promise/badge.svg?style=flat-square)](https://snyk.io/test/npm/request-promise) | ||
The world-famous HTTP client "Request" now Promises/A+ compliant. Powered by Bluebird. | ||
The simplified HTTP request client 'request' with Promise support. Powered by Bluebird. | ||
[Bluebird](https://github.com/petkaantonov/bluebird) and [Request](https://github.com/mikeal/request) are pretty awesome, but I found myself using the same design pattern. Request-Promise adds a Bluebird-powered `.then(...)` method to Request call objects. By default, http response codes other than 2xx will cause the promise to be rejected. This can be overwritten by setting `options.simple` to `false`. | ||
[Request](https://github.com/request/request) and [Bluebird](https://github.com/petkaantonov/bluebird) are pretty awesome, but I found myself using the same design pattern. Request-Promise adds a Bluebird-powered `.then(...)` method to Request call objects. By default, http response codes other than 2xx will cause the promise to be rejected. This can be overwritten by setting `options.simple = false`. | ||
Also check out the new libraries that are **very similar to `request-promise` v4**: | ||
- [`request-promise-native`](https://github.com/request/request-promise-native) v1 – Does not depend on Bluebird and uses native ES6 promises instead. | ||
- [`request-promise-any`](https://github.com/request/request-promise-any) v1 – Allows you to register any Promise library supported by [`any-promise`](https://www.npmjs.com/package/any-promise). | ||
--- | ||
## Migration from v3 to v4 | ||
1. `request` became a peer dependency. Thus make sure that `request` is installed into your project as a direct dependency. (`npm install --save request`) | ||
2. Continuation Local Storage is no longer supported. However, you [can get back the support](https://github.com/request/request-promise/wiki/Getting-Back-Support-for-Continuation-Local-Storage) by using `request-promise-any`. | ||
3. When you migrated your `transform` function to v3 and had to add `if (!(/^2/.test('' + response.statusCode))) { return resolveWithFullResponse ? response : body; }` you may now set the option `transform2xxOnly = true` instead. | ||
## Migration from v2 to v3 | ||
@@ -36,7 +44,8 @@ | ||
``` bash | ||
npm install request-promise | ||
``` | ||
npm install --save request | ||
npm install --save request-promise | ||
``` | ||
Request-Promise depends on loosely defined versions of Request and Bluebird. If you want to use specific versions of those modules please install them beforehand. | ||
`request` is defined as a peer-dependency and thus has to be installed separately. | ||
@@ -107,6 +116,8 @@ ## Cheat Sheet | ||
Set `option.body` to your data and `json: true` to encode the body as JSON. See below for HTML forms. | ||
``` js | ||
var options = { | ||
method: 'POST', | ||
uri: 'http://posttestserver.com/post.php', | ||
uri: 'http://api.posttestserver.com/post', | ||
body: { | ||
@@ -129,2 +140,4 @@ some: 'payload' | ||
Set `option.form` to your data to encode the body the same way as HTML forms do. | ||
``` js | ||
@@ -206,5 +219,6 @@ var options = { | ||
- Plus some additional options: | ||
- `simple` which is a boolean to set whether status codes other than 2xx should also reject the promise | ||
- `resolveWithFullResponse` which is a boolean to set whether the promise should be resolve with the full response or just the response body | ||
- `simple = true` which is a boolean to set whether status codes other than 2xx should also reject the promise | ||
- `resolveWithFullResponse = false` which is a boolean to set whether the promise should be resolved with the full response or just the response body | ||
- `transform` which takes a function to transform the response into a custom value with which the promise is resolved | ||
- `transform2xxOnly = false` which is a boolean to set whether the transform function is applied to all responses or only to those with a 2xx status code | ||
@@ -467,2 +481,4 @@ The objects returned by request calls like `rp(...)` or e.g. `rp.post(...)` are regular Promises/A+ compliant promises and can be assimilated by any compatible promise library. | ||
You may set `options.transform2xxOnly = true` to only execute the transform function for responses with a 2xx status code. For other status codes – independent of any other settings, e.g. `options.simple` – the transform function is not executed. | ||
#### Error handling | ||
@@ -491,24 +507,4 @@ | ||
Continuation Local Storage (CLS) is a great mechanism for backpacking data along asynchronous call chains that is best explained in [these slides](http://fredkschott.com/post/2014/02/conquering-asynchronous-context-with-cls/). If you want to use CLS you need to install the [continuation-local-storage package](https://www.npmjs.com/package/continuation-local-storage) and the [cls-bluebird package](https://www.npmjs.com/package/cls-bluebird). | ||
Continuation Local Storage is no longer supported. However, you [can get back the support](https://github.com/request/request-promise/wiki/Getting-Back-Support-for-Continuation-Local-Storage) by using `request-promise-any`. | ||
Just call `rp.bindCLS(ns)` **ONCE** before your first request to activate CLS: | ||
``` js | ||
var rp = require('request-promise'); | ||
var cls = require('continuation-local-storage'); | ||
var ns = cls.createNamespace('testNS'); | ||
rp.bindCLS(ns); | ||
ns.run(function () { | ||
ns.set('value', 'hi'); | ||
rp('http://google.com') | ||
.then(function () { | ||
console.log(ns.get('value')); // -> hi | ||
}); | ||
}); | ||
``` | ||
Since the [cls-bluebird package](https://www.npmjs.com/package/cls-bluebird) currently is just a quick and dirty implementation the CLS support is only experimental. | ||
## Debugging | ||
@@ -579,2 +575,10 @@ | ||
- v4.0.0 (2016-07-15) | ||
- **Breaking Change**: `request` is declared as a peer dependency which has to be installed separately by the user now | ||
- **Breaking Change**: Dropped support for Continuation Local Storage since [`request-promise-any`](https://github.com/request/request-promise-any) can be [used](https://github.com/request/request-promise/wiki/Getting-Back-Support-for-Continuation-Local-Storage) for that now | ||
- Introduced the `transform2xxOnly` option to ease the breaking change regarding the new `transform` handling in v3.0.0 | ||
*(Thanks to @stevage for pointing out the effect of the breaking change in [issue #131](https://github.com/request/request-promise/issues/131))* | ||
- Resolved issues [#65](https://github.com/request/request-promise/issues/65) and [#71](https://github.com/request/request-promise/issues/71) by publishing nearly identical libraries to support other Promise implementations: [`request-promise-native`](https://github.com/request/request-promise-native) and [`request-promise-any`](https://github.com/request/request-promise-any) | ||
*(Thanks to @benjamingr, @eilgin, @gillesdemey, @hildjj, @iggycoloma, @jonathanong, @knpwrs, @MarkHerhold, @massimocode, @mikeal, @niftylettuce, @raitucarp, @sherdeadlock, @tonylukasavage, and @vgoloviznin for the valuable discussions!)* | ||
- Relicensed this library with the ISC license | ||
- v3.0.0 (2016-04-16) | ||
@@ -640,4 +644,6 @@ - **Breaking Change**: Overhauled the handling of the `transform` function | ||
## MIT Licensed | ||
## License (ISC) | ||
See the [LICENSE file](LICENSE) for details. | ||
In case you never heard about the [ISC license](http://en.wikipedia.org/wiki/ISC_license) it is functionally equivalent to the MIT license. | ||
See the [LICENSE file](LICENSE) for details. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
13
640
31249
4
6
31
1
+ Added@request/promise-core@^1.0.0
+ Addedstealthy-require@^0.1.0
+ Added@request/promise-core@1.1.0(transitive)
+ Addedstealthy-require@0.1.0(transitive)
- Removedlodash@^4.6.1
- Removedrequest@^2.34
Updatedbluebird@^3.4.1