pull/1/head
954270063@qq.com 2020-04-16 00:23:24 +08:00
parent fdab3afb48
commit 329e468495
13 changed files with 833 additions and 136 deletions

63
src/axios.ts Normal file
View File

@ -0,0 +1,63 @@
/*
* @Author: early-autumn
* @Date: 2020-04-15 12:45:18
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-15 22:45:22
*/
import {
AxiosRequest,
AxiosRequestDefault,
AxiosMethodConfig,
ResponseData,
AxiosResponse,
AxiosInstance,
} from './types';
import Axios from './core/Axios';
import defaults from './helper/defaults';
/**
* Axios
*
* axios
*/
function createInstance(config: AxiosRequestDefault): AxiosInstance {
const instance = new Axios(config);
/**
* axios
*
* @
*
* @param url :
* @param config :
*
* @
*
* @param url :
* @param config :
*/
function axios<T extends ResponseData>(
url: AxiosRequest | string,
config: AxiosMethodConfig = {}
): Promise<AxiosResponse<T>> {
let requestConfig: AxiosRequest;
// 调用方式一处理请求配置
if (typeof url !== 'string') {
requestConfig = url;
}
// 调用方式二处理请求配置
else {
requestConfig = { ...config, url };
}
return instance.request(requestConfig);
}
// Axios 实例的所有属性和方法合并至 axios 函数
Object.assign(axios, instance);
return axios as AxiosInstance;
}
export default createInstance(defaults);

View File

