Merge pull request #18 from fluffff/config

feat: config 增加配置项
pull/19/head
初秋 2021-05-26 20:22:57 +08:00 committed by GitHub
commit 1489b19eb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 33 deletions

View File

@ -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;
}

View File

@ -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,

View File

@ -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',

View File

@ -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';
}

View File

@ -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,
};

View File

@ -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;
}