Skip to content

Commit

Permalink
Drop node-fetch due to Request class limitations
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrotoff committed Jul 6, 2023
1 parent 16a84d5 commit 9ae3d2a
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 179 deletions.
1 change: 0 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ jobs:
- run: npm run tsc:examples
- run: npm run start:examples
- run: npm run test:undici
- run: npm run test:node-fetch
- run: npm run test:e2e
- run: npm run test:coverage:whatwg-fetch
- run: npm run test:coverage:examples
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ A [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) wrapper.
- No dependencies
- Supports Node.js & web browsers
- Comes with test utilities
- Fully tested (against [node-fetch](https://github.com/node-fetch/node-fetch), [whatwg-fetch](https://github.com/github/fetch) & [undici](https://github.com/nodejs/undici))
- Fully tested (against [Undici](https://github.com/nodejs/undici) & [whatwg-fetch](https://github.com/github/fetch))
- Written in TypeScript

## Why?
Expand Down Expand Up @@ -94,7 +94,7 @@ Or copy-paste [Http.ts](src/Http.ts) into your source code.

- Nothing is needed if Node.js >= 18.0
- Use [`--experimental-fetch`](https://nodejs.org/docs/latest-v16.x/api/cli.html#--experimental-fetch) if Node.js >= 16.15 < 18.0
- [node-fetch](https://github.com/node-fetch/node-fetch) if Node.js < 16.15
- ⚠️ [node-fetch](https://github.com/node-fetch/node-fetch) is not supported with @tkrotoff/fetch >= 0.17 due to [`Request` class limitations](https://github.com/node-fetch/node-fetch/blob/v3.3.1/README.md#class-request)

Check [examples/node](examples/node)

Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const { defaults } = require('jest-config');

// [How to set transformIgnorePatterns to fix "Jest encountered an unexpected token"](https://github.com/nrwl/nx/issues/812)
const esModules = ['node-fetch', 'data-uri-to-buffer', 'fetch-blob', 'formdata-polyfill'];
const esModules = [];

/** @type {import('jest').Config} */
const config = {
Expand Down
24 changes: 8 additions & 16 deletions jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@ console.assert = assert;
/* eslint-disable global-require, @typescript-eslint/no-var-requires */
const fetchPolyfill = process.env.FETCH;
switch (fetchPolyfill) {
case 'whatwg-fetch': {
const whatwgFetch = require('whatwg-fetch');
globalThis.fetch = whatwgFetch.fetch;
globalThis.Request = whatwgFetch.Request;
globalThis.Response = whatwgFetch.Response;
globalThis.Headers = whatwgFetch.Headers;
break;
}
case 'node-fetch': {
const nodeFetch = require('node-fetch');
globalThis.fetch = nodeFetch.default;
globalThis.Request = nodeFetch.Request;
globalThis.Response = nodeFetch.Response;
globalThis.Headers = nodeFetch.Headers;
break;
}
case 'undici': {
const undici = require('undici');
globalThis.fetch = undici.fetch;
Expand All @@ -31,6 +15,14 @@ switch (fetchPolyfill) {
globalThis.Headers = undici.Headers;
break;
}
case 'whatwg-fetch': {
const whatwgFetch = require('whatwg-fetch');
globalThis.fetch = whatwgFetch.fetch;
globalThis.Request = whatwgFetch.Request;
globalThis.Response = whatwgFetch.Response;
globalThis.Headers = whatwgFetch.Headers;
break;
}
default: {
assert(false, `Invalid fetch polyfill: '${fetchPolyfill}'`);
}
Expand Down
91 changes: 0 additions & 91 deletions package-lock.json

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

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"test:coverage": "npm run test:coverage:undici",
"test:undici": "NODE_EXTRA_CA_CERTS=./src/createTestServer/createTestServer.cert FETCH=undici jest --verbose",
"test:coverage:undici": "NODE_EXTRA_CA_CERTS=./src/createTestServer/createTestServer.cert FETCH=undici jest --coverage",
"test:node-fetch": "NODE_EXTRA_CA_CERTS=./src/createTestServer/createTestServer.cert FETCH=node-fetch jest --verbose",
"test:whatwg-fetch": "NODE_EXTRA_CA_CERTS=./src/createTestServer/createTestServer.cert FETCH=whatwg-fetch jest --env=jsdom --verbose",
"test:coverage:whatwg-fetch": "NODE_EXTRA_CA_CERTS=./src/createTestServer/createTestServer.cert FETCH=whatwg-fetch jest --env=jsdom --coverage",
"test:e2e": "playwright test",
Expand Down Expand Up @@ -84,7 +83,6 @@
"husky": "^8.0.3",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"node-fetch": "^3.3.1",
"playwright": "^1.34.3",
"prettier": "^2.8.8",
"rollup": "^3.23.0",
Expand Down
18 changes: 5 additions & 13 deletions src/Http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,18 +565,14 @@ describe('body methods', () => {
// https://github.com/whatwg/fetch/issues/1147
let bodyAlreadyUsedError = '';
switch (process.env.FETCH) {
case 'node-fetch': {
bodyAlreadyUsedError = 'body used already for: ';
case 'undici': {
bodyAlreadyUsedError = 'Body is unusable';
break;
}
case 'whatwg-fetch': {
bodyAlreadyUsedError = 'Already read';
break;
}
case 'undici': {
bodyAlreadyUsedError = 'Body is unusable';
break;
}
default: {
assert(false, `Unknown FETCH env '${process.env.FETCH}'`);
}
Expand Down Expand Up @@ -634,18 +630,14 @@ test('cannot connect', async () => {

let requestFailedError = '';
switch (process.env.FETCH) {
case 'node-fetch': {
requestFailedError = `request to ${url} failed, reason: connect ECONNREFUSED 127.0.0.1:80`;
case 'undici': {
requestFailedError = 'fetch failed';
break;
}
case 'whatwg-fetch': {
requestFailedError = 'Network request failed';
break;
}
case 'undici': {
requestFailedError = 'fetch failed';
break;
}
default: {
assert(false, `Unknown FETCH env '${process.env.FETCH}'`);
}
Expand All @@ -659,7 +651,7 @@ test('cannot connect', async () => {
} catch (e) {
assert(e instanceof Error);
/* eslint-disable jest/no-conditional-expect */
expect(e.name).toEqual(process.env.FETCH === 'node-fetch' ? 'FetchError' : 'TypeError');
expect(e.name).toEqual('TypeError');
expect(e.message).toEqual(requestFailedError);
/* eslint-enable jest/no-conditional-expect */
}
Expand Down
2 changes: 1 addition & 1 deletion src/HttpError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test('HttpError with statusText (HTTP/1.1)', async () => {

test('HttpError without statusText because of HTTP/2', async () => {
// ["HTTP/2 doesn't have reason phrases anymore"](https://stackoverflow.com/q/41632077)
// Unfortunately HTTP/2 does not work with whatwg-fetch/jsdom and node-fetch so we cannot test using HTTP/2
// Unfortunately HTTP/2 does not work with whatwg-fetch/jsdom so we cannot test using HTTP/2
// Let's emulate an empty statusText instead

const body = {
Expand Down

0 comments on commit 9ae3d2a

Please sign in to comment.