Skip to content

Commit 04baaec

Browse files
tatomyrandriimredocly
authored andcommitted
refactor: move fetchWithTimeout out of the openapi-core package (#2035)
1 parent 3f84908 commit 04baaec

File tree

9 files changed

+60
-57
lines changed

9 files changed

+60
-57
lines changed

package-lock.json

Lines changed: 38 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"prettier": "npx prettier --write \"**/*.{ts,js,yaml,yml,json,md}\"",
2121
"prettier:check": "npx prettier --check \"**/*.{ts,js,yaml,yml,json,md}\"",
2222
"eslint": "eslint packages/**",
23-
"clean": "rm -rf packages/**/lib packages/**/node_modules packages/**/*.tsbuildinfo package-lock.json node_modules dist && git checkout package-lock.json",
23+
"clear": "rm -rf packages/**/lib packages/**/node_modules packages/**/*.tsbuildinfo package-lock.json node_modules dist && git checkout package-lock.json",
2424
"typecheck": "tsc --noEmit --skipLibCheck",
2525
"compile": "tsc -b tsconfig.build.json && npm run respect:parser:generate && npm run build-docs:copy-assets",
2626
"prepare": "npm run compile",

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"form-data": "^4.0.0",
5454
"glob": "^11.0.1",
5555
"handlebars": "^4.7.6",
56+
"https-proxy-agent": "^7.0.5",
5657
"mobx": "^6.0.4",
5758
"pluralize": "^8.0.0",
5859
"react": "^17.0.0 || ^18.2.0 || ^19.0.0",

packages/cli/src/__tests__/fetch-with-timeout.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import AbortController from 'abort-controller';
2-
import fetchWithTimeout from '../utils/fetch-with-timeout.js';
3-
import { getProxyAgent } from '@redocly/openapi-core';
42
import { HttpsProxyAgent } from 'https-proxy-agent';
3+
import fetchWithTimeout from '../utils/fetch-with-timeout.js';
4+
import * as proxyAgent from '../utils/proxy-agent.js';
55

66
const signalInstance = new AbortController().signal;
77

@@ -31,6 +31,11 @@ const mockFetch = vi.fn(() =>
3131
const originalFetch = global.fetch;
3232
global.fetch = mockFetch;
3333

34+
vi.mock('../utils/get-proxy-agent.js', async () => {
35+
const actual = await vi.importActual('../utils/get-proxy-agent.js');
36+
return { ...actual };
37+
});
38+
3439
describe('fetchWithTimeout', () => {
3540
beforeAll(() => {
3641
global.setTimeout = vi.fn() as any;
@@ -60,12 +65,12 @@ describe('fetchWithTimeout', () => {
6065
});
6166

6267
it('should call fetch with proxy agent', async () => {
63-
const proxyAgent = new HttpsProxyAgent('http://localhost');
64-
vi.mocked(getProxyAgent).mockReturnValueOnce(proxyAgent as any);
68+
const dispatcher = new HttpsProxyAgent('http://localhost');
69+
vi.spyOn(proxyAgent, 'getProxyAgent').mockReturnValueOnce(dispatcher);
6570

6671
await fetchWithTimeout('url');
6772

68-
expect(global.fetch).toHaveBeenCalledWith('url', { dispatcher: proxyAgent });
73+
expect(global.fetch).toHaveBeenCalledWith('url', { dispatcher });
6974
});
7075

7176
it('should call fetch without signal when timeout is not passed', async () => {

packages/cli/src/utils/fetch-with-timeout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getProxyAgent } from '@redocly/openapi-core';
1+
import { getProxyAgent } from './proxy-agent.js';
22

33
export const DEFAULT_FETCH_TIMEOUT = 3000;
44

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { HttpsProxyAgent } from 'https-proxy-agent';
2+
3+
export function getProxyAgent() {
4+
const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
5+
return proxy ? new HttpsProxyAgent(proxy) : undefined;
6+
}

packages/core/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
"node:fs": false,
3939
"node:path": "path-browserify",
4040
"node:os": false,
41-
"colorette": false,
42-
"https-proxy-agent": false
41+
"node:url": false,
42+
"node:module": false,
43+
"colorette": false
4344
},
4445
"homepage": "https://github.com/Redocly/redocly-cli",
4546
"keywords": [
@@ -59,7 +60,6 @@
5960
"@redocly/ajv": "^8.11.2",
6061
"@redocly/config": "^0.22.0",
6162
"colorette": "^1.2.0",
62-
"https-proxy-agent": "^7.0.5",
6363
"js-levenshtein": "^1.1.6",
6464
"js-yaml": "^4.1.0",
6565
"minimatch": "^10.0.1",

packages/core/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export {
66
slash,
77
doesYamlFileExist,
88
isTruthy,
9-
getProxyAgent,
109
pause,
1110
isPlainObject,
1211
dequal,

packages/core/src/utils.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { minimatch } from 'minimatch';
44
import { parseYaml } from './js-yaml/index.js';
55
import { env } from './env.js';
66
import { logger, colorize } from './logger.js';
7-
import { HttpsProxyAgent } from 'https-proxy-agent';
87
import * as pluralize1 from 'pluralize'; // FIXME: use correct import after migration to ESM
98
const pluralizeOne = (pluralize1 as any).default || pluralize1; // FIXME: use correct import after migration to ESM
109

@@ -309,11 +308,6 @@ function getUpdatedFieldName(updatedField: string, updatedObject?: string) {
309308
return `${typeof updatedObject !== 'undefined' ? `${updatedObject}.` : ''}${updatedField}`;
310309
}
311310

312-
export function getProxyAgent() {
313-
const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
314-
return proxy ? new HttpsProxyAgent(proxy) : undefined;
315-
}
316-
317311
/**
318312
* Checks if two objects are deeply equal.
319313
* Borrowed the source code from https://github.com/lukeed/dequal.

0 commit comments

Comments
 (0)