fix: 配置原始请求方法丢失

pull/49/head
zjx0905 2023-04-27 10:34:36 +08:00
parent 8b6eed2d6d
commit c107171eba
4 changed files with 19 additions and 16 deletions

View File

@ -26,7 +26,6 @@ export function dispatchRequest(config: AxiosRequestConfig) {
assert(isString(config.url), 'url 不是一个 string'); assert(isString(config.url), 'url 不是一个 string');
assert(isString(config.method), 'method 不是一个 string'); assert(isString(config.method), 'method 不是一个 string');
config.method = config.method!.toUpperCase() as AxiosRequestMethod;
config.headers = flattenHeaders(config); config.headers = flattenHeaders(config);
// 可以携带 data 的请求方法,转换 data // 可以携带 data 的请求方法,转换 data

View File

@ -9,6 +9,7 @@ import {
AxiosAdapterResponse, AxiosAdapterResponse,
AxiosAdapterResponseError, AxiosAdapterResponseError,
AxiosAdapterPlatformTask, AxiosAdapterPlatformTask,
AxiosAdapterRequestMethod,
} from '../adpater/createAdapter'; } from '../adpater/createAdapter';
import { isCancelToken } from './cancel'; import { isCancelToken } from './cancel';
import { AxiosErrorResponse, createError } from './createError'; import { AxiosErrorResponse, createError } from './createError';
@ -26,8 +27,9 @@ export function request(config: AxiosRequestConfig) {
return new Promise<AxiosResponse>((resolve, reject) => { return new Promise<AxiosResponse>((resolve, reject) => {
const adapterConfig: AxiosAdapterRequestConfig = { const adapterConfig: AxiosAdapterRequestConfig = {
...(config as AxiosAdapterRequestConfig), ...(config as AxiosAdapterRequestConfig),
url: transformURL(config),
type: generateType(config), type: generateType(config),
url: transformURL(config),
method: config.method!.toUpperCase() as AxiosAdapterRequestMethod,
success, success,
fail, fail,
}; };

View File

@ -38,18 +38,6 @@ describe('src/request/dispatchRequest.ts', () => {
).not.toThrowError(); ).not.toThrowError();
}); });
testEachMethods('应该支持 %s 转全大写', (k) => {
const c = {
adapter: mockAdapter(),
url: '/',
method: k,
};
dispatchRequest(c);
expect(c.method).toBe(k.toUpperCase());
});
test('坏的适配器应该抛出异常', () => { test('坏的适配器应该抛出异常', () => {
expect( expect(
dispatchRequest({ dispatchRequest({
@ -64,7 +52,7 @@ describe('src/request/dispatchRequest.ts', () => {
"config": { "config": {
"adapter": [Function], "adapter": [Function],
"headers": {}, "headers": {},
"method": "GET", "method": "get",
"url": "/", "url": "/",
}, },
"request": undefined, "request": undefined,
@ -72,7 +60,7 @@ describe('src/request/dispatchRequest.ts', () => {
"config": { "config": {
"adapter": [Function], "adapter": [Function],
"headers": {}, "headers": {},
"method": "GET", "method": "get",
"url": "/", "url": "/",
}, },
"data": undefined, "data": undefined,

View File

@ -18,6 +18,7 @@ describe('src/request/request.ts', () => {
}, },
baseURL: 'http://api.com', baseURL: 'http://api.com',
url: 'test', url: 'test',
method: 'get' as const,
}; };
const c2 = { const c2 = {
adapter(config: AnyObject) { adapter(config: AnyObject) {
@ -25,6 +26,7 @@ describe('src/request/request.ts', () => {
}, },
baseURL: 'http://api.com', baseURL: 'http://api.com',
url: 'test/:id', url: 'test/:id',
method: 'get' as const,
params: { params: {
id: 1, id: 1,
}, },
@ -46,6 +48,18 @@ describe('src/request/request.ts', () => {
request(c3); request(c3);
}); });
testEachMethods('应该支持 %s 转全大写', (k) => {
const c = {
adapter(config: AnyObject) {
expect(config.method).toBe(k.toUpperCase());
},
url: '/',
method: k,
};
request(c);
});
testEachMethods('%s 方法应该返回正确的响应体结构', (k) => { testEachMethods('%s 方法应该返回正确的响应体结构', (k) => {
const c = { const c = {
adapter: mockAdapter(), adapter: mockAdapter(),