From c107171eba69a129eddad34861611837a0efac25 Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Thu, 27 Apr 2023 10:34:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=85=8D=E7=BD=AE=E5=8E=9F=E5=A7=8B?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=96=B9=E6=B3=95=E4=B8=A2=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/request/dispatchRequest.ts | 1 - src/request/request.ts | 4 +++- test/request/dispatchRequest.test.ts | 16 ++-------------- test/request/request.test.ts | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 16 deletions(-) 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(),