@zeit/fetch-retry
Advanced tools
Comparing version 1.0.1 to 2.0.0
75
index.js
@@ -1,13 +0,1 @@ | ||
let fetch; | ||
try { | ||
fetch = require('node-fetch'); | ||
} catch (err) { | ||
if (err.code === 'MODULE_NOT_FOUND' && /node-fetch/.test(err.message)) { | ||
console.error('Could not find peer dependency `node-fetch`. ' + | ||
'Make sure it is installed: `yarn add node-fetch`'); | ||
} | ||
throw err; | ||
} | ||
const retry = require('async-retry'); | ||
@@ -21,29 +9,42 @@ const debug = require('debug')('fetch-retry'); | ||
const fetchRetry = (url, opts = {}, retryOpts) => ( | ||
retry(async (bail, attempt) => { | ||
const {method = 'GET'} = opts; | ||
try { | ||
// this will be retried | ||
const res = await fetch(url, opts); | ||
debug('status %d', res.status); | ||
if (res.status >= 500 && res.status < 600) { | ||
const err = new Error(res.statusText); | ||
err.code = err.status = err.statusCode = res.status; | ||
err.url = url; | ||
module.exports = setup; | ||
function setup(fetch) { | ||
if (!fetch) { | ||
fetch = require('node-fetch'); | ||
} | ||
function fetchRetry(url, opts = {}, retryOpts) { | ||
return retry(async (bail, attempt) => { | ||
const {method = 'GET'} = opts; | ||
try { | ||
// this will be retried | ||
const res = await fetch(url, opts); | ||
debug('status %d', res.status); | ||
if (res.status >= 500 && res.status < 600) { | ||
const err = new Error(res.statusText); | ||
err.code = err.status = err.statusCode = res.status; | ||
err.url = url; | ||
throw err; | ||
} else { | ||
return res; | ||
} | ||
} catch (err) { | ||
debug(`${method} ${url} error (${err.status}). ${attempt < MAX_RETRIES ? 'retrying' : ''}`, err); | ||
throw err; | ||
} else { | ||
return res; | ||
} | ||
} catch (err) { | ||
debug(`${method} ${url} error (${err.status}). ${attempt < MAX_RETRIES ? 'retrying' : ''}`, err); | ||
throw err; | ||
} | ||
}, retryOpts || { | ||
// timeouts will be [ 10, 50, 250 ] | ||
minTimeout: MIN_TIMEOUT, | ||
retries: MAX_RETRIES, | ||
factor: FACTOR | ||
}) | ||
); | ||
}, retryOpts || { | ||
// timeouts will be [ 10, 50, 250 ] | ||
minTimeout: MIN_TIMEOUT, | ||
retries: MAX_RETRIES, | ||
factor: FACTOR | ||
}) | ||
} | ||
module.exports = fetchRetry; | ||
for (const key of Object.keys(fetch)) { | ||
fetchRetry[key] = fetch[key]; | ||
} | ||
fetchRetry.default = fetchRetry; | ||
return fetchRetry; | ||
} |
{ | ||
"name": "@zeit/fetch-retry", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"devDependencies": { | ||
@@ -8,4 +8,8 @@ "jest": "^21.2.1", | ||
}, | ||
"peerDependencies": { | ||
"node-fetch": "*" | ||
}, | ||
"dependencies": { | ||
"async-retry": "^1.1.3" | ||
"async-retry": "^1.1.3", | ||
"debug": "^3.1.0" | ||
}, | ||
@@ -12,0 +16,0 @@ "scripts": { |
@@ -11,3 +11,4 @@ # fetch-retry | ||
```js | ||
const fetch = require('@zeit/fetch-retry'); | ||
const fetch = require('@zeit/fetch-retry')(require('node-fetch')) | ||
module.exports = async () => { | ||
@@ -19,3 +20,3 @@ const res = await fetch('http://localhost:3000') | ||
Make sure to `yarn add fetch-retry` in your main package. | ||
Make sure to `yarn add @zeit/fetch-retry` in your main package. | ||
@@ -22,0 +23,0 @@ The third optional parameter is custom [retry options](https://github.com/zeit/async-retry) |
const {createServer} = require('http'); | ||
const retryFetch = require('./index'); | ||
const retryFetch = require('./index')(); | ||
@@ -4,0 +4,0 @@ test('retries upon 500', async () => { |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
89844
6
49
3
6
+ Addeddebug@^3.1.0
+ Addeddata-uri-to-buffer@4.0.1(transitive)
+ Addeddebug@3.2.7(transitive)
+ Addedfetch-blob@3.2.0(transitive)
+ Addedformdata-polyfill@4.0.10(transitive)
+ Addedms@2.1.3(transitive)
+ Addednode-domexception@1.0.0(transitive)
+ Addednode-fetch@3.3.2(transitive)
+ Addedweb-streams-polyfill@3.3.3(transitive)