2023-04-03 21:03:33 +08:00
|
|
|
import { isFunction, isPlainObject, isString } from '../helpers/isTypes';
|
2023-03-28 21:32:54 +08:00
|
|
|
import { assert } from '../helpers/error';
|
2023-03-23 20:09:00 +08:00
|
|
|
import {
|
|
|
|
AxiosAdapterRequestConfig,
|
|
|
|
AxiosAdapterRequestMethod,
|
2023-04-05 13:31:48 +08:00
|
|
|
AxiosAdapterResponse,
|
|
|
|
AxiosAdapterResponseError,
|
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';
|
|
|
|
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function request<TData = unknown>(
|
|
|
|
config: AxiosRequestConfig,
|
|
|
|
): Promise<AxiosResponse<TData>> {
|
|
|
|
return new Promise((resolve, reject) => {
|
2023-04-02 18:26:24 +08:00
|
|
|
assert(isFunction(config.adapter), 'adapter 不是一个 function');
|
2023-04-03 21:03:33 +08:00
|
|
|
assert(isString(config.url), 'url 不是一个 string');
|
2023-03-23 20:09:00 +08:00
|
|
|
|
|
|
|
const adapterConfig: AxiosAdapterRequestConfig = {
|
|
|
|
...config,
|
2023-04-03 21:03:33 +08:00
|
|
|
url: config.url,
|
2023-03-23 20:09:00 +08:00
|
|
|
type: generateType(config),
|
2023-03-28 20:48:02 +08:00
|
|
|
method: config.method!.toUpperCase() as AxiosAdapterRequestMethod,
|
2023-03-23 20:09:00 +08:00
|
|
|
success,
|
|
|
|
fail,
|
|
|
|
};
|
|
|
|
|
2023-04-05 16:35:25 +08:00
|
|
|
const adapterTask = config.adapter!(adapterConfig);
|
2023-03-23 20:09:00 +08:00
|
|
|
|
2023-04-05 13:31:48 +08:00
|
|
|
function success(_: AxiosAdapterResponse<TData>): void {
|
|
|
|
const response = _ as AxiosResponse<TData>;
|
2023-03-23 20:09:00 +08:00
|
|
|
response.config = config;
|
|
|
|
response.request = adapterTask;
|
|
|
|
if (
|
|
|
|
!isFunction(config.validateStatus) ||
|
|
|
|
config.validateStatus(response.status)
|
|
|
|
) {
|
|
|
|
resolve(response);
|
|
|
|
} else {
|
|
|
|
catchError('请求失败', response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
catchError('网络错误', 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
|
|
|
const { cancelToken } = config;
|
|
|
|
if (isCancelToken(cancelToken)) {
|
|
|
|
cancelToken.onCancel((reason) => {
|
2023-03-23 20:09:00 +08:00
|
|
|
if (isPlainObject(adapterTask)) {
|
|
|
|
tryToggleProgressUpdate(adapterConfig, adapterTask.offProgressUpdate);
|
|
|
|
|
|
|
|
if (isFunction(adapterTask.abort)) {
|
|
|
|
adapterTask.abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reject(reason);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|