{"id":13475182,"url":"https://github.com/jonbern/fetch-retry","last_synced_at":"2025-03-26T23:30:35.409Z","repository":{"id":6135312,"uuid":"54849689","full_name":"jonbern/fetch-retry","owner":"jonbern","description":"Extend any fetch library with retry functionality.","archived":false,"fork":false,"pushed_at":"2024-07-03T18:48:04.000Z","size":257,"stargazers_count":309,"open_issues_count":3,"forks_count":57,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-23T05:37:48.016Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonbern.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-27T21:50:49.000Z","updated_at":"2025-02-17T03:00:38.000Z","dependencies_parsed_at":"2024-06-18T12:18:45.927Z","dependency_job_id":"169eb05d-29ac-428e-a153-079f1e9b5ed9","html_url":"https://github.com/jonbern/fetch-retry","commit_stats":{"total_commits":146,"total_committers":24,"mean_commits":6.083333333333333,"dds":"0.45890410958904104","last_synced_commit":"8684ef4e688375f623bd76f13add76dbc1d67cfb"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonbern%2Ffetch-retry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonbern%2Ffetch-retry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonbern%2Ffetch-retry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonbern%2Ffetch-retry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonbern","download_url":"https://codeload.github.com/jonbern/fetch-retry/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245753806,"owners_count":20666816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T16:01:18.044Z","updated_at":"2025-03-26T23:30:35.106Z","avatar_url":"https://github.com/jonbern.png","language":"JavaScript","readme":"# fetch-retry\n\nAdds retry functionality to the [Fetch](https://fetch.spec.whatwg.org/) API.\n\nIt wraps any `fetch` API package (eg: [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch), [cross-fetch](https://github.com/lquixada/cross-fetch), [isomorphic-unfetch](https://github.com/developit/unfetch), or [Node.js native's fetch implementation](https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#fetch)) and retries requests that fail due to network issues. It can also be configured to retry requests on specific HTTP status codes.\n\n[![Node.js CI](https://github.com/jonbern/fetch-retry/actions/workflows/node.js.yml/badge.svg)](https://github.com/jonbern/fetch-retry/actions/workflows/node.js.yml)\n\n## npm package\n\n```javascript\nnpm install fetch-retry --save\n```\n\n## Example\n`fetch-retry` is used the same way as `fetch`, but also accepts `retries`, `retryDelay`, and `retryOn` on the `options` object.\n\nThese properties are optional, and unless different defaults have been specified when requiring `fetch-retry`, these will default to 3 retries, with a 1000ms retry delay, and to only retry on network errors.\n\n```javascript\nconst originalFetch = require('isomorphic-fetch');\nconst fetch = require('fetch-retry')(originalFetch);\n\n// fetch-retry can also wrap Node.js's native fetch API implementation:\nconst fetch = require('fetch-retry')(global.fetch);\n```\n\n```javascript\nfetch(url, {\n    retries: 3,\n    retryDelay: 1000\n  })\n  .then(function(response) {\n    return response.json();\n  })\n  .then(function(json) {\n    // do something with the result\n    console.log(json);\n  });\n```\n\nor passing your own defaults:\n\n```javascript\nconst originalFetch = require('isomorphic-fetch');\nconst fetch = require('fetch-retry')(originalFetch, {\n    retries: 5,\n    retryDelay: 800\n  });\n```\n\n\u003e `fetch-retry` uses promises and requires you to polyfill the Promise API in order to support Internet Explorer.\n\n\n## Example: Exponential backoff\nThe default behavior of `fetch-retry` is to wait a fixed amount of time between attempts, but it is also possible to customize this by passing a function as the `retryDelay` option. The function is supplied three arguments: `attempt` (starting at 0), `error` (in case of a network error), and `response`. It must return a number indicating the delay.\n\n```javascript\nfetch(url, {\n    retryDelay: function(attempt, error, response) {\n      return Math.pow(2, attempt) * 1000; // 1000, 2000, 4000\n    }\n  }).then(function(response) {\n    return response.json();\n  }).then(function(json) {\n    // do something with the result\n    console.log(json);\n  });\n```\n\n## Example: Retry on 503 (Service Unavailable)\nThe default behavior of `fetch-retry` is to only retry requests on network related issues, but it is also possible to configure it to retry on specific HTTP status codes. This is done by using the `retryOn` property, which expects an array of HTTP status codes.\n\n```javascript\nfetch(url, {\n    retryOn: [503]\n  })\n  .then(function(response) {\n    return response.json();\n  })\n  .then(function(json) {\n    // do something with the result\n    console.log(json);\n  });\n```\n\n## Example: Retry custom behavior\nThe `retryOn` option may also be specified as a function, in which case it will be supplied three arguments: `attempt` (starting at 0), `error` (in case of a network error), and `response`. Return a truthy value from this function in order to trigger a retry, any falsy value will result in the call to fetch either resolving (in case the last attempt resulted in a response), or rejecting (in case the last attempt resulted in an error).\n\n```javascript\nfetch(url, {\n    retryOn: function(attempt, error, response) {\n      // retry on any network error, or 4xx or 5xx status codes\n      if (error !== null || response.status \u003e= 400) {\n        console.log(`retrying, attempt number ${attempt + 1}`);\n        return true;\n      }\n    })\n    .then(function(response) {\n      return response.json();\n    }).then(function(json) {\n      // do something with the result\n      console.log(json);\n    });\n```\n\n## Example: Retry custom behavior with async\nThe `retryOn` option may also be used with async and await for calling asyncronous functions:\n\n```javascript\nfetch(url, {\n    retryOn: async function(attempt, error, response) {\n      if (attempt \u003e 3) return false;\n\n      if (error !== null) {\n        var json = await response.json();\n        if (json.property !== undefined) {\n          return true;\n        }\n      }\n    })\n    .then(function(response) {\n      return response.json();\n    }).then(function(json) {\n      // do something with the result\n      console.log(json);\n    });\n```\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonbern%2Ffetch-retry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonbern%2Ffetch-retry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonbern%2Ffetch-retry/lists"}