Skip to content

Commit

Permalink
feat: 支持京东小程序
Browse files Browse the repository at this point in the history
  • Loading branch information
zjxxxxxxxxx committed Mar 28, 2023
1 parent b15b31e commit 0d1d21f
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Expand Up @@ -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
}
}
1 change: 1 addition & 0 deletions global.d.ts
Expand Up @@ -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>;
1 change: 1 addition & 0 deletions src/adapter.ts
Expand Up @@ -140,6 +140,7 @@ export function getAdapterDefault(): AxiosAdapter | undefined {
() => qh,
() => ks,
() => dd,
() => jd,
];

let platform;
Expand Down
1 change: 1 addition & 0 deletions src/core/dispatchRequest.ts
Expand Up @@ -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(
Expand Down
12 changes: 3 additions & 9 deletions src/core/flattenHeaders.ts
@@ -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,
Expand All @@ -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',
Expand Down
9 changes: 4 additions & 5 deletions src/core/generateType.ts
@@ -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';
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/request.ts
@@ -1,5 +1,5 @@
import { isFunction, isPlainObject } from '../helpers/is';
import { assert, toUpperCase } from '../helpers/utils';
import { assert } from '../helpers/utils';
import {
AxiosAdapterRequestConfig,
AxiosAdapterRequestMethod,
Expand Down Expand Up @@ -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,
};
Expand Down
18 changes: 1 addition & 17 deletions src/helpers/utils.ts
@@ -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 = {};
Expand Down Expand Up @@ -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;
}
16 changes: 0 additions & 16 deletions test/helpers/utils.test.ts
Expand Up @@ -5,8 +5,6 @@ import {
omit,
pick,
throwError,
toLowerCase,
toUpperCase,
} from '../../src/helpers/utils';

describe('对 src/helpers/utils.ts 进行测试', () => {
Expand Down Expand Up @@ -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');
});
});

0 comments on commit 0d1d21f

Please sign in to comment.