Skip to content

Commit

Permalink
Merge pull request #81 from pwmckenna/override-request
Browse files Browse the repository at this point in the history
add ability to override request object
  • Loading branch information
FGRibreau committed Aug 1, 2018
2 parents c26a18c + 9e54d89 commit 99e434e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Expand Up @@ -182,6 +182,29 @@ request({
});
```

## How to access the underlying request library

You can access to the underlying `request` library thanks to `request.Request`:

```javascript
const request = require('requestretry');
console.log(request.Request); // original request library
```

Thus, if needed, it's possible to monkey-patch or extend the underlying Request library:

```javascript
request.Request = class extends request.Request {
constructor(url, options, f, retryConfig) {
super(url, options, f, retryConfig);
// this constructor will be called for every requestretry call,
// and give you global logging
console.log('Request', url, options, f, retryConfig);
}
}
```


## Modifying `request` options

You can use the `defaults` method to provide default options like so:
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -166,7 +166,7 @@ Request.prototype.abort = function () {

function Factory(url, options, f) {
var retryConfig = _.chain(_.isObject(url) ? url : options || {}).defaults(DEFAULTS).pick(Object.keys(DEFAULTS)).value();
var req = new Request(url, options, f, retryConfig);
var req = new Factory.Request(url, options, f, retryConfig);
req._tryUntilFail();
return req;
}
Expand Down
20 changes: 20 additions & 0 deletions test/request.test.js
@@ -0,0 +1,20 @@
'use strict';

var request = require('../');
var t = require('chai').assert;

describe('Request', function () {
it('should use overridden Request', function (done) {
var set = false;
request.Request = class extends request.Request {
constructor(url, options, f, retryConfig) {
super(url, options, f, retryConfig);
set = true;
}
};
request('http://www.filltext.com/?rows=1', function (err, response, body) {
t.strictEqual(set, true);
done();
});
});
});

0 comments on commit 99e434e

Please sign in to comment.