Skip to content
This repository was archived by the owner on May 15, 2024. It is now read-only.

Commit ac0d616

Browse files
authored
Refactor/req to fetch (#49)
* refactor(request): refactor to fetch * refactor(request): remove request dep * refactor(request): fix yarn lock * refactor(request): run yarn audit fix
1 parent 7ff0046 commit ac0d616

File tree

5 files changed

+41
-304
lines changed

5 files changed

+41
-304
lines changed
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,29 @@
1414

1515
const instances = [];
1616

17-
const request = jest.fn((config) => {
17+
const fetch = jest.fn((url, config) => {
1818
const reqInstance = {
1919
mockRequest: true,
2020
config,
2121
events: {},
2222
};
23-
reqInstance.on = jest.fn((event, cb) => {
23+
reqInstance.catch = jest.fn((cb) => {
24+
const event = 'error';
2425
reqInstance.events[event] = reqInstance.events[event] || [];
2526
reqInstance.events[event].push(cb);
2627
return reqInstance;
2728
});
2829
reqInstance.emit = jest.fn((event, data) => {
2930
(reqInstance.events[event] || []).forEach((cb) => cb(data));
3031
});
31-
reqInstance.pipe = jest.fn((s) => s);
32+
reqInstance.pipe = jest.fn((s) => Promise.resolve(s));
3233
instances.push(reqInstance);
3334
return reqInstance;
3435
});
3536

36-
request.__resetRequests = () => { // eslint-disable-line no-underscore-dangle
37+
fetch.__resetRequests = () => { // eslint-disable-line no-underscore-dangle
3738
instances.splice(0, Infinity);
3839
};
39-
request.__getRequest = (n) => instances[n]; // eslint-disable-line no-underscore-dangle
40+
fetch.__getRequest = (n) => Promise.resolve(instances[n]); // eslint-disable-line no-underscore-dangle, max-len
4041

41-
module.exports = request;
42+
module.exports = fetch;

__tests__/src/middleware.spec.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
* under the License.
1313
*/
1414

15-
import request from 'request';
15+
import fetch from 'node-fetch';
1616
import chalk from 'chalk';
1717
import {
1818
setRemoteUrl,
1919
pipe,
2020
} from '../../src/middleware';
2121

22-
jest.mock('request');
2322
jest.mock('chalk', () => ({ red: jest.fn((s) => `<red>${s}</red>`) }));
23+
jest.mock('node-fetch');
2424

2525
describe('setRemoteUrl', () => {
2626
it('should build the remote URL for the request and attach it to the request', () => new Promise((done) => {
@@ -47,8 +47,8 @@ describe('pipe', () => {
4747
const res = { data: 'data' };
4848

4949
beforeEach(() => {
50-
request.__resetRequests(); // eslint-disable-line no-underscore-dangle
51-
request.mockClear();
50+
fetch.__resetRequests(); // eslint-disable-line no-underscore-dangle
51+
fetch.mockClear();
5252
req.pipe.mockClear();
5353
});
5454

@@ -58,22 +58,22 @@ describe('pipe', () => {
5858
expect(req.headers.referer).toBe(undefined);
5959
});
6060

61-
it('should pipe request', () => {
61+
it('should pipe request', async () => {
6262
pipe(false)(req, res);
63-
const reqInstance = request.__getRequest(0); // eslint-disable-line no-underscore-dangle
64-
expect(req.pipe).toBeCalledWith(reqInstance);
63+
const reqInstance = await fetch.__getRequest(0); // eslint-disable-line no-underscore-dangle
64+
expect(req.pipe).toHaveBeenCalledWith(reqInstance);
6565
});
6666

67-
it('should pipe response', () => {
67+
it('should pipe response', async () => {
6868
pipe(false)(req, res);
69-
const reqInstance = request.__getRequest(0); // eslint-disable-line no-underscore-dangle
69+
const reqInstance = await fetch.__getRequest(0); // eslint-disable-line no-underscore-dangle
7070
expect(reqInstance.pipe).toBeCalledWith(res);
7171
});
7272

73-
it('should show a message when there is a socket error', () => {
73+
it('should show a message when there is a socket error', async () => {
7474
jest.spyOn(console, 'error').mockImplementation(() => 0);
7575
pipe(false)(req, res);
76-
const reqInstance = request.__getRequest(0); // eslint-disable-line no-underscore-dangle
76+
const reqInstance = await fetch.__getRequest(0); // eslint-disable-line no-underscore-dangle
7777
reqInstance.emit('error', new Error('test error like socket timeout'));
7878
expect(chalk.red).toHaveBeenCalled();
7979
expect(console.error).toHaveBeenCalled();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"chalk": "^3.0.0",
5959
"cors": "^2.8.3",
6060
"express": "^4.15.2",
61-
"request": "^2.88.2",
61+
"node-fetch": "2.6.7",
6262
"rimraf": "^2.6.1",
6363
"yargs": "^15.4.1"
6464
},

src/middleware.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,28 @@
1313
*/
1414

1515
/* eslint-disable no-param-reassign */
16-
import request from 'request';
16+
import fetch from 'node-fetch';
1717
import chalk from 'chalk';
1818

1919
export const setRemoteUrl = (remoteBaseUrl) => (req, res, next) => {
2020
req.remoteUrl = `${remoteBaseUrl}${req.originalUrl.slice(req.baseUrl.length)}`;
2121
next();
2222
};
2323

24-
export const pipe = () => (req, res) => {
24+
export const pipe = () => async (req, res) => {
2525
// avoid copying the request
2626
delete req.headers.origin;
2727
delete req.headers.referer;
2828

2929
const config = {
3030
method: req.method,
31-
url: req.remoteUrl,
3231
rejectUnauthorized: false,
3332
};
3433

3534
return req
3635
.pipe(
37-
request(config)
38-
.on('error', (err) => {
36+
await fetch(req.remoteUrl, config)
37+
.catch((err) => {
3938
console.error(`${chalk.red(`request to ${req.remoteUrl} failed:`)} ${err.message}`);
4039
})
4140
)

0 commit comments

Comments
 (0)