Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add node-fetch-v3 support #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
'use strict';

module.exports = {
"extends": "@adobe/eslint-config-asset-compute"
"extends": "@adobe/eslint-config-asset-compute",
"parserOptions": {
"ecmaVersion": 12
},
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ typings/
.vscode
!.vscode/launch.json

# Webstorm
.idea

# serverless
.serverless/action.zip
.webpack
Expand Down
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
'use strict';

const AbortController = require('abort-controller');
const fetch = require('node-fetch');
const {FetchError} = fetch;

/**
* Retry
Expand Down Expand Up @@ -137,7 +135,7 @@ function checkParameters(retryOptions) {
* Fetch retry that wraps around `node-fetch` library
* @param {String} url request url
* @param {Options} options options for fetch request (e.g. headers, RetryOptions for retries or `false` if no do not want to perform retries)
* @returns {Object} json response of calling fetch
* @returns {Object} json response of calling fetch
*/
module.exports = async function (url, options) {
options = options || {};
Expand All @@ -157,7 +155,7 @@ module.exports = async function (url, options) {
}

try {
const response = await fetch(url, options);
const response = await import('node-fetch').then( ({ default: fetch }) => fetch(url, options));
clearTimeout(timeoutHandler);

if (!retry(retryOptions, null, response)) {
Expand All @@ -172,6 +170,7 @@ module.exports = async function (url, options) {

if (!retry(retryOptions, error, null)) {
if (error.name === 'AbortError') {
const FetchError = await import('node-fetch').then( ({ FetchError }) => FetchError);
return reject(new FetchError(`network timeout at ${url}`, 'request-timeout'));
}

Expand Down
49 changes: 46 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "Apache-2.0",
"dependencies": {
"abort-controller": "^3.0.0",
"node-fetch": "^2.6.1"
"node-fetch": "^3.0.0"
},
"devDependencies": {
"@adobe/eslint-config-asset-compute": "^1.3.1",
Expand All @@ -24,7 +24,8 @@
"nock": "^13.0.4",
"nyc": "^15.1.0",
"rewire": "^5.0.0",
"semantic-release": "^17.2.1"
"semantic-release": "^17.2.1",
"timeout-signal": "^1.1.0"
},
"keywords": [
"fetch",
Expand Down
52 changes: 26 additions & 26 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ describe('test fetch retry', () => {
.reply(200, { ok: true });

const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`,
{
method: 'GET',
headers: { Authorization: 'Basic thisShouldBeAnAuthHeader' }
{
method: 'GET',
headers: { Authorization: 'Basic thisShouldBeAnAuthHeader' }
}
);
assert.strictEqual(response.ok, true);
Expand All @@ -217,9 +217,9 @@ describe('test fetch retry', () => {
.reply(200, { ok: true });

const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`,
{
method: 'GET',
headers: { Authorization: 'Bearer thisShouldBeAToken' }
{
method: 'GET',
headers: { Authorization: 'Bearer thisShouldBeAToken' }
}
);
assert.strictEqual(response.ok, true);
Expand Down Expand Up @@ -256,9 +256,9 @@ describe('test fetch retry', () => {
.reply(401, { ok: false });

const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`,
{
method: 'GET',
headers: { Authorization: 'Basic thisShouldBeAnAuthHeader' }
{
method: 'GET',
headers: { Authorization: 'Basic thisShouldBeAnAuthHeader' }
}
);
assert.strictEqual(response.ok, false);
Expand All @@ -269,7 +269,7 @@ describe('test fetch retry', () => {
.get(FAKE_PATH)
.reply(500);
const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`, { method: 'GET', retryOptions: false });
assert.strictEqual(response.statusText, 'Internal Server Error');
assert.strictEqual(response.statusText, '');
});

it('test get retry with default settings 500 then 200', async () => {
Expand All @@ -283,7 +283,7 @@ describe('test fetch retry', () => {
const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`, { method: 'GET' });
assert(nock.isDone());
assert(response.ok);
assert.strictEqual(response.statusText, 'OK');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 200);
});

Expand All @@ -297,13 +297,13 @@ describe('test fetch retry', () => {
.get(FAKE_PATH)
.matchHeader('Authorization', 'Basic thisShouldBeAnAuthHeader')
.reply(200, { ok: true });
const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`,
{
method: 'GET', headers: { Authorization: 'Basic thisShouldBeAnAuthHeader' }
const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`,
{
method: 'GET', headers: { Authorization: 'Basic thisShouldBeAnAuthHeader' }
});
assert(nock.isDone());
assert(response.ok);
assert.strictEqual(response.statusText, 'OK');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 200);
});

Expand All @@ -314,7 +314,7 @@ describe('test fetch retry', () => {
const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`, { method: 'GET' });
assert(nock.isDone());
assert(!response.ok);
assert.strictEqual(response.statusText, 'Bad Request');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 400);
});

Expand All @@ -325,7 +325,7 @@ describe('test fetch retry', () => {
const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`, { method: 'GET' });
assert(nock.isDone());
assert(!response.ok);
assert.strictEqual(response.statusText, 'Not Found');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 404);
});

Expand All @@ -336,7 +336,7 @@ describe('test fetch retry', () => {
const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`, { method: 'GET' });
assert(nock.isDone());
assert(!response.ok);
assert.strictEqual(response.statusText, 'Multiple Choices');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 300);
});

Expand All @@ -353,7 +353,7 @@ describe('test fetch retry', () => {
.reply(200, { ok: true });
const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`, { method: 'GET' });
assert(nock.isDone());
assert.strictEqual(response.statusText, 'OK');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 200);
});

Expand Down Expand Up @@ -388,7 +388,7 @@ describe('test fetch retry', () => {
}
});
assert(nock.isDone());
assert.strictEqual(response.statusText, 'Not Found');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 404);
});

Expand All @@ -406,7 +406,7 @@ describe('test fetch retry', () => {
});
assert(!nock.isDone()); // should fail on first fetch call
nock.cleanAll();
assert.strictEqual(response.statusText, 'HTTP Version Not Supported');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 505);
});

Expand All @@ -429,7 +429,7 @@ describe('test fetch retry', () => {
}
});
assert(nock.isDone());
assert.strictEqual(response.statusText, 'OK');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 200);
});

Expand All @@ -452,7 +452,7 @@ describe('test fetch retry', () => {
}
});
assert(!nock.isDone()); // nock should not have gotten all calls
assert.strictEqual(response.statusText, 'Unauthorized');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 401);

// clean up nock
Expand All @@ -470,7 +470,7 @@ describe('test fetch retry', () => {
.reply(200);
const response = await fetch(`${FAKE_BASE_URL}${FAKE_PATH}`,
{
method: 'GET',
method: 'GET',
headers: {
Authorization: 'Basic thisShouldBeAnAuthHeader'
},
Expand All @@ -483,7 +483,7 @@ describe('test fetch retry', () => {
}
});
assert(!nock.isDone()); // nock should not have gotten all calls
assert.strictEqual(response.statusText, 'Unauthorized');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 401);

// clean up nock
Expand All @@ -505,7 +505,7 @@ describe('test fetch retry', () => {
});
assert(nock.isDone());
nock.cleanAll(); // clean persisted nock
assert.strictEqual(response.statusText, 'Not Found');
assert.strictEqual(response.statusText, '');
assert.strictEqual(response.status, 404);

}).timeout(3000);
Expand Down