From 021e05233a95ebc5c5f93d114d225cc45a22675f Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Sun, 16 Apr 2023 13:43:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Proxy=20uni=20?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/adapter.ts | 68 ++++++++++++++++++++---------------- src/helpers/isTypes.ts | 4 --- test/helpers/isTypes.test.ts | 7 ---- 3 files changed, 38 insertions(+), 41 deletions(-) diff --git a/src/adapter.ts b/src/adapter.ts index c48ea78..65967a5 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -1,5 +1,4 @@ import { - isEmptyArray, isFunction, isPlainObject, isString, @@ -161,31 +160,48 @@ export interface AxiosAdapter { } export function getAdapterDefault() { - const tryGetPlatforms = [ - () => uni, - () => wx, - () => my, - () => swan, - () => tt, - () => qq, - () => qh, - () => ks, - () => dd, - () => jd, - ]; + const platform = revisePlatformApiNames(getPlatform()); - let platform; - while (!isEmptyArray(tryGetPlatforms) && !isPlatform(platform)) { - try { - const tryGetPlatform = tryGetPlatforms.shift(); - if (isPlainObject((platform = tryGetPlatform!()))) { - platform = revisePlatformApiNames(platform); - } - } catch (err) { - // 避免出现异常导致程序被终止 + function getPlatform() { + const undef = 'undefined'; + + if (typeof uni !== undef) { + return { + request: uni.request, + uploadFile: uni.uploadFile, + downloadFile: uni.downloadFile, + }; + } else if (typeof wx !== undef) { + return wx; + } else if (typeof my !== undef) { + return my; + } else if (typeof swan !== undef) { + return swan; + } else if (typeof tt !== undef) { + return tt; + } else if (typeof qq !== undef) { + return qq; + } else if (typeof qh !== undef) { + return qh; + } else if (typeof ks !== undef) { + return ks; + } else if (typeof dd !== undef) { + return dd; + } else if (typeof jd !== undef) { + return jd; } } + function revisePlatformApiNames(platform?: AnyObject) { + return ( + platform && { + request: platform.request ?? platform.httpRequest, + upload: platform.upload ?? platform.uploadFile, + download: platform.download ?? platform.downloadFile, + } + ); + } + if (!isPlatform(platform)) { return; } @@ -329,11 +345,3 @@ export function isPlatform(value: unknown): value is AxiosPlatform { isFunction(value.download) ); } - -export function revisePlatformApiNames(platform: AnyObject): AxiosPlatform { - return { - request: platform.request ?? platform.httpRequest, - upload: platform.upload ?? platform.uploadFile, - download: platform.download ?? platform.downloadFile, - }; -} diff --git a/src/helpers/isTypes.ts b/src/helpers/isTypes.ts index 28e7743..e49d6d1 100644 --- a/src/helpers/isTypes.ts +++ b/src/helpers/isTypes.ts @@ -22,10 +22,6 @@ export function isArray(value: any): value is T[] { return Array.isArray(value); } -export function isEmptyArray(value: any): value is [] { - return isArray(value) && value.length === 0; -} - export function isDate(date: any): date is Date { return _toString.call(date) === '[object Date]'; } diff --git a/test/helpers/isTypes.test.ts b/test/helpers/isTypes.test.ts index 3181d4f..47342dc 100644 --- a/test/helpers/isTypes.test.ts +++ b/test/helpers/isTypes.test.ts @@ -2,7 +2,6 @@ import { describe, test, expect } from 'vitest'; import { isArray, isDate, - isEmptyArray, isPlainObject, isFunction, isNull, @@ -18,12 +17,6 @@ describe('src/helpers/isTypes.ts', () => { expect(isArray([1])).toBeTruthy(); }); - test('应该能判断是空数组', () => { - expect(isEmptyArray([1])).toBeFalsy(); - expect(isArray(new Array(0))).toBeTruthy(); - expect(isEmptyArray([])).toBeTruthy(); - }); - test('应该能判断是普通对象', () => { expect(isPlainObject(new String())).toBeFalsy(); expect(isPlainObject(new Function())).toBeFalsy();