feat: config 增加配置项
parent
c27a7da480
commit
85b1f96eee
|
@ -53,6 +53,7 @@ export interface AxiosRequestConfig {
|
|||
cancelToken?: CancelToken;
|
||||
data?: AxiosRequestData | AxiosRequestFormData | AxiosRequestFormData;
|
||||
dataType?: 'json' | '其他';
|
||||
download?: boolean;
|
||||
enableHttp2?: boolean;
|
||||
enableQuic?: boolean;
|
||||
enableCache?: boolean;
|
||||
|
@ -68,6 +69,7 @@ export interface AxiosRequestConfig {
|
|||
transformRequest?: AxiosTransformer | AxiosTransformer[];
|
||||
transformResponse?: AxiosTransformer | AxiosTransformer[];
|
||||
timeout?: number;
|
||||
upload?: boolean;
|
||||
url?: string;
|
||||
validateStatus?: (status: number) => boolean;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import {
|
|||
isEmptyArray,
|
||||
isFunction,
|
||||
isPlainObject,
|
||||
isString,
|
||||
isUndefined,
|
||||
} from '../helpers/is';
|
||||
import { assert, throwError } from '../helpers/utils';
|
||||
|
@ -15,6 +16,8 @@ import {
|
|||
AxiosResponseError,
|
||||
} from './Axios';
|
||||
|
||||
export type AdapterRequestType = 'request' | 'download' | 'upload';
|
||||
|
||||
export type AdapterRequestMethod =
|
||||
| 'OPTIONS'
|
||||
| 'GET'
|
||||
|
@ -25,8 +28,6 @@ export type AdapterRequestMethod =
|
|||
| 'TRACE'
|
||||
| 'CONNECT';
|
||||
|
||||
export type AdapterRequestType = 'request' | 'download' | 'upload';
|
||||
|
||||
export interface AxiosAdapterRequestConfig extends AxiosRequestConfig {
|
||||
type: AdapterRequestType;
|
||||
method: AdapterRequestMethod;
|
||||
|
@ -187,6 +188,16 @@ export function createAdapter(platform: AxiosPlatform): AxiosAdapter {
|
|||
upload: AxiosAdapterUpload,
|
||||
config: AxiosAdapterRequestConfig,
|
||||
): AxiosAdapterTask | void {
|
||||
assert(isPlainObject(config.data), '上传文件时 data 需要是一个 object');
|
||||
assert(
|
||||
isString(config.data!.fileName),
|
||||
'上传文件时 data.fileName 需要是一个 string',
|
||||
);
|
||||
assert(
|
||||
isString(config.data!.filePath),
|
||||
'上传文件时 data.filePath 需要是一个 string',
|
||||
);
|
||||
|
||||
const {
|
||||
fileName,
|
||||
filePath,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { isPlainObject } from '../helpers/is';
|
||||
import { omit } from '../helpers/utils';
|
||||
import { omit, toLowerCase } from '../helpers/utils';
|
||||
import {
|
||||
AxiosRequestConfig,
|
||||
AxiosRequestMethodAlias,
|
||||
|
@ -10,11 +10,11 @@ export function flattenHeaders(
|
|||
config: AxiosRequestConfig,
|
||||
): AxiosRequestHeaders | undefined {
|
||||
if (!isPlainObject(config.headers)) {
|
||||
return;
|
||||
return config.headers;
|
||||
}
|
||||
|
||||
const common = 'common';
|
||||
const method = config.method?.toLowerCase() ?? 'get';
|
||||
const method = toLowerCase<AxiosRequestMethodAlias>(config.method, 'get');
|
||||
const alias: AxiosRequestMethodAlias[] = [
|
||||
'options',
|
||||
'get',
|
||||
|
|
|
@ -1,37 +1,17 @@
|
|||
import { isPlainObject, isString } from 'src/helpers/is';
|
||||
import { assert } from '../helpers/utils';
|
||||
import { toLowerCase } from '../helpers/utils';
|
||||
import { AdapterRequestType } from './adapter';
|
||||
import { AxiosRequestConfig } from './Axios';
|
||||
import { AxiosRequestConfig, AxiosRequestMethodAlias } from './Axios';
|
||||
|
||||
export function generateType(config: AxiosRequestConfig): AdapterRequestType {
|
||||
let requestType: AdapterRequestType = 'request';
|
||||
|
||||
if (
|
||||
!isPlainObject(config.headers) ||
|
||||
!/multipart\/form-data/.test(
|
||||
config.headers['Content-Type'] ?? config.headers['content-type'] ?? '',
|
||||
)
|
||||
) {
|
||||
return requestType;
|
||||
}
|
||||
|
||||
const method = config.method?.toLowerCase() ?? 'get';
|
||||
|
||||
if (method === 'post') {
|
||||
assert(isPlainObject(config.data), '上传文件时 data 需要是一个 object');
|
||||
assert(
|
||||
isString(config.data!.fileName),
|
||||
'上传文件时 data.fileName 需要是一个 string',
|
||||
);
|
||||
assert(
|
||||
isString(config.data!.filePath),
|
||||
'上传文件时 data.filePath 需要是一个 string',
|
||||
);
|
||||
const method = toLowerCase<AxiosRequestMethodAlias>(config.method, 'get');
|
||||
|
||||
if (config.upload && method === 'post') {
|
||||
requestType = 'upload';
|
||||
}
|
||||
|
||||
if (method === 'get') {
|
||||
if (config.download && method === 'get') {
|
||||
requestType = 'download';
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { isFunction, isPlainObject } from '../helpers/is';
|
||||
import { assert } from '../helpers/utils';
|
||||
import { assert, toUpperCase } from '../helpers/utils';
|
||||
import {
|
||||
AxiosAdapterRequestConfig,
|
||||
AdapterRequestMethod,
|
||||
|
@ -41,7 +41,7 @@ export function request<TData = any>(
|
|||
...config,
|
||||
url: config.url ?? '',
|
||||
type: generateType(config),
|
||||
method: (config.method?.toUpperCase() as AdapterRequestMethod) ?? 'GET',
|
||||
method: toUpperCase<AdapterRequestMethod>(config.method, 'GET'),
|
||||
success,
|
||||
fail,
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { isPlainObject } from './is';
|
||||
import { isPlainObject, isString } from './is';
|
||||
|
||||
export function deepMerge<T = any>(...objs: T[]): T {
|
||||
const result: AnyObject = {};
|
||||
|
@ -52,3 +52,19 @@ 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)) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return value.toLowerCase() as T;
|
||||
}
|
||||
|
||||
export function toUpperCase<T extends string>(value: any, defaultValue: T): T {
|
||||
if (!isString(value)) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return value.toUpperCase() as T;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue