Minimalistic 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). Now with unique feature: allows switching proxies on retry!
- Default redirect strategy is 'manual' so we get 30x redirects as responses
- Response timeout is 20s
- Will consider each 50x response as a bad one and will retry (pass
retryOnHttpResponse: false
to disable)
Major differences from https://www.npmjs.com/package/@adobe/node-fetch-retry:
- In YANFRT, Retries are configured by amount of attempts and not by total allowed duration of request (more convenient when request latency is fluctuating between 1 and 20 seconds like in most of my use cases)
- One simple retry strategy: just amount of attempts and delay between requests
- No Apache OpenWhisk and no other fat
Differences compared to https://www.npmjs.com/package/fetch-retry :
- YANFRT is based on node-fetch
- 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 :
- YANFRT has timeouts handling based on AbortController
- Can retry based on http response statuses
Since version 1.2 YANFRT supports changing opts of request on each retry (via beforeRetry
optional callback). This allows switching broken proxies.
See /test/ folder ("can change agent on retry" test)
npm i node-fetch-retry-timeout
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 }
}
})
npm run test