diff --git a/src/request/dispatchRequest.ts b/src/request/dispatchRequest.ts index 581c0ad..1382b8e 100644 --- a/src/request/dispatchRequest.ts +++ b/src/request/dispatchRequest.ts @@ -26,7 +26,6 @@ export function dispatchRequest(config: AxiosRequestConfig) { assert(isString(config.url), 'url 不是一个 string'); assert(isString(config.method), 'method 不是一个 string'); - config.method = config.method!.toUpperCase() as AxiosRequestMethod; config.headers = flattenHeaders(config); // 可以携带 data 的请求方法,转换 data diff --git a/src/request/request.ts b/src/request/request.ts index 197abfc..74cb958 100644 --- a/src/request/request.ts +++ b/src/request/request.ts @@ -9,6 +9,7 @@ import { AxiosAdapterResponse, AxiosAdapterResponseError, AxiosAdapterPlatformTask, + AxiosAdapterRequestMethod, } from '../adpater/createAdapter'; import { isCancelToken } from './cancel'; import { AxiosErrorResponse, createError } from './createError'; @@ -26,8 +27,9 @@ export function request(config: AxiosRequestConfig) { return new Promise((resolve, reject) => { const adapterConfig: AxiosAdapterRequestConfig = { ...(config as AxiosAdapterRequestConfig), - url: transformURL(config), type: generateType(config), + url: transformURL(config), + method: config.method!.toUpperCase() as AxiosAdapterRequestMethod, success, fail, }; diff --git a/test/request/dispatchRequest.test.ts b/test/request/dispatchRequest.test.ts index 0b9435a..53a53d2 100644 --- a/test/request/dispatchRequest.test.ts +++ b/test/request/dispatchRequest.test.ts @@ -38,18 +38,6 @@ describe('src/request/dispatchRequest.ts', () => { ).not.toThrowError(); }); - testEachMethods('应该支持 %s 转全大写', (k) => { - const c = { - adapter: mockAdapter(), - url: '/', - method: k, - }; - - dispatchRequest(c); - - expect(c.method).toBe(k.toUpperCase()); - }); - test('坏的适配器应该抛出异常', () => { expect( dispatchRequest({ @@ -64,7 +52,7 @@ describe('src/request/dispatchRequest.ts', () => { "config": { "adapter": [Function], "headers": {}, - "method": "GET", + "method": "get", "url": "/", }, "request": undefined, @@ -72,7 +60,7 @@ describe('src/request/dispatchRequest.ts', () => { "config": { "adapter": [Function], "headers": {}, - "method": "GET", + "method": "get", "url": "/", }, "data": undefined, diff --git a/test/request/request.test.ts b/test/request/request.test.ts index db05a77..1153e22 100644 --- a/test/request/request.test.ts +++ b/test/request/request.test.ts @@ -18,6 +18,7 @@ describe('src/request/request.ts', () => { }, baseURL: 'http://api.com', url: 'test', + method: 'get' as const, }; const c2 = { adapter(config: AnyObject) { @@ -25,6 +26,7 @@ describe('src/request/request.ts', () => { }, baseURL: 'http://api.com', url: 'test/:id', + method: 'get' as const, params: { id: 1, }, @@ -46,6 +48,18 @@ describe('src/request/request.ts', () => { request(c3); }); + testEachMethods('应该支持 %s 转全大写', (k) => { + const c = { + adapter(config: AnyObject) { + expect(config.method).toBe(k.toUpperCase()); + }, + url: '/', + method: k, + }; + + request(c); + }); + testEachMethods('%s 方法应该返回正确的响应体结构', (k) => { const c = { adapter: mockAdapter(),