diff --git a/.eslintrc.json b/.eslintrc.json index f92d35e..01ff112 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -8,6 +8,7 @@ "plugins": ["@typescript-eslint"], "root": true, "rules": { - "@typescript-eslint/no-explicit-any": 0 + "@typescript-eslint/no-explicit-any": 0, + "@typescript-eslint/no-non-null-assertion": 0 } } diff --git a/global.d.ts b/global.d.ts index 984dd3b..9c03c7c 100644 --- a/global.d.ts +++ b/global.d.ts @@ -7,5 +7,6 @@ declare const qq: unknown; declare const qh: unknown; declare const ks: unknown; declare const dd: unknown; +declare const jd: unknown; type AnyObject = Record; diff --git a/src/adapter.ts b/src/adapter.ts index 46307ee..d6d62b5 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -140,6 +140,7 @@ export function getAdapterDefault(): AxiosAdapter | undefined { () => qh, () => ks, () => dd, + () => jd, ]; let platform; diff --git a/src/core/dispatchRequest.ts b/src/core/dispatchRequest.ts index 79af25d..6c7fd64 100644 --- a/src/core/dispatchRequest.ts +++ b/src/core/dispatchRequest.ts @@ -17,6 +17,7 @@ export default function dispatchRequest( ): Promise { throwIfCancellationRequested(config); + config.method = config.method ?? 'get'; config.url = transformURL(config); config.headers = flattenHeaders(config); config.data = transformData( diff --git a/src/core/flattenHeaders.ts b/src/core/flattenHeaders.ts index bb8f667..2b1f6e2 100644 --- a/src/core/flattenHeaders.ts +++ b/src/core/flattenHeaders.ts @@ -1,10 +1,6 @@ import { isPlainObject } from '../helpers/is'; -import { omit, toLowerCase } from '../helpers/utils'; -import { - AxiosRequestConfig, - AxiosRequestMethodAlias, - AxiosRequestHeaders, -} from './Axios'; +import { omit } from '../helpers/utils'; +import { AxiosRequestConfig, AxiosRequestHeaders } from './Axios'; export function flattenHeaders( config: AxiosRequestConfig, @@ -15,9 +11,7 @@ export function flattenHeaders( return { ...(config.headers.common ?? {}), - ...(config.headers[ - toLowerCase(config.method, 'get') - ] ?? {}), + ...(config.headers[config.method!.toLowerCase()] ?? {}), ...omit( config.headers, 'common', diff --git a/src/core/generateType.ts b/src/core/generateType.ts index 632a286..64f12ec 100644 --- a/src/core/generateType.ts +++ b/src/core/generateType.ts @@ -1,17 +1,16 @@ -import { toLowerCase } from '../helpers/utils'; import { AxiosAdapterRequestType } from '../adapter'; -import { AxiosRequestConfig, AxiosRequestMethodAlias } from './Axios'; +import { AxiosRequestConfig } from './Axios'; export function generateType( config: AxiosRequestConfig, ): AxiosAdapterRequestType { let requestType: AxiosAdapterRequestType = 'request'; - const method = toLowerCase(config.method, 'get'); - if (config.upload && method === 'post') { + const method = config.method!.toUpperCase(); + if (config.upload && method === 'POST') { requestType = 'upload'; } - if (config.download && method === 'get') { + if (config.download && method === 'GET') { requestType = 'download'; } diff --git a/src/core/request.ts b/src/core/request.ts index 2a43818..fe4bf29 100644 --- a/src/core/request.ts +++ b/src/core/request.ts @@ -1,5 +1,5 @@ import { isFunction, isPlainObject } from '../helpers/is'; -import { assert, toUpperCase } from '../helpers/utils'; +import { assert } from '../helpers/utils'; import { AxiosAdapterRequestConfig, AxiosAdapterRequestMethod, @@ -46,7 +46,7 @@ export function request( ...config, url: config.url ?? '', type: generateType(config), - method: toUpperCase(config.method, 'GET'), + method: config.method!.toUpperCase() as AxiosAdapterRequestMethod, success, fail, }; diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 02a01cf..debc9cf 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -1,4 +1,4 @@ -import { isPlainObject, isString } from './is'; +import { isPlainObject } from './is'; export function deepMerge(...objs: T[]): T { const result: AnyObject = {}; @@ -52,19 +52,3 @@ export function assert(condition: boolean, msg: string) { export function throwError(msg: string): void { throw new Error(`[axios-miniprogram]: ${msg}`); } - -export function toLowerCase(value: any, defaultValue: T): T { - if (!isString(value)) { - value = defaultValue; - } - - return value.toLowerCase() as T; -} - -export function toUpperCase(value: any, defaultValue: T): T { - if (!isString(value)) { - value = defaultValue; - } - - return value.toUpperCase() as T; -} diff --git a/test/helpers/utils.test.ts b/test/helpers/utils.test.ts index d74690a..8f6f183 100644 --- a/test/helpers/utils.test.ts +++ b/test/helpers/utils.test.ts @@ -5,8 +5,6 @@ import { omit, pick, throwError, - toLowerCase, - toUpperCase, } from '../../src/helpers/utils'; describe('对 src/helpers/utils.ts 进行测试', () => { @@ -49,18 +47,4 @@ describe('对 src/helpers/utils.ts 进行测试', () => { '[axios-miniprogram]: msg ', ); }); - - test('测试 toLowerCase() 是否符合预期', () => { - expect(toLowerCase('', 'GET')).toBe(''); - expect(toLowerCase(undefined, 'GET')).toBe('get'); - expect(toLowerCase('GET', '')).toBe('get'); - expect(toLowerCase('Get', '')).toBe('get'); - }); - - test('测试 toUpperCase() 是否符合预期', () => { - expect(toUpperCase('', 'get')).toBe(''); - expect(toUpperCase(undefined, 'get')).toBe('GET'); - expect(toUpperCase('get', '')).toBe('GET'); - expect(toUpperCase('Get', '')).toBe('GET'); - }); });