fetch-retry icon indicating copy to clipboard operation
fetch-retry copied to clipboard

ENHANCEMENT: Some way to add different url sources?

Open federicosan opened this issue 4 years ago • 1 comments

I am integrating payments for a site and I need to fetch the dollar price for ARS, I need this to make the payment. I am fetching this from a national bank's API but it sometimes fails so I made my own fetch retry, and then started researching for it. I am used to making bots so I need a fault-tolerant fetch, a fetch that will always give you what you need, maybe by providing different source APIs, it would retry or switch until it gets a useful result. I am still trying to understand your code, it seems you are using some knowledge on how fetch retries by itself? I can't see the code looping or increasing attempts.

federicosan avatar Aug 16 '19 19:08 federicosan

Maybe this could be handled in a fetch-alt bundle instead? It could wrap this bundle and if this bundle fails with its retry-strategies the fetch-alt bundle tries the next (randomised) url?

export default function fetch(fetch) {
  return function(urls: string[], isFail: (response: Response) => boolean, options) {
    return urls.reduce(function(result, url) {
      return result.then(isFail)
    }, Promise.resolve())
  }
}
const fetch = require("fetch-alt")("@vercel/fetch-retry")("node-fetch")

fetch([url1,url2,url3], function isFail(response) {
  return response.status === 418
})

pke avatar Sep 18 '21 21:09 pke