2023-04-18 11:14:57 +08:00
|
|
|
import { isFunction, isPlainObject, isUndefined } from '../helpers/isTypes';
|
2023-03-23 20:09:00 +08:00
|
|
|
import {
|
|
|
|
AxiosAdapterRequestConfig,
|
|
|
|
AxiosAdapterRequestMethod,
|
2023-04-05 13:31:48 +08:00
|
|
|
AxiosAdapterResponse,
|
|
|
|
AxiosAdapterResponseError,
|
2023-04-11 13:13:45 +08:00
|
|
|
AxiosAdapterTask,
|
2023-03-24 20:15:51 +08:00
|
|
|
} from '../adapter';
|
2023-03-23 20:09:00 +08:00
|
|
|
import {
|
|
|
|
AxiosProgressCallback,
|
|
|
|
AxiosRequestConfig,
|
|
|
|
AxiosResponse,
|
|
|
|
AxiosResponseError,
|
|
|
|
} from './Axios';
|
|
|
|
import { isCancelToken } from './cancel';
|
|
|
|
import { AxiosErrorResponse, createError } from './createError';
|
|
|
|
import { generateType } from './generateType';
|
2023-04-16 13:39:10 +08:00
|
|
|
import AxiosDomain from './AxiosDomain';
|
2023-03-23 20:09:00 +08:00
|
|
|
|
|
|
|
function tryToggleProgressUpdate(
|
|
|
|
adapterConfig: AxiosAdapterRequestConfig,
|
|
|
|
progressUpdate?: (callback: AxiosProgressCallback) => void,
|
|
|
|
) {
|
2023-03-28 21:32:54 +08:00
|
|
|
const { onUploadProgress, onDownloadProgress } = adapterConfig;
|
2023-03-23 20:09:00 +08:00
|
|
|
if (isFunction(progressUpdate)) {
|
|
|
|
switch (adapterConfig.type) {
|
|
|
|
case 'upload':
|
2023-03-28 21:32:54 +08:00
|
|
|
if (isFunction(onUploadProgress)) {
|
|
|
|
progressUpdate(onUploadProgress);
|
2023-03-23 20:09:00 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'download':
|
2023-03-28 21:32:54 +08:00
|
|
|
if (isFunction(onDownloadProgress)) {
|
|
|
|
progressUpdate(onDownloadProgress);
|
2023-03-23 20:09:00 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-16 13:39:10 +08:00
|
|
|
/**
|
|
|
|
* 可以携带 data 的请求方法
|
|
|
|
*/
|
|
|
|
export const withDataRE = new RegExp(`^${AxiosDomain.asd.join('|')}$`, 'i');
|
|
|
|
|
2023-04-09 15:20:10 +08:00
|
|
|
export function request(config: AxiosRequestConfig) {
|
|
|
|
return new Promise<AxiosResponse>((resolve, reject) => {
|
2023-04-09 21:01:43 +08:00
|
|
|
const { adapter, url, method, cancelToken } = config;
|
2023-03-23 20:09:00 +08:00
|
|
|
|
2023-04-16 13:39:10 +08:00
|
|
|
if (!withDataRE.test(method!)) {
|
|
|
|
delete config.data;
|
|
|
|
}
|
|
|
|
|
2023-03-23 20:09:00 +08:00
|
|
|
const adapterConfig: AxiosAdapterRequestConfig = {
|
|
|
|
...config,
|
2023-04-09 21:01:43 +08:00
|
|
|
url: url!,
|
2023-03-23 20:09:00 +08:00
|
|
|
type: generateType(config),
|
2023-04-09 21:01:43 +08:00
|
|
|
method: method!.toUpperCase() as AxiosAdapterRequestMethod,
|
2023-03-23 20:09:00 +08:00
|
|
|
success,
|
|
|
|
fail,
|
|
|
|
};
|
|
|
|
|
2023-04-11 13:13:45 +08:00
|
|
|
let adapterTask: AxiosAdapterTask;
|
|
|
|
try {
|
|
|
|
adapterTask = adapter!(adapterConfig);
|
|
|
|
} catch {
|
|
|
|
fail({
|
|
|
|
status: 400,
|
|
|
|
statusText: 'Bad Adapter',
|
|
|
|
headers: {},
|
|
|
|
});
|
|
|
|
}
|
2023-03-23 20:09:00 +08:00
|
|
|
|
2023-04-09 15:20:10 +08:00
|
|
|
function success(_: AxiosAdapterResponse): void {
|
|
|
|
const response = _ as AxiosResponse;
|
2023-03-23 20:09:00 +08:00
|
|
|
response.config = config;
|
|
|
|
response.request = adapterTask;
|
2023-04-09 15:20:10 +08:00
|
|
|
|
2023-04-18 11:14:57 +08:00
|
|
|
if (isUndefined(response.status)) {
|
|
|
|
response.status = 200;
|
|
|
|
}
|
|
|
|
if (isUndefined(response.statusText)) {
|
|
|
|
response.statusText = 'OK';
|
|
|
|
}
|
|
|
|
if (isUndefined(response.headers)) {
|
|
|
|
response.headers = {};
|
|
|
|
}
|
|
|
|
|
2023-04-07 21:24:23 +08:00
|
|
|
if (config.validateStatus?.(response.status) ?? true) {
|
2023-03-23 20:09:00 +08:00
|
|
|
resolve(response);
|
|
|
|
} else {
|
2023-04-07 21:24:23 +08:00
|
|
|
catchError('validate status fail', response);
|
2023-03-23 20:09:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:31:48 +08:00
|
|
|
function fail(_: AxiosAdapterResponseError): void {
|
2023-04-05 16:35:25 +08:00
|
|
|
const responseError = _ as AxiosResponseError;
|
|
|
|
responseError.isFail = true;
|
|
|
|
responseError.config = config;
|
|
|
|
responseError.request = adapterTask;
|
2023-04-18 11:14:57 +08:00
|
|
|
|
|
|
|
if (isUndefined(responseError.status)) {
|
|
|
|
responseError.status = 400;
|
|
|
|
}
|
|
|
|
if (isUndefined(responseError.statusText)) {
|
|
|
|
responseError.statusText = 'Fail Adapter';
|
|
|
|
}
|
|
|
|
if (isUndefined(responseError.headers)) {
|
|
|
|
responseError.headers = {};
|
|
|
|
}
|
|
|
|
|
2023-04-09 15:20:10 +08:00
|
|
|
catchError('request fail', responseError);
|
2023-03-23 20:09:00 +08:00
|
|
|
}
|
|
|
|
|
2023-04-05 16:35:25 +08:00
|
|
|
function catchError(
|
|
|
|
message: string,
|
|
|
|
errorResponse: AxiosErrorResponse,
|
|
|
|
): void {
|
|
|
|
reject(createError(message, config, errorResponse, adapterTask));
|
2023-03-23 20:09:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isPlainObject(adapterTask)) {
|
|
|
|
tryToggleProgressUpdate(adapterConfig, adapterTask.onProgressUpdate);
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:27:45 +08:00
|
|
|
if (isCancelToken(cancelToken)) {
|
|
|
|
cancelToken.onCancel((reason) => {
|
2023-03-23 20:09:00 +08:00
|
|
|
if (isPlainObject(adapterTask)) {
|
|
|
|
tryToggleProgressUpdate(adapterConfig, adapterTask.offProgressUpdate);
|
|
|
|
|
2023-04-07 21:24:23 +08:00
|
|
|
adapterTask?.abort?.();
|
2023-03-23 20:09:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
reject(reason);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|