{"id":13455382,"url":"https://github.com/sjinks/node-fetch-retry-ts","last_synced_at":"2025-04-10T00:40:59.316Z","repository":{"id":36973898,"uuid":"265975060","full_name":"sjinks/node-fetch-retry-ts","owner":"sjinks","description":"Adds retry functionality to fetch()","archived":false,"fork":false,"pushed_at":"2025-04-08T03:15:13.000Z","size":2890,"stargazers_count":7,"open_issues_count":5,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T00:40:55.165Z","etag":null,"topics":["fetch","fetch-api","node","nodejs","retry","retry-request"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/fetch-retry-ts","language":"TypeScript","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/sjinks.png","metadata":{"funding":{"custom":["https://www.paypal.com/donate/?hosted_button_id=SAG6877JDJ3KU","https://send.monobank.ua/jar/7rosVfiwKM"]},"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":"2020-05-21T23:42:10.000Z","updated_at":"2025-04-07T22:18:33.000Z","dependencies_parsed_at":"2025-03-17T10:00:35.107Z","dependency_job_id":null,"html_url":"https://github.com/sjinks/node-fetch-retry-ts","commit_stats":{"total_commits":448,"total_committers":5,"mean_commits":89.6,"dds":0.6272321428571428,"last_synced_commit":"26178c419fcc76af68a7a396216135a0e83c2a21"},"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjinks%2Fnode-fetch-retry-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjinks%2Fnode-fetch-retry-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjinks%2Fnode-fetch-retry-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjinks%2Fnode-fetch-retry-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sjinks","download_url":"https://codeload.github.com/sjinks/node-fetch-retry-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137997,"owners_count":21053775,"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":["fetch","fetch-api","node","nodejs","retry","retry-request"],"created_at":"2024-07-31T08:01:04.673Z","updated_at":"2025-04-10T00:40:59.297Z","avatar_url":"https://github.com/sjinks.png","language":"TypeScript","funding_links":["https://www.paypal.com/donate/?hosted_button_id=SAG6877JDJ3KU","https://send.monobank.ua/jar/7rosVfiwKM"],"categories":["TypeScript"],"sub_categories":[],"readme":"# fetch-retry-ts\n\n![Build and Test CI](https://github.com/sjinks/node-fetch-retry-ts/workflows/Build%20and%20Test%20CI/badge.svg)\n\nAdds retry functionality to `fetch()`.\n\n## Installation\n\n```bash\nnpm install fetch-retry-ts\n```\n\n## Usage\n\n```typescript\nimport originalFetch from 'isomorphic-fetch';\nimport fetchBuilder from 'fetch-retry-ts';\n\nconst options = {\n    retries: 3,\n    retryDelay: 1000,\n    retryOn: [419, 503, 504],\n};\n\nconst fetch = fetchBuilder(originalFetch, options);\n\nfetch('https://example.com/').then(/* ... */);\n```\n\n`fetch-retry-ts` exports a function, which is used to build the new `fetch()`-compatible function supporting the retry logic.\nIt accepts two arguments:\n  * `fetch` (required): the original `fetch()` function (from `isomorphic-fetch` etc)\n  * `defaults` (optional): default values for the retry logic:\n    * `retries?: number`: number of attempts to make (3 by default);\n    * `retryDelay?: number | () =\u003e number | (attempt: number, error: Error | null, response: Response | null) =\u003e number`: delay between attempts (in ms). If specified as a function, the function accepts the following parameters:\n      * `attempt`: the number of the current attempt;\n      * `error`: `Error` object coming from `fetch()` when it rejects on a network failure, or `null`;\n      * `response`: `Response` or `null` if `error !== null`\n    It should return an integer, which is treated as the delay in ms before the enxt attempt is made. The default value for `retryDelay` is `1000`.\n    * `retryOn?: number[] | (attempt: number, retries: number, error: Error | null, response: Response | null) =\u003e boolean`: if specified as an array of integers, it is treated as a list of HTTP codes which trigger retry. When specified as a function, that functoin accepts the same parameters as the one described in `retryDelay`, and an additional parameter called `retries`, whcih is the number of configured retries. The function should return a truthy value if the request should be retried. *If `retryOn` is a function, `retries` is ignored.* The default value for `retryOn` in `[429, 503, 504]`.\nIt returns a function to be used instead of `fetch()`.\n\nThe returned function accepts the same arguments as `fetch(input: RequestInfo, init?: RequestInit)`, and three additional properties in `init` object. Those are `retries`, `retryDelay`, and `retryOn`.\n\n## Examples\n\n### Retry on any 5xx Error\n\n```typescript\nfetch(url, {\n    retryOn: (attempt: number, retries: number, error: Error | null, response: Response | null): boolean =\u003e (\n        attempt \u003c retries \u0026\u0026 (!!error || !response || response.status \u003e= 500)\n    ),\n}).then(/* ... */)\n```\n\n### Retry only on Network Failures\n\n```typescript\nfetch(url, {\n    retryOn: [],\n}).then(/* ... */)\n```\n\n### Do not retry on Network Failures\n\n```typescript\nfetch(url, {\n    retryOn: (attempt: number, retries: number, error: Error | null, response: Response | null): boolean =\u003e (\n        attempt \u003c retries \u0026\u0026 error === null /* \u0026\u0026 additional logic to check response code */\n    ),\n}).then(/* ... */)\n```\n\n### Exponential Backoff\n\n```typescript\nfetch(url, {\n    retryDelay: (attempt: number, error: Error | null, response: Response | null): number =\u003e (\n        Math.pow(2, attempt) * 1000\n    ),\n}).then(/* ... */)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsjinks%2Fnode-fetch-retry-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsjinks%2Fnode-fetch-retry-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsjinks%2Fnode-fetch-retry-ts/lists"}