fix: 修复 Proxy uni 错误

pull/41/head
zjx0905 2023-04-16 13:43:28 +08:00
parent 2c3ff567c1
commit 021e05233a
3 changed files with 38 additions and 41 deletions

View File

@ -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,
};
}

View File

@ -22,10 +22,6 @@ export function isArray<T = unknown>(value: any): value is T[] {
return Array.isArray(value);
}
export function isEmptyArray<T = unknown>(value: any): value is [] {
return isArray<T>(value) && value.length === 0;
}
export function isDate(date: any): date is Date {
return _toString.call(date) === '[object Date]';
}

View File

@ -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();