diff --git a/src/core/Axios.ts b/src/core/Axios.ts index c76c733..4c17da2 100644 --- a/src/core/Axios.ts +++ b/src/core/Axios.ts @@ -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; } diff --git a/src/core/adapter.ts b/src/core/adapter.ts index 77551b8..c64c722 100644 --- a/src/core/adapter.ts +++ b/src/core/adapter.ts @@ -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, diff --git a/src/core/flattenHeaders.ts b/src/core/flattenHeaders.ts index 71db3c1..982c3b5 100644 --- a/src/core/flattenHeaders.ts +++ b/src/core/flattenHeaders.ts @@ -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(config.method, 'get'); const alias: AxiosRequestMethodAlias[] = [ 'options', 'get', diff --git a/src/core/generateType.ts b/src/core/generateType.ts index dbdb642..f807df8 100644 --- a/src/core/generateType.ts +++ b/src/core/generateType.ts @@ -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(config.method, 'get'); + if (config.upload && method === 'post') { requestType = 'upload'; } - if (method === 'get') { + if (config.download && method === 'get') { requestType = 'download'; } diff --git a/src/core/request.ts b/src/core/request.ts index 27f1095..cb78902 100644 --- a/src/core/request.ts +++ b/src/core/request.ts @@ -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( ...config, url: config.url ?? '', type: generateType(config), - method: (config.method?.toUpperCase() as AdapterRequestMethod) ?? 'GET', + method: toUpperCase(config.method, 'GET'), success, fail, }; diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 6854f1c..47b6320 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -1,4 +1,4 @@ -import { isPlainObject } from './is'; +import { isPlainObject, isString } from './is'; export function deepMerge(...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(value: any, defaultValue: T): T { + if (!isString(value)) { + return defaultValue; + } + + return value.toLowerCase() as T; +} + +export function toUpperCase(value: any, defaultValue: T): T { + if (!isString(value)) { + return defaultValue; + } + + return value.toUpperCase() as T; +}