Skip to content

Commit 26dc344

Browse files
committed
fix: 使用axios代替fetch以防止请求出错
1 parent 7e704ab commit 26dc344

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
}
8686
},
8787
"dependencies": {
88+
"foca-axios": "^4.1.2",
8889
"listr2": "^8.3.2",
8990
"lodash-es": "^4.17.21",
9091
"minimist": "^1.2.8",
@@ -112,7 +113,6 @@
112113
"@types/qs": "^6.9.18",
113114
"@vitest/coverage-v8": "^3.1.1",
114115
"axios": "^1.8.4",
115-
"foca-axios": "^4.1.2",
116116
"husky": "^9.1.7",
117117
"only-allow": "^1.2.1",
118118
"release-it": "^19.0.1",

pnpm-lock.yaml

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

src/lib/path-to-openapi.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@ import path from 'node:path';
33
import type { OpenAPIV3 } from 'openapi-types';
44
import YAML from 'yaml';
55
import type { OpenapiClientConfig } from '../define-config';
6+
import axios from 'foca-axios';
67

78
export const pathToOpenapi = async (
89
uri: string,
910
onLoaded?: OpenapiClientConfig['onDocumentLoaded'],
1011
): Promise<OpenAPIV3.Document> => {
1112
let originContent: string;
1213
if (uri.startsWith('http:') || uri.startsWith('https:')) {
13-
const response = await fetch(uri, { method: 'get' });
14-
originContent = await response.text();
14+
// 使用fetch时会出现偶发性错误:`connect ECONNREFUSED 0.0.0.0:443`
15+
originContent = await axios.get(uri, { responseType: 'text' });
1516
} else {
1617
originContent = await readFile(path.resolve(uri), 'utf8');
1718
}
1819

1920
let document: OpenAPIV3.Document;
20-
if (originContent.startsWith('{')) {
21+
try {
2122
document = JSON.parse(originContent);
22-
} else {
23+
} catch {
2324
document = YAML.parse(originContent);
2425
}
2526

test/lib/path-to-openapi.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { pathToOpenapi } from '../../src/lib/path-to-openapi';
33
import path from 'node:path';
44
import { readFileSync } from 'node:fs';
55
import type { OpenAPIV3 } from 'openapi-types';
6+
import axios from 'foca-axios';
67

78
test('从本地获取json', async () => {
89
const result = await pathToOpenapi('./openapi/openapi.json');
@@ -19,18 +20,13 @@ test('从本地获取yaml', async () => {
1920
});
2021

2122
test('从远程获取', async () => {
22-
const originalFetch = globalThis.fetch;
23-
globalThis.fetch = vitest.fn().mockResolvedValueOnce({
24-
ok: true,
25-
text: () => {
26-
return Promise.resolve(
27-
JSON.stringify(readFileSync(path.resolve('openapi', 'openapi.json'), 'utf8')),
28-
);
29-
},
23+
const originalFetch = axios.request;
24+
axios.request = vitest.fn().mockImplementation(async () => {
25+
return JSON.stringify(readFileSync(path.resolve('openapi', 'openapi.json'), 'utf8'));
3026
});
3127
const result = await pathToOpenapi('http://example.com');
3228
expect(result).toMatchFileSnapshot(path.resolve('openapi', 'openapi.json'));
33-
globalThis.fetch = originalFetch;
29+
axios.request = originalFetch;
3430
});
3531

3632
test('支持加载事件', async () => {

0 commit comments

Comments
 (0)