Socket
Socket
Sign inDemoInstall

fetch-retry

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-retry - npm Package Compare versions

Comparing version 4.1.1 to 5.0.0

.husky/pre-commit

4

index.d.ts

@@ -18,3 +18,3 @@ /// <reference lib="dom" />

interface IRequestInitWithRetry extends RequestInit {
export interface RequestInitWithRetry extends RequestInit {
retries?: number;

@@ -25,4 +25,4 @@ retryDelay?: number | RequestDelayFunction;

function fetchBuilder(fetch: typeof _fetch, defaults?: object): ((input: RequestInfo, init?: IRequestInitWithRetry) => Promise<Response>);
function fetchBuilder(fetch: typeof _fetch, defaults?: object): ((input: RequestInfo, init?: RequestInitWithRetry) => Promise<Response>);
export = fetchBuilder;
}

@@ -65,3 +65,4 @@ 'use strict';

var wrappedFetch = function (attempt) {
fetch(input, init)
var _input = input instanceof Request ? input.clone() : input;
fetch(_input, init)
.then(function (response) {

@@ -68,0 +69,0 @@ if (Array.isArray(retryOn) && retryOn.indexOf(response.status) === -1) {

{
"name": "fetch-retry",
"version": "4.1.1",
"version": "5.0.0",
"description": "Extend any fetch library with retry functionality",
"repository": "https://github.com/jonbern/fetch-retry.git",
"main": "index.js",
"browser": "dist/fetch-retry.umd.js",
"types": "index.d.ts",

@@ -11,3 +12,5 @@ "scripts": {

"test": "nyc mocha test/**/**.js && npm run lint",
"integration-test": "mocha test/integration/"
"integration-test": "mocha test/integration/",
"build": "rollup -c",
"prepare": "husky install"
},

@@ -34,4 +37,7 @@ "keywords": [

"nyc": "^14.1.1",
"sinon": "^6.3.5"
"rollup": "^2.52.3",
"@rollup/plugin-commonjs": "^19.0.0",
"sinon": "^6.3.5",
"husky": "^7.0.0"
}
}

@@ -48,116 +48,162 @@ 'use strict';

[200, 503, 404].forEach(statusCode => {
[
['with url string', (baseUrl, init) => fetchRetry(baseUrl, init)],
['with URL type', (baseUrl, init) => {
const url = new URL(baseUrl);
return fetchRetry(url, init);
}],
['with Request argument', (baseUrl, init) => {
const request = new Request(baseUrl);
return fetchRetry(request, init);
}],
].forEach(([usagePatternDescription, fetchInvocation]) => {
describe('when endpoint returns ' + statusCode, () => {
describe(usagePatternDescription, () => {
before(() => {
return setupResponses([statusCode]);
});
[200, 503, 404].forEach(statusCode => {
it('does not retry request', () => {
return fetchRetry(baseUrl)
.then(getCallCount)
.should.eventually.equal(1);
describe('when endpoint returns ' + statusCode, () => {
before(() => {
return setupResponses([statusCode]);
});
it('does not retry request', () => {
return fetchInvocation(baseUrl)
.then(getCallCount)
.should.eventually.equal(1);
});
});
});
});
});
describe('when configured to retry on a specific HTTP code', () => {
describe('and it never succeeds', () => {
const retryOn = [503];
beforeEach(() => {
return setupResponses([503, 503, 503, 503]);
});
it('retries the request #retries times', () => {
const init = {
retries: 3,
retryDelay: 100,
retryOn
};
const expectedCallCount = init.retries + 1;
return fetchRetry(baseUrl, init)
.then(getCallCount)
.should.eventually.equal(expectedCallCount);
});
it('eventually resolves the promise with the response of the last request', () => {
const init = {
retries: 3,
retryDelay: 100,
retryOn
};
const expectedResponse = {
status: 503,
ok: false
};
return fetchRetry(baseUrl, init)
.then(response => {
return {
status: response.status,
ok: response.ok
describe('when configured to retry on a specific HTTP code', () => {
describe('and it never succeeds', () => {
const retryOn = [503];
beforeEach(() => {
return setupResponses([503, 503, 503, 503]);
});
it('retries the request #retries times', () => {
const init = {
retries: 3,
retryDelay: 100,
retryOn
};
})
.should.become(expectedResponse);
const expectedCallCount = init.retries + 1;
return fetchInvocation(baseUrl, init)
.then(getCallCount)
.should.eventually.equal(expectedCallCount);
});
it('eventually resolves the promise with the response of the last request', () => {
const init = {
retries: 3,
retryDelay: 100,
retryOn
};
const expectedResponse = {
status: 503,
ok: false
};
return fetchInvocation(baseUrl, init)
.then(response => {
return {
status: response.status,
ok: response.ok
};
})
.should.become(expectedResponse);
});
});
describe('and it eventually succeeds', () => {
const retryOnStatus = 503;
const responses = [503, 503, 200];
const requestsToRetry = responses
.filter(response => response === retryOnStatus)
.length;
beforeEach(() => {
return setupResponses(responses);
});
it('retries the request up to #retries times', () => {
const init = {
retries: 3,
retryDelay: 100,
retryOn: [retryOnStatus]
};
const expectedCallCount = requestsToRetry + 1;
return fetchInvocation(baseUrl, init)
.then(getCallCount)
.should.eventually.equal(expectedCallCount);
});
it('eventually resolves the promise with the received response of the last request', () => {
const init = {
retries: 3,
retryDelay: 100,
retryOn: [retryOnStatus]
};
const expectedResponse = {
status: 200,
ok: true
};
return fetchInvocation(baseUrl, init)
.then(response => {
return {
status: response.status,
ok: response.ok
};
})
.should.become(expectedResponse);
});
});
});
});
describe('and it eventually succeeds', () => {
const retryOnStatus = 503;
const responses = [503, 503, 200];
const requestsToRetry = responses
.filter(response => response === retryOnStatus)
.length;
beforeEach(() => {
return setupResponses(responses);
});
it('retries the request up to #retries times', () => {
const init = {
retries: 3,
retryDelay: 100,
retryOn: [retryOnStatus]
};
const expectedCallCount = requestsToRetry + 1;
return fetchRetry(baseUrl, init)
.then(getCallCount)
.should.eventually.equal(expectedCallCount);
});
it('eventually resolves the promise with the received response of the last request', () => {
const init = {
retries: 3,
retryDelay: 100,
retryOn: [retryOnStatus]
};
const expectedResponse = {
status: 200,
ok: true
};
return fetchRetry(baseUrl, init)
.then(response => {
return {
status: response.status,
ok: response.ok
describe('when configured to retry on a set of HTTP codes', () => {
describe('and it never succeeds', () => {
const retryOn = [503, 404];
beforeEach(() => {
return setupResponses([503, 404, 404, 503]);
});
it('retries the request #retries times', () => {
const init = {
retries: 3,
retryDelay: 100,
retryOn
};
})
.should.become(expectedResponse);
const expectedCallCount = init.retries + 1;
return fetchInvocation(baseUrl, init)
.then(getCallCount)
.should.eventually.equal(expectedCallCount);
});
});
});
});

@@ -167,30 +213,2 @@

describe('when configured to retry on a set of HTTP codes', () => {
describe('and it never succeeds', () => {
const retryOn = [503, 404];
beforeEach(() => {
return setupResponses([503, 404, 404, 503]);
});
it('retries the request #retries times', () => {
const init = {
retries: 3,
retryDelay: 100,
retryOn
};
const expectedCallCount = init.retries + 1;
return fetchRetry(baseUrl, init)
.then(getCallCount)
.should.eventually.equal(expectedCallCount);
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc