feat: 支持京东小程序
parent
b15b31ee55
commit
0d1d21fc66
|
@ -8,6 +8,7 @@
|
||||||
"plugins": ["@typescript-eslint"],
|
"plugins": ["@typescript-eslint"],
|
||||||
"root": true,
|
"root": true,
|
||||||
"rules": {
|
"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 qh: unknown;
|
||||||
declare const ks: unknown;
|
declare const ks: unknown;
|
||||||
declare const dd: unknown;
|
declare const dd: unknown;
|
||||||
|
declare const jd: unknown;
|
||||||
|
|
||||||
type AnyObject<T = any> = Record<string, T>;
|
type AnyObject<T = any> = Record<string, T>;
|
||||||
|
|
|
@ -140,6 +140,7 @@ export function getAdapterDefault(): AxiosAdapter | undefined {
|
||||||
() => qh,
|
() => qh,
|
||||||
() => ks,
|
() => ks,
|
||||||
() => dd,
|
() => dd,
|
||||||
|
() => jd,
|
||||||
];
|
];
|
||||||
|
|
||||||
let platform;
|
let platform;
|
||||||
|
|
|
@ -17,6 +17,7 @@ export default function dispatchRequest<TData = unknown>(
|
||||||
): Promise<AxiosResponse> {
|
): Promise<AxiosResponse> {
|
||||||
throwIfCancellationRequested(config);
|
throwIfCancellationRequested(config);
|
||||||
|
|
||||||
|
config.method = config.method ?? 'get';
|
||||||
config.url = transformURL(config);
|
config.url = transformURL(config);
|
||||||
config.headers = flattenHeaders(config);
|
config.headers = flattenHeaders(config);
|
||||||
config.data = transformData(
|
config.data = transformData(
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
import { isPlainObject } from '../helpers/is';
|
import { isPlainObject } from '../helpers/is';
|
||||||
import { omit, toLowerCase } from '../helpers/utils';
|
import { omit } from '../helpers/utils';
|
||||||
import {
|
import { AxiosRequestConfig, AxiosRequestHeaders } from './Axios';
|
||||||
AxiosRequestConfig,
|
|
||||||
AxiosRequestMethodAlias,
|
|
||||||
AxiosRequestHeaders,
|
|
||||||
} from './Axios';
|
|
||||||
|
|
||||||
export function flattenHeaders(
|
export function flattenHeaders(
|
||||||
config: AxiosRequestConfig,
|
config: AxiosRequestConfig,
|
||||||
|
@ -15,9 +11,7 @@ export function flattenHeaders(
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...(config.headers.common ?? {}),
|
...(config.headers.common ?? {}),
|
||||||
...(config.headers[
|
...(config.headers[config.method!.toLowerCase()] ?? {}),
|
||||||
toLowerCase<AxiosRequestMethodAlias>(config.method, 'get')
|
|
||||||
] ?? {}),
|
|
||||||
...omit(
|
...omit(
|
||||||
config.headers,
|
config.headers,
|
||||||
'common',
|
'common',
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
import { toLowerCase } from '../helpers/utils';
|
|
||||||
import { AxiosAdapterRequestType } from '../adapter';
|
import { AxiosAdapterRequestType } from '../adapter';
|
||||||
import { AxiosRequestConfig, AxiosRequestMethodAlias } from './Axios';
|
import { AxiosRequestConfig } from './Axios';
|
||||||
|
|
||||||
export function generateType(
|
export function generateType(
|
||||||
config: AxiosRequestConfig,
|
config: AxiosRequestConfig,
|
||||||
): AxiosAdapterRequestType {
|
): AxiosAdapterRequestType {
|
||||||
let requestType: AxiosAdapterRequestType = 'request';
|
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';
|
requestType = 'upload';
|
||||||
}
|
}
|
||||||
if (config.download && method === 'get') {
|
if (config.download && method === 'GET') {
|
||||||
requestType = 'download';
|
requestType = 'download';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { isFunction, isPlainObject } from '../helpers/is';
|
import { isFunction, isPlainObject } from '../helpers/is';
|
||||||
import { assert, toUpperCase } from '../helpers/utils';
|
import { assert } from '../helpers/utils';
|
||||||
import {
|
import {
|
||||||
AxiosAdapterRequestConfig,
|
AxiosAdapterRequestConfig,
|
||||||
AxiosAdapterRequestMethod,
|
AxiosAdapterRequestMethod,
|
||||||
|
@ -46,7 +46,7 @@ export function request<TData = unknown>(
|
||||||
...config,
|
...config,
|
||||||
url: config.url ?? '',
|
url: config.url ?? '',
|
||||||
type: generateType(config),
|
type: generateType(config),
|
||||||
method: toUpperCase<AxiosAdapterRequestMethod>(config.method, 'GET'),
|
method: config.method!.toUpperCase() as AxiosAdapterRequestMethod,
|
||||||
success,
|
success,
|
||||||
fail,
|
fail,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { isPlainObject, isString } from './is';
|
import { isPlainObject } from './is';
|
||||||
|
|
||||||
export function deepMerge<T extends AnyObject>(...objs: T[]): T {
|
export function deepMerge<T extends AnyObject>(...objs: T[]): T {
|
||||||
const result: AnyObject = {};
|
const result: AnyObject = {};
|
||||||
|
@ -52,19 +52,3 @@ export function assert(condition: boolean, msg: string) {
|
||||||
export function throwError(msg: string): void {
|
export function throwError(msg: string): void {
|
||||||
throw new Error(`[axios-miniprogram]: ${msg}`);
|
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,
|
omit,
|
||||||
pick,
|
pick,
|
||||||
throwError,
|
throwError,
|
||||||
toLowerCase,
|
|
||||||
toUpperCase,
|
|
||||||
} from '../../src/helpers/utils';
|
} from '../../src/helpers/utils';
|
||||||
|
|
||||||
describe('对 src/helpers/utils.ts 进行测试', () => {
|
describe('对 src/helpers/utils.ts 进行测试', () => {
|
||||||
|
@ -49,18 +47,4 @@ describe('对 src/helpers/utils.ts 进行测试', () => {
|
||||||
'[axios-miniprogram]: msg ',
|
'[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