Skip to content

Commit

Permalink
feat(onretry): Add options.onRetry (#48)
Browse files Browse the repository at this point in the history
Fixes: #21
  • Loading branch information
SimenB authored and zkat committed Nov 14, 2017
1 parent cf28f83 commit f90ccff
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pooling, proxies, retries, [and more](#features)!
* [`opts.ca, opts.cert, opts.key`](#https-opts)
* [`opts.maxSockets`](#opts-max-sockets)
* [`opts.retry`](#opts-retry)
* [`opts.onRetry`](#opts-onretry)
* [`opts.integrity`](#opts-integrity)
* [Message From Our Sponsors](#wow)

Expand Down Expand Up @@ -143,6 +144,7 @@ make-fetch-happen augments the `node-fetch` API with additional features availab
* [`opts.localAddress`](#opts-local-address)
* [`opts.maxSockets`](#opts-max-sockets)
* [`opts.retry`](#opts-retry) - Request retry settings
* [`opts.onRetry`](#opts-onretry) - a function called whenever a retry is attempted
* [`opts.integrity`](#opts-integrity) - [Subresource Integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity) metadata.

#### <a name="opts-cache-manager"></a> `> opts.cacheManager`
Expand Down Expand Up @@ -363,6 +365,20 @@ fetch('http://one-more.site.com', {
})
```

#### <a name="opts-onretry"></a> `> opts.onRetry`

A function called whenever a retry is attempted.

##### Example

```javascript
fetch('https://flaky.site.com', {
onRetry() {
console.log('we will retry!')
}
})
```

#### <a name="opts-integrity"></a> `> opts.integrity`

Matches the response body against the given [Subresource Integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity) metadata. If verification fails, the request will fail with an `EINTEGRITY` error.
Expand All @@ -376,7 +392,7 @@ fetch('https://registry.npmjs.org/make-fetch-happen/-/make-fetch-happen-1.0.0.tg
integrity: 'sha1-o47j7zAYnedYFn1dF/fR9OV3z8Q='
}) // -> ok

fetch('https://malicious-registry.org/make-fetch-happen/-/make-fetch-happen-1.0.0.tgz'. {
fetch('https://malicious-registry.org/make-fetch-happen/-/make-fetch-happen-1.0.0.tgz', {
integrity: 'sha1-o47j7zAYnedYFn1dF/fR9OV3z8Q='
}) // Error: EINTEGRITY
```
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ function remoteFetch (uri, opts) {
if (!isMethodGetHead) {
return opts.cacheManager.delete(req).then(() => {
if (res.status >= 500 && req.method !== 'POST' && !isStream) {
if (typeof opts.onRetry === 'function') {
opts.onRetry(res)
}

return retryHandler(res)
}

Expand All @@ -372,6 +376,10 @@ function remoteFetch (uri, opts) {
)

if (isRetriable) {
if (typeof opts.onRetry === 'function') {
opts.onRetry(res)
}

return retryHandler(res)
}

Expand Down Expand Up @@ -439,6 +447,10 @@ function remoteFetch (uri, opts) {
throw err
}

if (typeof opts.onRetry === 'function') {
opts.onRetry(err)
}

return retryHandler(err)
})
},
Expand Down
26 changes: 26 additions & 0 deletions test/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,30 @@ test('accepts opts.retry shorthands', t => {
})
})

test('calls opts.onRetry', t => {
const srv = tnock(t, HOST)
let retryNotification = 0
let attempt = 0

srv.get('/test').delay(100).times(2).reply(200, () => {
attempt++
if (attempt >= 2) {
srv.get('/test').reply(200, CONTENT)
}
return null
})
return fetch(`${HOST}/test`, {
timeout: 50,
retry: {
retries: 2,
minTimeout: 5
},
onRetry: () => {
retryNotification++
}
}).then(() => {
t.equal(retryNotification, 2, 'onRetry was called correct number of times')
})
})

test('retries non-POST requests on ECONNRESET')

0 comments on commit f90ccff

Please sign in to comment.