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 { import {
isEmptyArray,
isFunction, isFunction,
isPlainObject, isPlainObject,
isString, isString,
@ -161,31 +160,48 @@ export interface AxiosAdapter {
} }
export function getAdapterDefault() { export function getAdapterDefault() {
const tryGetPlatforms = [ const platform = revisePlatformApiNames(getPlatform());
() => uni,
() => wx,
() => my,
() => swan,
() => tt,
() => qq,
() => qh,
() => ks,
() => dd,
() => jd,
];
let platform; function getPlatform() {
while (!isEmptyArray(tryGetPlatforms) && !isPlatform(platform)) { const undef = 'undefined';
try {
const tryGetPlatform = tryGetPlatforms.shift(); if (typeof uni !== undef) {
if (isPlainObject((platform = tryGetPlatform!()))) { return {
platform = revisePlatformApiNames(platform); request: uni.request,
} uploadFile: uni.uploadFile,
} catch (err) { 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)) { if (!isPlatform(platform)) {
return; return;
} }
@ -329,11 +345,3 @@ export function isPlatform(value: unknown): value is AxiosPlatform {
isFunction(value.download) 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); 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 { export function isDate(date: any): date is Date {
return _toString.call(date) === '[object Date]'; return _toString.call(date) === '[object Date]';
} }

View File

@ -2,7 +2,6 @@ import { describe, test, expect } from 'vitest';
import { import {
isArray, isArray,
isDate, isDate,
isEmptyArray,
isPlainObject, isPlainObject,
isFunction, isFunction,
isNull, isNull,
@ -18,12 +17,6 @@ describe('src/helpers/isTypes.ts', () => {
expect(isArray([1])).toBeTruthy(); expect(isArray([1])).toBeTruthy();
}); });
test('应该能判断是空数组', () => {
expect(isEmptyArray([1])).toBeFalsy();
expect(isArray(new Array(0))).toBeTruthy();
expect(isEmptyArray([])).toBeTruthy();
});
test('应该能判断是普通对象', () => { test('应该能判断是普通对象', () => {
expect(isPlainObject(new String())).toBeFalsy(); expect(isPlainObject(new String())).toBeFalsy();
expect(isPlainObject(new Function())).toBeFalsy(); expect(isPlainObject(new Function())).toBeFalsy();