Skip to content

Commit

Permalink
Merge pull request #83 from jonbern/feat/node_native_fetch
Browse files Browse the repository at this point in the history
Fixes tests for node 18, updates deps, adds native node example
  • Loading branch information
jonbern authored Feb 26, 2023
2 parents a3aea6e + 83e6073 commit 2ed3d78
Show file tree
Hide file tree
Showing 6 changed files with 1,551 additions and 4,409 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# fetch-retry

Adds retry functionality to the `Fetch` API.
Adds retry functionality to the [Fetch](https://fetch.spec.whatwg.org/) API.

It 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) and etc.) and retries requests that fail due to network issues. It can also be configured to retry requests on specific HTTP status codes.
It 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.

[![Build Status](https://travis-ci.org/jonbern/fetch-retry.svg?branch=master)](https://travis-ci.org/jonbern/fetch-retry)

## npm package

Expand All @@ -18,10 +17,11 @@ npm install fetch-retry --save
These 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.

```javascript
require('es6-promise').polyfill();
const originalFetch = require('isomorphic-fetch');
const fetch = require('fetch-retry')(originalFetch);

var originalFetch = require('isomorphic-fetch');
var fetch = require('fetch-retry')(originalFetch);
// fetch-retry can also wrap Node.js's native fetch API implementation:
const fetch = require('fetch-retry')(global.fetch);
```

```javascript
Expand All @@ -41,8 +41,8 @@ fetch(url, {
or passing your own defaults:

```javascript
var originalFetch = require('isomorphic-fetch');
var fetch = require('fetch-retry')(originalFetch, {
const originalFetch = require('isomorphic-fetch');
const fetch = require('fetch-retry')(originalFetch, {
retries: 5,
retryDelay: 800
});
Expand Down
6 changes: 4 additions & 2 deletions dist/fetch-retry.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.fetchRetry = factory());
}(this, (function () { 'use strict';
})(this, (function () { 'use strict';

var fetchRetry = function (fetch, defaults) {
defaults = defaults || {};
Expand Down Expand Up @@ -66,6 +66,8 @@
// eslint-disable-next-line no-undef
return new Promise(function (resolve, reject) {
var wrappedFetch = function (attempt) {
// As of node 18, this is no longer needed since node comes with native support for fetch:
/* istanbul ignore next */
var _input =
typeof Request !== 'undefined' && input instanceof Request
? input.clone()
Expand Down Expand Up @@ -146,4 +148,4 @@

return fetchRetry;

})));
}));
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ module.exports = function (fetch, defaults) {
// eslint-disable-next-line no-undef
return new Promise(function (resolve, reject) {
var wrappedFetch = function (attempt) {
// As of node 18, this is no longer needed since node comes with native support for fetch:
/* istanbul ignore next */
var _input =
typeof Request !== 'undefined' && input instanceof Request
? input.clone()
Expand Down
Loading

0 comments on commit 2ed3d78

Please sign in to comment.