Skip to content

Commit 8b6eed2

Browse files
committed
fix: 配置原始 URL 丢失
1 parent 4b02582 commit 8b6eed2

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

src/request/dispatchRequest.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Cancel, isCancel, isCancelToken } from './cancel';
1010
import { flattenHeaders } from './flattenHeaders';
1111
import { AxiosTransformer, transformData } from './transformData';
1212
import { request } from './request';
13-
import { transformURL } from './transformURL';
1413
import { AxiosErrorResponse } from './createError';
1514

1615
/**
@@ -38,8 +37,6 @@ export function dispatchRequest(config: AxiosRequestConfig) {
3837
delete config.data;
3938
}
4039

41-
config.url = transformURL(config);
42-
4340
function onSuccess(response: AxiosResponse) {
4441
throwIfCancellationRequested(config);
4542
dataTransformer(response, config.transformResponse);

src/request/request.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { isCancelToken } from './cancel';
1414
import { AxiosErrorResponse, createError } from './createError';
1515
import { generateType } from './generateType';
16+
import { transformURL } from './transformURL';
1617

1718
/**
1819
* 开始请求
@@ -25,6 +26,7 @@ export function request(config: AxiosRequestConfig) {
2526
return new Promise<AxiosResponse>((resolve, reject) => {
2627
const adapterConfig: AxiosAdapterRequestConfig = {
2728
...(config as AxiosAdapterRequestConfig),
29+
url: transformURL(config),
2830
type: generateType(config),
2931
success,
3032
fail,

test/request/dispatchRequest.test.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,36 +86,6 @@ describe('src/request/dispatchRequest.ts', () => {
8686
`);
8787
});
8888

89-
test('应该支持转换 URL', () => {
90-
const c1 = {
91-
...defaults,
92-
url: 'test',
93-
};
94-
const c2 = {
95-
...defaults,
96-
url: 'test/:id',
97-
params: {
98-
id: 1,
99-
},
100-
};
101-
const c3 = {
102-
...defaults,
103-
method: 'post' as const,
104-
url: 'test/:id',
105-
data: {
106-
id: 1,
107-
},
108-
};
109-
110-
dispatchRequest(c1);
111-
dispatchRequest(c2);
112-
dispatchRequest(c3);
113-
114-
expect(c1.url).toBe('http://api.com/test');
115-
expect(c2.url).toBe('http://api.com/test/1');
116-
expect(c3.url).toBe('http://api.com/test/1');
117-
});
118-
11989
test('应该支持拉平请求头', () => {
12090
const c = {
12191
...defaults,

test/request/request.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@ import { request } from '@/request/request';
1111
import axios from '@/axios';
1212

1313
describe('src/request/request.ts', () => {
14+
test('应该支持转换 URL', () => {
15+
const c1 = {
16+
adapter(config: AnyObject) {
17+
expect(config.url).toBe('http://api.com/test');
18+
},
19+
baseURL: 'http://api.com',
20+
url: 'test',
21+
};
22+
const c2 = {
23+
adapter(config: AnyObject) {
24+
expect(config.url).toBe('http://api.com/test/1');
25+
},
26+
baseURL: 'http://api.com',
27+
url: 'test/:id',
28+
params: {
29+
id: 1,
30+
},
31+
};
32+
const c3 = {
33+
adapter(config: AnyObject) {
34+
expect(config.url).toBe('http://api.com/test/1');
35+
},
36+
baseURL: 'http://api.com',
37+
url: 'test/:id',
38+
method: 'post' as const,
39+
data: {
40+
id: 1,
41+
},
42+
};
43+
44+
request(c1);
45+
request(c2);
46+
request(c3);
47+
});
48+
1449
testEachMethods('%s 方法应该返回正确的响应体结构', (k) => {
1550
const c = {
1651
adapter: mockAdapter(),

0 commit comments

Comments
 (0)