commit
e171b228d2
|
@ -1,6 +1,10 @@
|
||||||
import { buildURL } from '../utils';
|
import { buildURL } from '../utils';
|
||||||
import { mergeConfig } from './mergeConfig';
|
import { mergeConfig } from './mergeConfig';
|
||||||
import { AdapterRequestMethod, AxiosAdapter } from './adapter';
|
import {
|
||||||
|
AdapterRequestMethod,
|
||||||
|
AxiosAdapter,
|
||||||
|
AxiosAdapterTask,
|
||||||
|
} from './adapter';
|
||||||
import { CancelToken } from './cancel';
|
import { CancelToken } from './cancel';
|
||||||
import dispatchRequest from './dispatchRequest';
|
import dispatchRequest from './dispatchRequest';
|
||||||
import InterceptorManager from './InterceptorManager';
|
import InterceptorManager from './InterceptorManager';
|
||||||
|
@ -73,6 +77,8 @@ export interface AxiosResponse<TData = any> {
|
||||||
statusText: string;
|
statusText: string;
|
||||||
headers: AxiosResponseHeaders;
|
headers: AxiosResponseHeaders;
|
||||||
data: TData;
|
data: TData;
|
||||||
|
config?: AxiosRequestConfig;
|
||||||
|
request?: AxiosAdapterTask;
|
||||||
cookies?: string[];
|
cookies?: string[];
|
||||||
profile?: AnyObject;
|
profile?: AnyObject;
|
||||||
}
|
}
|
||||||
|
@ -81,6 +87,8 @@ export interface AxiosResponseError extends AnyObject {
|
||||||
status: number;
|
status: number;
|
||||||
statusText: string;
|
statusText: string;
|
||||||
headers: AxiosResponseHeaders;
|
headers: AxiosResponseHeaders;
|
||||||
|
config?: AxiosRequestConfig;
|
||||||
|
request?: AxiosAdapterTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosConstructor {
|
export interface AxiosConstructor {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import {
|
import {
|
||||||
assert,
|
assert,
|
||||||
isEmptyArray,
|
isEmptyArray,
|
||||||
|
isFunction,
|
||||||
isPlainObject,
|
isPlainObject,
|
||||||
isPlatform,
|
isPlatform,
|
||||||
isUndefined,
|
isUndefined,
|
||||||
|
@ -86,10 +87,12 @@ export interface AxiosAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createAdapter(platform: AxiosPlatform): AxiosAdapter {
|
export function createAdapter(platform: AxiosPlatform): AxiosAdapter {
|
||||||
assert(isPlainObject(platform), 'platform 需要是一个 Object 类型');
|
assert(isPlainObject(platform), 'platform 需要是一个 object');
|
||||||
|
assert(isFunction(platform.request), 'platform.request 需要是一个 function');
|
||||||
|
assert(isFunction(platform.upload), 'platform.upload 需要是一个 function');
|
||||||
assert(
|
assert(
|
||||||
isPlatform(platform),
|
isFunction(platform.download),
|
||||||
'platform.request 与 platform.download 和 platform.upload 需要是一个 Function 类型',
|
'platform.download 需要是一个 function',
|
||||||
);
|
);
|
||||||
|
|
||||||
function transformResult(result: any): void {
|
function transformResult(result: any): void {
|
||||||
|
|
|
@ -50,7 +50,7 @@ export default function dispatchRequest<TData = any>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return config.errorHandler?.(reason) ?? Promise.reject(reason);
|
return Promise.reject(config.errorHandler?.(reason) ?? reason);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,17 @@ export function request<TData = any>(
|
||||||
url: config.url ?? '',
|
url: config.url ?? '',
|
||||||
type: generateType(config),
|
type: generateType(config),
|
||||||
method: (config.method?.toUpperCase() as AdapterRequestMethod) ?? 'GET',
|
method: (config.method?.toUpperCase() as AdapterRequestMethod) ?? 'GET',
|
||||||
success(response: AxiosResponse): void {
|
success,
|
||||||
|
fail,
|
||||||
|
};
|
||||||
|
|
||||||
|
const adapterTask = config.adapter!(adapterConfig) as
|
||||||
|
| AxiosAdapterTask
|
||||||
|
| undefined;
|
||||||
|
|
||||||
|
function success(response: AxiosResponse): void {
|
||||||
|
response.config = config;
|
||||||
|
response.request = adapterTask;
|
||||||
if (
|
if (
|
||||||
!isFunction(config.validateStatus) ||
|
!isFunction(config.validateStatus) ||
|
||||||
config.validateStatus(response.status)
|
config.validateStatus(response.status)
|
||||||
|
@ -50,15 +60,13 @@ export function request<TData = any>(
|
||||||
} else {
|
} else {
|
||||||
catchError('请求失败', response);
|
catchError('请求失败', response);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
fail(error: AxiosResponseError): void {
|
|
||||||
catchError('网络错误', error);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const adapterTask = config.adapter!(adapterConfig) as
|
function fail(error: AxiosResponseError): void {
|
||||||
| AxiosAdapterTask
|
error.config = config;
|
||||||
| undefined;
|
error.request = adapterTask;
|
||||||
|
catchError('网络错误', error);
|
||||||
|
}
|
||||||
|
|
||||||
function catchError(message: string, response?: AxiosErrorResponse): void {
|
function catchError(message: string, response?: AxiosErrorResponse): void {
|
||||||
reject(createError(message, config, adapterTask, response));
|
reject(createError(message, config, adapterTask, response));
|
||||||
|
|
Loading…
Reference in New Issue