feat: 支持京东小程序
parent
b15b31ee55
commit
0d1d21fc66
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<T = any> = Record<string, T>;
|
||||
|
|
|
@ -140,6 +140,7 @@ export function getAdapterDefault(): AxiosAdapter | undefined {
|
|||
() => qh,
|
||||
() => ks,
|
||||
() => dd,
|
||||
() => jd,
|
||||
];
|
||||
|
||||
let platform;
|
||||
|
|
|
@ -17,6 +17,7 @@ export default function dispatchRequest<TData = unknown>(
|
|||
): Promise<AxiosResponse> {
|
||||
throwIfCancellationRequested(config);
|
||||
|
||||
config.method = config.method ?? 'get';
|
||||
config.url = transformURL(config);
|
||||
config.headers = flattenHeaders(config);
|
||||
config.data = transformData(
|
||||
|
|
|
@ -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<AxiosRequestMethodAlias>(config.method, 'get')
|
||||
] ?? {}),
|
||||
...(config.headers[config.method!.toLowerCase()] ?? {}),
|
||||
...omit(
|
||||
config.headers,
|
||||
'common',
|
||||
|
|
|
@ -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<AxiosRequestMethodAlias>(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';
|
||||
}
|
||||
|
||||
|
|
|
@ -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<TData = unknown>(
|
|||
...config,
|
||||
url: config.url ?? '',
|
||||
type: generateType(config),
|
||||
method: toUpperCase<AxiosAdapterRequestMethod>(config.method, 'GET'),
|
||||
method: config.method!.toUpperCase() as AxiosAdapterRequestMethod,
|
||||
success,
|
||||
fail,
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { isPlainObject, isString } from './is';
|
||||
import { isPlainObject } from './is';
|
||||
|
||||
export function deepMerge<T extends AnyObject>(...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<T extends string>(value: any, defaultValue: T): T {
|
||||
if (!isString(value)) {
|
||||
value = defaultValue;
|
||||
}
|
||||
|
||||
return value.toLowerCase() as T;
|
||||
}
|
||||
|
||||
export function toUpperCase<T extends string>(value: any, defaultValue: T): T {
|
||||
if (!isString(value)) {
|
||||
value = defaultValue;
|
||||
}
|
||||
|
||||
return value.toUpperCase() as T;
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue