feat: config 增加配置项

pull/18/head
954270063@qq.com 2021-05-26 20:21:37 +08:00
parent c27a7da480
commit 85b1f96eee
6 changed files with 42 additions and 33 deletions

View File

@ -53,6 +53,7 @@ export interface AxiosRequestConfig {
cancelToken?: CancelToken; cancelToken?: CancelToken;
data?: AxiosRequestData | AxiosRequestFormData | AxiosRequestFormData; data?: AxiosRequestData | AxiosRequestFormData | AxiosRequestFormData;
dataType?: 'json' | '其他'; dataType?: 'json' | '其他';
download?: boolean;
enableHttp2?: boolean; enableHttp2?: boolean;
enableQuic?: boolean; enableQuic?: boolean;
enableCache?: boolean; enableCache?: boolean;
@ -68,6 +69,7 @@ export interface AxiosRequestConfig {
transformRequest?: AxiosTransformer | AxiosTransformer[]; transformRequest?: AxiosTransformer | AxiosTransformer[];
transformResponse?: AxiosTransformer | AxiosTransformer[]; transformResponse?: AxiosTransformer | AxiosTransformer[];
timeout?: number; timeout?: number;
upload?: boolean;
url?: string; url?: string;
validateStatus?: (status: number) => boolean; validateStatus?: (status: number) => boolean;
} }

View File

@ -2,6 +2,7 @@ import {
isEmptyArray, isEmptyArray,
isFunction, isFunction,
isPlainObject, isPlainObject,
isString,
isUndefined, isUndefined,
} from '../helpers/is'; } from '../helpers/is';
import { assert, throwError } from '../helpers/utils'; import { assert, throwError } from '../helpers/utils';
@ -15,6 +16,8 @@ import {
AxiosResponseError, AxiosResponseError,
} from './Axios'; } from './Axios';
export type AdapterRequestType = 'request' | 'download' | 'upload';
export type AdapterRequestMethod = export type AdapterRequestMethod =
| 'OPTIONS' | 'OPTIONS'
| 'GET' | 'GET'
@ -25,8 +28,6 @@ export type AdapterRequestMethod =
| 'TRACE' | 'TRACE'
| 'CONNECT'; | 'CONNECT';
export type AdapterRequestType = 'request' | 'download' | 'upload';
export interface AxiosAdapterRequestConfig extends AxiosRequestConfig { export interface AxiosAdapterRequestConfig extends AxiosRequestConfig {
type: AdapterRequestType; type: AdapterRequestType;
method: AdapterRequestMethod; method: AdapterRequestMethod;
@ -187,6 +188,16 @@ export function createAdapter(platform: AxiosPlatform): AxiosAdapter {
upload: AxiosAdapterUpload, upload: AxiosAdapterUpload,
config: AxiosAdapterRequestConfig, config: AxiosAdapterRequestConfig,
): AxiosAdapterTask | void { ): 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 { const {
fileName, fileName,
filePath, filePath,

View File

@ -1,5 +1,5 @@
import { isPlainObject } from '../helpers/is'; import { isPlainObject } from '../helpers/is';
import { omit } from '../helpers/utils'; import { omit, toLowerCase } from '../helpers/utils';
import { import {
AxiosRequestConfig, AxiosRequestConfig,
AxiosRequestMethodAlias, AxiosRequestMethodAlias,
@ -10,11 +10,11 @@ export function flattenHeaders(
config: AxiosRequestConfig, config: AxiosRequestConfig,
): AxiosRequestHeaders | undefined { ): AxiosRequestHeaders | undefined {
if (!isPlainObject(config.headers)) { if (!isPlainObject(config.headers)) {
return; return config.headers;
} }
const common = 'common'; const common = 'common';
const method = config.method?.toLowerCase() ?? 'get'; const method = toLowerCase<AxiosRequestMethodAlias>(config.method, 'get');
const alias: AxiosRequestMethodAlias[] = [ const alias: AxiosRequestMethodAlias[] = [
'options', 'options',
'get', 'get',

View File

@ -1,37 +1,17 @@
import { isPlainObject, isString } from 'src/helpers/is'; import { toLowerCase } from '../helpers/utils';
import { assert } from '../helpers/utils';
import { AdapterRequestType } from './adapter'; import { AdapterRequestType } from './adapter';
import { AxiosRequestConfig } from './Axios'; import { AxiosRequestConfig, AxiosRequestMethodAlias } from './Axios';
export function generateType(config: AxiosRequestConfig): AdapterRequestType { export function generateType(config: AxiosRequestConfig): AdapterRequestType {
let requestType: AdapterRequestType = 'request'; let requestType: AdapterRequestType = 'request';
if ( const method = toLowerCase<AxiosRequestMethodAlias>(config.method, 'get');
!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',
);
if (config.upload && method === 'post') {
requestType = 'upload'; requestType = 'upload';
} }
if (method === 'get') { if (config.download && method === 'get') {
requestType = 'download'; requestType = 'download';
} }

View File

@ -1,5 +1,5 @@
import { isFunction, isPlainObject } from '../helpers/is'; import { isFunction, isPlainObject } from '../helpers/is';
import { assert } from '../helpers/utils'; import { assert, toUpperCase } from '../helpers/utils';
import { import {
AxiosAdapterRequestConfig, AxiosAdapterRequestConfig,
AdapterRequestMethod, AdapterRequestMethod,
@ -41,7 +41,7 @@ export function request<TData = any>(
...config, ...config,
url: config.url ?? '', url: config.url ?? '',
type: generateType(config), type: generateType(config),
method: (config.method?.toUpperCase() as AdapterRequestMethod) ?? 'GET', method: toUpperCase<AdapterRequestMethod>(config.method, 'GET'),
success, success,
fail, fail,
}; };

View File

@ -1,4 +1,4 @@
import { isPlainObject } from './is'; import { isPlainObject, isString } from './is';
export function deepMerge<T = any>(...objs: T[]): T { export function deepMerge<T = any>(...objs: T[]): T {
const result: AnyObject = {}; const result: AnyObject = {};
@ -52,3 +52,19 @@ 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)) {
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;
}