@ -2,7 +2,7 @@
* @Author: early-autumn * @Author: early-autumn
* @Date: 2020-04-13 20:00:08 * @Date: 2020-04-13 20:00:08
* @LastEditors: early-autumn * @LastEditors: early-autumn
* @LastEditTime: 2020-04-14 19:17:53 * @LastEditTime: 2020-04-15 17:29:07
*/ */
import { CancelToken, CancelAction, CancelExecutor, CancelTokenSource } from '../types'; import { CancelToken, CancelAction, CancelExecutor, CancelTokenSource } from '../types';
import Cancel from './Cancel'; import Cancel from './Cancel';
@ -31,7 +31,7 @@ export default class CancelTokenStatic implements CancelToken {
executor(action); executor(action);
} }
throwIfRequested() { throwIfRequested(): void {
if (this.reason) { if (this.reason) {
throw this.reason; throw this.reason;
} }

View File

@ -2,53 +2,231 @@
* @Author: early-autumn * @Author: early-autumn
* @Date: 2020-04-13 18:00:27 * @Date: 2020-04-13 18:00:27
* @LastEditors: early-autumn * @LastEditors: early-autumn
* @LastEditTime: 2020-04-14 23:40:52 * @LastEditTime: 2020-04-16 00:11:53
*/ */
import { Params, Data, AxiosRequestConfig, Axios, AxiosMethodConfig, AxiosPromise, Method } from '../types'; import {
Method,
Params,
Data,
Interceptors,
AxiosRequest,
AxiosRequestDefault,
AxiosMethodConfig,
ResponseData,
AxiosResponse,
Axios,
} from '../types';
import InterceptorManager from './InterceptorManager';
import mergeConfig from './mergeConfig';
import dispatchRequest from './dispatchRequest'; import dispatchRequest from './dispatchRequest';
interface PromiseCatch {
request: Promise<AxiosRequest>;
response?: Promise<AxiosResponse>;
}
export default class AxiosStatic implements Axios { export default class AxiosStatic implements Axios {
request(config: AxiosRequestConfig): AxiosPromise { /**
return dispatchRequest(config); *
*/
defaults: AxiosRequestDefault;
/**
* Axios
*/
public interceptors: Interceptors;
constructor(config: AxiosRequestDefault) {
this.defaults = config;
this.interceptors = {
request: new InterceptorManager<AxiosRequest>(),
response: new InterceptorManager<AxiosResponse>(),
};
} }
options(url: string, params?: Params, config?: AxiosMethodConfig): AxiosPromise { /**
return this._requestMethodWithoutParams('options', url, params, config); * HTTP
*
* @param config
*/
public request<T extends ResponseData>(config: AxiosRequest): Promise<AxiosResponse<T>> {
config = mergeConfig(this.defaults, config);
const promise: PromiseCatch = {
request: Promise.resolve(config),
};
// 执行前置拦截器
this.interceptors.request.forEach(({ resolved, rejected }) => {
promise.request = promise.request.then(resolved, rejected);
}, 'reverse');
// 发送请求
promise.response = promise.request.then(dispatchRequest, (err: any) => {
throw err;
});
// 执行后置拦截器
this.interceptors.response.forEach(({ resolved, rejected }) => {
promise.response = promise.response?.then(resolved, rejected);
});
return promise.response as Promise<AxiosResponse<T>>;
} }
get(url: string, params?: Params, config?: AxiosMethodConfig): AxiosPromise { /**
return this._requestMethodWithoutParams('get', url, params, config); * HTTP OPTIONS
*
* @param url
* @param params
* @param config
*/
public options<T extends ResponseData>(
url: string,
params?: Params,
config?: AxiosMethodConfig
): Promise<AxiosResponse<T>> {
return this._requestMethodWithoutParams<T>('options', url, params, config);
} }
head(url: string, params?: Params, config?: AxiosMethodConfig): AxiosPromise { /**
return this._requestMethodWithoutParams('head', url, params, config); * HTTP GET
*
* @param url
* @param params
* @param config
*/
public get<T extends ResponseData>(
url: string,
params?: Params,
config?: AxiosMethodConfig
): Promise<AxiosResponse<T>> {
return this._requestMethodWithoutParams<T>('get', url, params, config);
} }
post(url: string, data?: Data, config?: AxiosMethodConfig): AxiosPromise { /**
return this._requestMethodWithoutData('post', url, data, config); * HTTP HEAD
*
* @param url
* @param params
* @param config
*/
public head<T extends ResponseData>(
url: string,
params?: Params,
config?: AxiosMethodConfig
): Promise<AxiosResponse<T>> {
return this._requestMethodWithoutParams<T>('head', url, params, config);
} }
put(url: string, data?: Data, config?: AxiosMethodConfig): AxiosPromise { /**
return this._requestMethodWithoutData('put', url, data, config); * HTTP POST
*
* @param url
* @param data
* @param config
*/
public post<T extends ResponseData>(url: string, data?: Data, config?: AxiosMethodConfig): Promise<AxiosResponse<T>> {
return this._requestMethodWithoutData<T>('post', url, data, config);
} }
delete(url: string, params?: Params, config?: AxiosMethodConfig): AxiosPromise { /**
return this._requestMethodWithoutParams('delete', url, params, config); * HTTP PUT
*
* @param url
* @param data
* @param config
*/
public put<T extends ResponseData>(url: string, data?: Data, config?: AxiosMethodConfig): Promise<AxiosResponse<T>> {
return this._requestMethodWithoutData<T>('put', url, data, config);
} }
trace(url: string, params?: Params, config?: AxiosMethodConfig): AxiosPromise { /**
return this._requestMethodWithoutParams('trace', url, params, config); * HTTP DELETE
*
* @param url
* @param params
* @param config
*/
public delete<T extends ResponseData>(
url: string,
params?: Params,
config?: AxiosMethodConfig
): Promise<AxiosResponse<T>> {
return this._requestMethodWithoutParams<T>('delete', url, params, config);
} }
connect(url: string, params?: Params, config?: AxiosMethodConfig): AxiosPromise { /**
return this._requestMethodWithoutParams('connect', url, params, config); * HTTP TRACE
*
* @param url
* @param params
* @param config
*/
public trace<T extends ResponseData>(
url: string,
params?: Params,
config?: AxiosMethodConfig
): Promise<AxiosResponse<T>> {
return this._requestMethodWithoutParams<T>('trace', url, params, config);
} }
_requestMethodWithoutParams(method: Method, url: string, params?: Params, config: AxiosMethodConfig = {}) { /**
return this.request({ ...config, method, url, params }); * HTTP CONNECT
*
* @param url
* @param params
* @param config
*/
public connect<T extends ResponseData>(
url: string,
params?: Params,
config?: AxiosMethodConfig
): Promise<AxiosResponse<T>> {
return this._requestMethodWithoutParams<T>('connect', url, params, config);
} }
_requestMethodWithoutData(method: Method, url: string, data?: Data, config: AxiosMethodConfig = {}) { /**
return this.request({ ...config, method, url, data }); * HTTP
*
* @param method
* @param url
* @param params
* @param config
*/
private _requestMethodWithoutParams<T extends ResponseData>(
method: Method,
url: string,
params?: Params,
config: AxiosMethodConfig = {}
): Promise<AxiosResponse<T>> {
return this.request<T>({
...config,
method,
url,
params,
});
}
/**
* HTTP
*
* @param method
* @param url
* @param data
* @param config
*/
private _requestMethodWithoutData<T extends ResponseData>(
method: Method,
url: string,
data?: Data,
config: AxiosMethodConfig = {}
): Promise<AxiosResponse<T>> {
return this.request<T>({
...config,
method,
url,
data,
});
} }
} }

View File

@ -0,0 +1,78 @@
/*
* @Author: early-autumn
* @Date: 2020-04-15 17:50:50
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-15 23:41:22
*/
import {
InterceptorResolved,
InterceptorRejected,
Interceptor,
InterceptorExecutor,
InterceptorManager,
} from '../types';
/**
*
*/
export default class InterceptorManagerStatic<T> implements InterceptorManager<T> {
/**
* id
*/
private id: number;
/**
*
*/
private interceptors: Map<number, Interceptor<T>>;
constructor() {
this.id = 0;
this.interceptors = new Map();
}
/**
*
*
* @param resolved
* @param rejected
*/
public use(
resolved: InterceptorResolved<T>,
rejected: InterceptorRejected = (err) => {
throw err;
}
) {
this.interceptors.set(this.id, {
resolved,
rejected,
});
return ++this.id;
}
/**
*
*
* @param id id
*/
public eject(id: number): void {
this.interceptors.delete(id);
}
/**
*
*
* @param executor
* @param reverse
*/
public forEach(executor: InterceptorExecutor<T>, reverse?: 'reverse'): void {
let interceptors: Interceptor<T>[] = [...this.interceptors.values()];
if (reverse === 'reverse') {
interceptors = interceptors.reverse();
}
interceptors.forEach(executor);
}
}

View File

@ -2,26 +2,47 @@
* @Author: early-autumn * @Author: early-autumn
* @Date: 2020-04-14 22:23:39 * @Date: 2020-04-14 22:23:39
* @LastEditors: early-autumn * @LastEditors: early-autumn
* @LastEditTime: 2020-04-14 22:55:33 * @LastEditTime: 2020-04-15 15:50:18
*/ */
import { AxiosRequestConfig, AxiosResponse } from '../types'; import { AxiosRequest, AxiosResponse } from '../types';
/**
* AxiosError Error
*/
class AxiosError extends Error { class AxiosError extends Error {
/**
* Axios
*/
isAxiosError = true; isAxiosError = true;
config: AxiosRequestConfig; /**
*
*/
config: AxiosRequest;
/**
*
*/
response?: AxiosResponse; response?: AxiosResponse;
constructor(message: string, config: AxiosRequestConfig, response?: AxiosResponse) { constructor(message: string, config: AxiosRequest, response?: AxiosResponse) {
super(message); super(message);
this.config = config; this.config = config;
this.response = response; this.response = response;
// 修复 // 修复继承系统自带类 prototype 设置失败的问题
Object.setPrototypeOf(this, AxiosError.prototype); Object.setPrototypeOf(this, AxiosError.prototype);
} }
} }
export default function createError(message: string, config: AxiosRequestConfig, response?: AxiosResponse): AxiosError { /**
* AxiosError
*
* AxiosError
*
* @param message
* @param config
* @param response
*/
export default function createError(message: string, config: AxiosRequest, response?: AxiosResponse): AxiosError {
return new AxiosError(message, config, response); return new AxiosError(message, config, response);
} }

View File

@ -2,13 +2,34 @@
* @Author: early-autumn * @Author: early-autumn
* @Date: 2020-04-13 15:22:22 * @Date: 2020-04-13 15:22:22
* @LastEditors: early-autumn * @LastEditors: early-autumn
* @LastEditTime: 2020-04-14 23:23:46 * @LastEditTime: 2020-04-15 20:21:13
*/ */
import { AxiosRequestConfig, AxiosPromise } from '../types'; import { AxiosRequest, AxiosResponse } from '../types';
import processURL from '../helper/processURL';
import processData from '../helper/processData';
import request from './request'; import request from './request';
import transformRequestConfig from './transformRequestConfig';
export default function dispatchRequest(config: AxiosRequestConfig): AxiosPromise { /**
*
*
* @param config
*/
function transformRequestConfig(config: AxiosRequest): void {
const { url, params, data } = config;
config.url = processURL(url, params);
if (data !== undefined) {
config.data = processData(data);
}
}
/**
*
*
* @param config
*/
export default function dispatchRequest(config: AxiosRequest): Promise<AxiosResponse> {
transformRequestConfig(config); transformRequestConfig(config);
return request(config); return request(config);

15
src/core/mergeConfig.ts Normal file
View File

@ -0,0 +1,15 @@
/*
* @Author: early-autumn
* @Date: 2020-04-15 22:48:25
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-16 00:18:44
*/
import { AxiosRequest, AxiosRequestDefault } from '../types';
/**
*
*/
export default function mergeConfig(defaults: AxiosRequestDefault, config: AxiosRequest): AxiosRequest {
console.log(defaults);
return config;
}

View File

@ -2,9 +2,9 @@
* @Author: early-autumn * @Author: early-autumn
* @Date: 2020-04-13 18:01:16 * @Date: 2020-04-13 18:01:16
* @LastEditors: early-autumn * @LastEditors: early-autumn
* @LastEditTime: 2020-04-14 22:51:17 * @LastEditTime: 2020-04-15 23:19:04
*/ */
import { AxiosRequestConfig, AxiosPromise } from '../types'; import { MethodType, AxiosRequest, AxiosResponse } from '../types';
import createError from './createError'; import createError from './createError';
/** /**
@ -12,33 +12,48 @@ import createError from './createError';
* *
* @param config * @param config
*/ */
export default function request(config: AxiosRequestConfig): AxiosPromise { export default function request(config: AxiosRequest): Promise<AxiosResponse> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const { cancelToken, method, ...options } = config; const { cancelToken, method, ...options } = config;
// method 转为全大写 // method 转为全大写
const methodType = (method?.toUpperCase() ?? 'GET') as WechatMiniprogram.RequestOption['method']; const methodType = method?.toUpperCase() as MethodType;
function catchError({ errMsg }: WechatMiniprogram.GeneralCallbackResult): void { /**
reject(createError(errMsg, config)); *
*
* @param param0
* @param response
*/
function catchError({ errMsg }: { errMsg: string }, response?: AxiosResponse): void {
reject(createError(errMsg, config, response));
} }
function handleResponse(result: WechatMiniprogram.RequestSuccessCallbackResult): void { /**
*
*
* @param result
*/
function checkStatusCode(result: WechatMiniprogram.RequestSuccessCallbackResult): void {
const response = { ...result, config }; const response = { ...result, config };
const { statusCode, errMsg } = response; const { statusCode, errMsg } = response;
// 成功
if (statusCode >= 200 && statusCode < 300) { if (statusCode >= 200 && statusCode < 300) {
resolve(response); resolve(response);
} else { }
reject(createError(!!errMsg ? errMsg : `Request failed with status code ${statusCode}`, config, response)); // 失败
else {
// `Request failed with status code ${statusCode}`
catchError({ errMsg }, response);
} }
} }
// 替换 config 中的 success fail complete // 发送请求
// 替换 options 中的 success fail complete
const request = wx.request({ const request = wx.request({
...options, ...options,
method: methodType, method: methodType,
success: handleResponse, success: checkStatusCode,
fail: catchError, fail: catchError,
complete: undefined, complete: undefined,
}); });

View File

@ -1,21 +0,0 @@
/*
* @Author: early-autumn
* @Date: 2020-04-14 10:15:50
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-14 23:09:27
*/
import { AxiosRequestConfig } from '../types';
import processURL from '../helper/processURL';
import processData from '../helper/processData';
/**
* config
*
* @param config AxiosRequestConfig
*/
export default function transformRequestConfig(config: AxiosRequestConfig): void {
const { url, params, data } = config;
config.url = processURL(url, params);
config.data = processData(data);
}

30
src/helper/defaults.ts Normal file
View File

@ -0,0 +1,30 @@
/*
* @Author: early-autumn
* @Date: 2020-04-15 22:09:38
* @LastEditors: early-autumn
* @LastEditTime: 2020-04-15 23:20:30
*/
import { AxiosRequestDefault } from '../types';
const defaults: AxiosRequestDefault = {
method: 'get',
header: {
common: {
Accept: 'application/json, test/plain, */*',
},
options: {},
get: {},
head: {},
post: {
'Context-Type': 'application/x-www-form-urlencoded',
},
put: {
'Context-Type': 'application/x-www-form-urlencoded',
},
delete: {},
trace: {},
connect: {},
},
};
export default defaults;

View File

@ -2,7 +2,7 @@
* @Author: early-autumn * @Author: early-autumn
* @Date: 2020-04-13 21:45:45 * @Date: 2020-04-13 21:45:45
* @LastEditors: early-autumn * @LastEditors: early-autumn
* @LastEditTime: 2020-04-14 23:07:28 * @LastEditTime: 2020-04-15 11:54:36
*/ */
import { Params } from '../types'; import { Params } from '../types';
import { isPlainObject, isDate } from './utils'; import { isPlainObject, isDate } from './utils';
@ -26,8 +26,8 @@ function encode(str: string): string {
/** /**
* URL * URL
* *
* @param url URL * @param url
* @param params * @param paramsStr
*/ */
function joinURL(url: string, paramsStr: string): string { function joinURL(url: string, paramsStr: string): string {
// 移除 hash // 移除 hash
@ -46,8 +46,8 @@ function joinURL(url: string, paramsStr: string): string {
/** /**
* URL * URL
* *
* @param url URL * @param url
* @param params * @param params
*/ */
export default function processURL(url: string, params?: Params): string { export default function processURL(url: string, params?: Params): string {
if (params === undefined) { if (params === undefined) {

View File

@ -2,5 +2,34 @@
* @Author: early-autumn * @Author: early-autumn
* @Date: 2020-04-14 23:22:52 * @Date: 2020-04-14 23:22:52
* @LastEditors: early-autumn * @LastEditors: early-autumn
* @LastEditTime: 2020-04-14 23:22:52 * @LastEditTime: 2020-04-15 16:56:41
*/ */
import axios from './axios';
interface Test {
test1: string;
test2: string;
test3: string;
}
axios<Test>('/test').then((res) => {
console.log(res.data.test3);
});
axios<Test>({ url: '' }).then((res) => {
console.log(res.data.test1);
});
axios
.request<Test>({ url: '' })
.then((res) => {
console.log(res.data.test1);
});
axios.get<Test>('', {}, {}).then((res) => {
console.log(res.data.test1);
});
// axios.post<string>('', {}, {}).then((res) => {
// console.log(res.data);
// });

View File

@ -2,71 +2,86 @@
* @Author: early-autumn * @Author: early-autumn
* @Date: 2020-04-13 15:23:53 * @Date: 2020-04-13 15:23:53
* @LastEditors: early-autumn * @LastEditors: early-autumn
* @LastEditTime: 2020-04-14 23:42:10 * @LastEditTime: 2020-04-15 23:48:48
*/ */
import 'miniprogram-api-typings'; import 'miniprogram-api-typings';
/**
*
*/
export declare type AnyObject = Record<string, any>; export declare type AnyObject = Record<string, any>;
/**
*
*/
export declare type MethodType = WechatMiniprogram.RequestOption['method'];
/**
* Axios
*/
export declare type Method = 'options' | 'get' | 'head' | 'post' | 'put' | 'delete' | 'trace' | 'connect' | MethodType;
/**
* Axios
*/
export declare type Params = AnyObject; export declare type Params = AnyObject;
export declare type Data = AxiosRequestConfig['data']; /**
* Axios
*/
export declare type Data = WechatMiniprogram.RequestOption['data'];
/** /**
* *
*/ */
export declare type Method = export type AxiosRequest = Omit<WechatMiniprogram.RequestOption, 'method' | 'success' | 'fail' | 'complete'> & {
| 'options'
| 'get'
| 'head'
| 'post'
| 'put'
| 'delete'
| 'trace'
| 'connect'
| WechatMiniprogram.RequestOption['method'];
/**
*
*/
export type AxiosRequestConfig = Omit<WechatMiniprogram.RequestOption, 'method' | 'success' | 'fail' | 'complete'> & {
/** HTTP /** HTTP
* *
* *
* - 'options': HTTP OPTIONS; * - 'options': HTTP OPTIONS;
* - 'OPTIONS': HTTP OPTIONS;
* - 'get': HTTP GET; * - 'get': HTTP GET;
* - 'GET': HTTP GET;
* - 'head': HTTP HEAD; * - 'head': HTTP HEAD;
* - 'HEAD': HTTP HEAD;
* - 'post': HTTP POST; * - 'post': HTTP POST;
* - 'POST': HTTP POST;
* - 'put': HTTP PUT; * - 'put': HTTP PUT;
* - 'PUT': HTTP PUT;
* - 'delete': HTTP DELETE; * - 'delete': HTTP DELETE;
* - 'DELETE': HTTP DELETE;
* - 'trace': HTTP TRACE; * - 'trace': HTTP TRACE;
* - 'TRACE': HTTP TRACE;
* - 'connect': HTTP CONNECT; * - 'connect': HTTP CONNECT;
* - 'OPTIONS': HTTP OPTIONS;
* - 'GET': HTTP GET;
* - 'HEAD': HTTP HEAD;
* - 'POST': HTTP POST;
* - 'PUT': HTTP PUT;
* - 'DELETE': HTTP DELETE;
* - 'TRACE': HTTP TRACE;
* - 'CONNECT': HTTP CONNECT; * - 'CONNECT': HTTP CONNECT;
*/ */
method?: Method; method?: Method;
/** /**
* URL *
*/ */
params?: Params; params?: Params;
/**
*
*/
data?: Data;
/** /**
* http2 * http2
*/ */
enableHttp2?: boolean; enableHttp2?: boolean;
/** /**
* quic * quic
*/ */
enableQuic?: boolean; enableQuic?: boolean;
/** /**
* cache * cache
*/ */
enableCache?: boolean; enableCache?: boolean;
/** /**
* *
*/ */
@ -74,61 +89,305 @@ export type AxiosRequestConfig = Omit<WechatMiniprogram.RequestOption, 'method'
}; };
/** /**
* *
*/ */
export interface AxiosResponse extends WechatMiniprogram.RequestSuccessCallbackResult { export declare type AxiosRequestDefault = Omit<AxiosRequest, 'url' | 'header' | 'cancelToken'> & {
config: AxiosRequestConfig; /**
} *
*/
export declare type AxiosPromise = Promise<AxiosResponse>; baseURL?: string;
/**
export type AxiosMethodConfig = Omit<AxiosRequestConfig, 'url'>; *
*/
header?: {
/**
*
*/
common?: AnyObject;
/**
* options
*/
options?: AnyObject;
/**
* get
*/
get?: AnyObject;
/**
* head
*/
head?: AnyObject;
/**
* post
*/
post?: AnyObject;
/**
* put
*/
put?: AnyObject;
/**
* delete
*/
delete?: AnyObject;
/**
* trace
*/
trace?: AnyObject;
/**
* connect
*/
connect?: AnyObject;
};
};
/** /**
* Axios *
*/
export declare type ResponseData = WechatMiniprogram.RequestSuccessCallbackResult['data'];
/**
*
*/
export interface AxiosResponse<T extends ResponseData = ResponseData>
extends WechatMiniprogram.RequestSuccessCallbackResult {
/**
*
*/
data: T;
/**
*
*/
config: AxiosRequest;
}
/**
*
*/
export interface InterceptorResolved<T = any> {
(value: T): Promise<T>;
}
/**
*
*/
export interface InterceptorRejected {
(err: any): any;
}
/**
*
*/
export declare type Interceptor<T = any> = {
/**
*
*/
resolved: InterceptorResolved<T>;
/**
*
*/
rejected: InterceptorRejected;
};
/**
*
*/
export interface InterceptorExecutor<T = any> {
(interceptor: Interceptor<T>): void;
}
/**
*
*/
export interface InterceptorManager<T = any> {
/**
*
*
* @param resolved
* @param rejected
*/
use(resolved: InterceptorResolved<T>, rejected?: InterceptorRejected): number;
/**
*
*
* @param id id
*/
eject(id: number): void;
/**
*
*
* @param executor
* @param reverse
*/
forEach(executor: InterceptorExecutor<T>, reverse?: 'reverse'): void;
}
/**
* Axios
*/
export interface Interceptors {
/**
* request
*/
request: InterceptorManager<AxiosRequest>;
/**
* response
*/
response: InterceptorManager<AxiosResponse>;
}
/**
* Axios
*/
export type AxiosMethodConfig = Omit<AxiosRequest, 'url'>;
/**
* Axios
*/ */
export interface Axios { export interface Axios {
/** /**
* *
* @param config 000
*/ */
request(config: AxiosRequestConfig): AxiosPromise; defaults: AxiosRequestDefault;
options(url: string, params?: Params, config?: AxiosMethodConfig): AxiosPromise; /**
* Axios
*/
interceptors: Interceptors;
get(url: string, params?: Params, config?: AxiosMethodConfig): AxiosPromise; /**
* HTTP
*
* @param config
*/
request<T extends ResponseData>(config: AxiosRequest): Promise<AxiosResponse<T>>;
head(url: string, params?: Params, config?: AxiosMethodConfig): AxiosPromise; /**
* HTTP OPTIONS
*
* @param url
* @param params
* @param config
*/
options<T extends ResponseData>(url: string, params?: Params, config?: AxiosMethodConfig): Promise<AxiosResponse<T>>;
post(url: string, data?: Data, config?: AxiosMethodConfig): AxiosPromise; /**
* HTTP GET
*
* @param url
* @param params
* @param config
*/
get<T extends ResponseData>(url: string, params?: Params, config?: AxiosMethodConfig): Promise<AxiosResponse<T>>;
put(url: string, data?: Data, config?: AxiosMethodConfig): AxiosPromise; /**
* HTTP HEAD
*
* @param url
* @param params
* @param config
*/
head<T extends ResponseData>(url: string, params?: Params, config?: AxiosMethodConfig): Promise<AxiosResponse<T>>;
delete(url: string, params?: Params, config?: AxiosMethodConfig): AxiosPromise; /**
* HTTP POST
*
* @param url
* @param data
* @param config
*/
post<T extends ResponseData>(url: string, data?: Data, config?: AxiosMethodConfig): Promise<AxiosResponse<T>>;
trace(url: string, params?: Data, config?: AxiosMethodConfig): AxiosPromise; /**
* HTTP PUT
*
* @param url
* @param data
* @param config
*/
put<T extends ResponseData>(url: string, data?: Data, config?: AxiosMethodConfig): Promise<AxiosResponse<T>>;
connect(url: string, params?: Data, config?: AxiosMethodConfig): AxiosPromise; /**
* HTTP DELETE
*
* @param url
* @param params
* @param config
*/
delete<T extends ResponseData>(url: string, params?: Params, config?: AxiosMethodConfig): Promise<AxiosResponse<T>>;
/**
* HTTP TRACE
*
* @param url
* @param params
* @param config
*/
trace<T extends ResponseData>(url: string, params?: Data, config?: AxiosMethodConfig): Promise<AxiosResponse<T>>;
/**
* HTTP CONNECT
*
* @param url
* @param params
* @param config
*/
connect<T extends ResponseData>(url: string, params?: Data, config?: AxiosMethodConfig): Promise<AxiosResponse<T>>;
} }
/**
* axios
*
* , Axios
*/
export interface AxiosInstance extends Axios { export interface AxiosInstance extends Axios {
(config: AxiosRequestConfig): AxiosPromise; /**
*
*
* @param config
*/
<T extends ResponseData>(config: AxiosRequest): Promise<AxiosResponse<T>>;
/**
*
*
* @param url
* @param config
*/
<T extends ResponseData>(url: string, config?: AxiosMethodConfig): Promise<AxiosResponse<T>>;
} }
// export interface AxiosError extends Error { /**
// isAxiosError: boolean; * AxiosError Error
// config: AxiosRequestConfig; */
// response?: AxiosResponse; export interface AxiosError extends Error {
// } /**
* Axios
*/
isAxiosError: boolean;
/**
*
*/
config: AxiosRequest;
/**
*
*/
response?: AxiosResponse;
}
/** /**
* *
*/ */
export interface Cancel { export interface Cancel {
/** /**
* *
*/ */
message?: string; message?: string;
/** /**
* *
*/ */
@ -157,12 +416,14 @@ export interface CancelToken {
* *
*/ */
reason?: Cancel; reason?: Cancel;
/** /**
* *
*/ */
listener: Promise<Cancel>; listener: Promise<Cancel>;
/** /**
* * ,
*/ */
throwIfRequested(): void; throwIfRequested(): void;
} }
@ -171,6 +432,13 @@ export interface CancelToken {
* source * source
*/ */
export interface CancelTokenSource { export interface CancelTokenSource {
/**
*
*/
token: CancelToken; token: CancelToken;
/**
*
*/
cancel: CancelAction; cancel: CancelAction;
} }