feat: response 增加部分属性
parent
c447ae06fc
commit
4b01fc2e5c
|
@ -1,6 +1,10 @@
|
|||
import { buildURL } from '../utils';
|
||||
import { mergeConfig } from './mergeConfig';
|
||||
import { AdapterRequestMethod, AxiosAdapter } from './adapter';
|
||||
import {
|
||||
AdapterRequestMethod,
|
||||
AxiosAdapter,
|
||||
AxiosAdapterTask,
|
||||
} from './adapter';
|
||||
import { CancelToken } from './cancel';
|
||||
import dispatchRequest from './dispatchRequest';
|
||||
import InterceptorManager from './InterceptorManager';
|
||||
|
@ -73,6 +77,8 @@ export interface AxiosResponse<TData = any> {
|
|||
statusText: string;
|
||||
headers: AxiosResponseHeaders;
|
||||
data: TData;
|
||||
config?: AxiosRequestConfig;
|
||||
request?: AxiosAdapterTask;
|
||||
cookies?: string[];
|
||||
profile?: AnyObject;
|
||||
}
|
||||
|
@ -81,6 +87,8 @@ export interface AxiosResponseError extends AnyObject {
|
|||
status: number;
|
||||
statusText: string;
|
||||
headers: AxiosResponseHeaders;
|
||||
config?: AxiosRequestConfig;
|
||||
request?: AxiosAdapterTask;
|
||||
}
|
||||
|
||||
export interface AxiosConstructor {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import {
|
||||
assert,
|
||||
isEmptyArray,
|
||||
isFunction,
|
||||
isPlainObject,
|
||||
isPlatform,
|
||||
isUndefined,
|
||||
|
@ -86,10 +87,12 @@ export interface 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(
|
||||
isPlatform(platform),
|
||||
'platform.request 与 platform.download 和 platform.upload 需要是一个 Function 类型',
|
||||
isFunction(platform.download),
|
||||
'platform.download 需要是一个 function',
|
||||
);
|
||||
|
||||
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,25 +41,33 @@ export function request<TData = any>(
|
|||
url: config.url ?? '',
|
||||
type: generateType(config),
|
||||
method: (config.method?.toUpperCase() as AdapterRequestMethod) ?? 'GET',
|
||||
success(response: AxiosResponse): void {
|
||||
if (
|
||||
!isFunction(config.validateStatus) ||
|
||||
config.validateStatus(response.status)
|
||||
) {
|
||||
resolve(response);
|
||||
} else {
|
||||
catchError('请求失败', response);
|
||||
}
|
||||
},
|
||||
fail(error: AxiosResponseError): void {
|
||||
catchError('网络错误', error);
|
||||
},
|
||||
success,
|
||||
fail,
|
||||
};
|
||||
|
||||
const adapterTask = config.adapter!(adapterConfig) as
|
||||
| AxiosAdapterTask
|
||||
| undefined;
|
||||
|
||||
function success(response: AxiosResponse): void {
|
||||
response.config = config;
|
||||
response.request = adapterTask;
|
||||
if (
|
||||
!isFunction(config.validateStatus) ||
|
||||
config.validateStatus(response.status)
|
||||
) {
|
||||
resolve(response);
|
||||
} else {
|
||||
catchError('请求失败', response);
|
||||
}
|
||||
}
|
||||
|
||||
function fail(error: AxiosResponseError): void {
|
||||
error.config = config;
|
||||
error.request = adapterTask;
|
||||
catchError('网络错误', error);
|
||||
}
|
||||
|
||||
function catchError(message: string, response?: AxiosErrorResponse): void {
|
||||
reject(createError(message, config, adapterTask, response));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue