node-fetch with retries and timeout. Done right.
npm install node-fetch-retry-timeoutMinimalistic drop-in replacement for node-fetch. Created because other alternatives were not good for me (and believe me: I really tried hard to avoid creating yet another node-fetch extra feature package).
retryOnHttpResponse: false to disable)Differences compared to https://www.npmjs.com/package/fetch-retry :
1. YANFRT is based on node-fetch
2. More flexible retry on http response codes (not an array of codes but a callback so you can do (response.status >= 400 && response.status <= 600) || response.status == 302
Differences compared to https://www.npmjs.com/package/node-fetch-retry :
1. YANFRT has timeouts handling based on AbortController
1. Can retry based on http response statuses
beforeRetry optional callback). This allows switching broken proxies. npm i node-fetch-retry-timeoutjs
const fetch = require('node-fetch-retry-timeout')
let response = await fetch('https://google.com', {
method: 'GET',
retry: 2, // number attempts to retry
pause: 500, // pause between requests (ms)
timeout: 5000, // timeout PER 1 REQUEST (ms)
retryOnHttpResponse: r => r.status >= 500, // this is the default implementation of retryOnHttpResponse, pass false to disable
beforeRetry: (retryNum, error) => { // switch opts on retry (retryNum == 1 before first retry is initiated, check the existence of error.response for advanced logic)
return { headers: { 'Switch header': 'header-value' }, agent: SomeRandomAgent }
}
})
`Running tests
npm run test`