refactor: 使用新的私有属性语法
parent
69044f3583
commit
43c18d1723
|
@ -239,25 +239,25 @@ export default class Axios {
|
||||||
/**
|
/**
|
||||||
* 普通请求别名
|
* 普通请求别名
|
||||||
*/
|
*/
|
||||||
public static as = ['options', 'trace', 'connect'] as const;
|
static as = ['options', 'trace', 'connect'] as const;
|
||||||
/**
|
/**
|
||||||
* 带请求参数的请求别名
|
* 带请求参数的请求别名
|
||||||
*/
|
*/
|
||||||
public static pas = ['head', 'get', 'delete'] as const;
|
static pas = ['head', 'get', 'delete'] as const;
|
||||||
/**
|
/**
|
||||||
* 带请求数据的请求别名
|
* 带请求数据的请求别名
|
||||||
*/
|
*/
|
||||||
public static das = ['post', 'put'] as const;
|
static das = ['post', 'put'] as const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认请求配置
|
* 默认请求配置
|
||||||
*/
|
*/
|
||||||
public defaults: AxiosRequestConfig;
|
defaults: AxiosRequestConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 拦截器
|
* 拦截器
|
||||||
*/
|
*/
|
||||||
public interceptors = {
|
interceptors = {
|
||||||
/**
|
/**
|
||||||
* 请求拦截器
|
* 请求拦截器
|
||||||
*/
|
*/
|
||||||
|
@ -271,49 +271,49 @@ export default class Axios {
|
||||||
/**
|
/**
|
||||||
* 发送 options 请求
|
* 发送 options 请求
|
||||||
*/
|
*/
|
||||||
public options!: AxiosAliasMethod;
|
options!: AxiosAliasMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 get 请求
|
* 发送 get 请求
|
||||||
*/
|
*/
|
||||||
public get!: AxiosWithParamsAliasMethod;
|
get!: AxiosWithParamsAliasMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 head 请求
|
* 发送 head 请求
|
||||||
*/
|
*/
|
||||||
public head!: AxiosWithParamsAliasMethod;
|
head!: AxiosWithParamsAliasMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 post 请求
|
* 发送 post 请求
|
||||||
*/
|
*/
|
||||||
public post!: AxiosWithDataAliasMethod;
|
post!: AxiosWithDataAliasMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 put 请求
|
* 发送 put 请求
|
||||||
*/
|
*/
|
||||||
public put!: AxiosWithDataAliasMethod;
|
put!: AxiosWithDataAliasMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 delete 请求
|
* 发送 delete 请求
|
||||||
*/
|
*/
|
||||||
public delete!: AxiosWithParamsAliasMethod;
|
delete!: AxiosWithParamsAliasMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 trace 请求
|
* 发送 trace 请求
|
||||||
*/
|
*/
|
||||||
public trace!: AxiosAliasMethod;
|
trace!: AxiosAliasMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送 connect 请求
|
* 发送 connect 请求
|
||||||
*/
|
*/
|
||||||
public connect!: AxiosAliasMethod;
|
connect!: AxiosAliasMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例化
|
* 实例化
|
||||||
*
|
*
|
||||||
* @param defaults 默认配置
|
* @param defaults 默认配置
|
||||||
*/
|
*/
|
||||||
public constructor(defaults: AxiosRequestConfig = {}) {
|
constructor(defaults: AxiosRequestConfig = {}) {
|
||||||
this.defaults = defaults;
|
this.defaults = defaults;
|
||||||
|
|
||||||
for (const alias of Axios.as) {
|
for (const alias of Axios.as) {
|
||||||
|
@ -349,7 +349,7 @@ export default class Axios {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getUri(config: AxiosRequestConfig): string {
|
getUri(config: AxiosRequestConfig): string {
|
||||||
const { url, params, paramsSerializer } = mergeConfig(
|
const { url, params, paramsSerializer } = mergeConfig(
|
||||||
this.defaults,
|
this.defaults,
|
||||||
config,
|
config,
|
||||||
|
@ -363,7 +363,7 @@ export default class Axios {
|
||||||
*
|
*
|
||||||
* @param config 请求配置
|
* @param config 请求配置
|
||||||
*/
|
*/
|
||||||
public request<TData = unknown>(
|
request<TData = unknown>(
|
||||||
config: AxiosRequestConfig,
|
config: AxiosRequestConfig,
|
||||||
): Promise<AxiosResponse<TData>> {
|
): Promise<AxiosResponse<TData>> {
|
||||||
const requestConfig = mergeConfig(this.defaults, config);
|
const requestConfig = mergeConfig(this.defaults, config);
|
||||||
|
|
|
@ -16,28 +16,28 @@ export interface InterceptorExecutor {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class InterceptorManager<T = unknown> {
|
export default class InterceptorManager<T = unknown> {
|
||||||
private id = 0;
|
#id = 0;
|
||||||
|
|
||||||
private interceptors: AnyObject<Interceptor<T>> = {};
|
#interceptors: AnyObject<Interceptor<T>> = {};
|
||||||
|
|
||||||
public use(
|
use(
|
||||||
resolved: InterceptorResolved<T>,
|
resolved: InterceptorResolved<T>,
|
||||||
rejected?: InterceptorRejected,
|
rejected?: InterceptorRejected,
|
||||||
): number {
|
): number {
|
||||||
this.interceptors[++this.id] = {
|
this.#interceptors[++this.#id] = {
|
||||||
resolved,
|
resolved,
|
||||||
rejected,
|
rejected,
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.id;
|
return this.#id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public eject(id: number): void {
|
eject(id: number): void {
|
||||||
delete this.interceptors[id];
|
delete this.#interceptors[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
public forEach(executor: InterceptorExecutor, reverse?: boolean): void {
|
forEach(executor: InterceptorExecutor, reverse?: boolean): void {
|
||||||
let interceptors: Interceptor<any>[] = Object.values(this.interceptors);
|
let interceptors: Interceptor<any>[] = Object.values(this.#interceptors);
|
||||||
if (reverse) interceptors = interceptors.reverse();
|
if (reverse) interceptors = interceptors.reverse();
|
||||||
interceptors.forEach(executor);
|
interceptors.forEach(executor);
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,13 +33,13 @@ export interface CancelTokenConstructor {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Cancel {
|
export class Cancel {
|
||||||
public message?: string;
|
message?: string;
|
||||||
|
|
||||||
public constructor(message?: string) {
|
constructor(message?: string) {
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public toString(): string {
|
toString(): string {
|
||||||
const message = this.message ? `: ${this.message}` : '';
|
const message = this.message ? `: ${this.message}` : '';
|
||||||
|
|
||||||
return `Cancel${message}`;
|
return `Cancel${message}`;
|
||||||
|
@ -51,21 +51,21 @@ export function isCancel(value: unknown): value is Cancel {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CancelToken {
|
export class CancelToken {
|
||||||
private reason?: Cancel;
|
#reason?: Cancel;
|
||||||
|
|
||||||
public onCancel: Promise<Cancel>['then'];
|
onCancel: Promise<Cancel>['then'];
|
||||||
|
|
||||||
public constructor(executor: CancelExecutor) {
|
constructor(executor: CancelExecutor) {
|
||||||
let action!: CancelAction;
|
let action!: CancelAction;
|
||||||
const promise = new Promise<Cancel>((resolve) => {
|
const promise = new Promise<Cancel>((resolve) => {
|
||||||
action = (message) => {
|
action = (message) => {
|
||||||
if (this.reason) {
|
if (this.#reason) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.reason = new Cancel(message);
|
this.#reason = new Cancel(message);
|
||||||
|
|
||||||
resolve(this.reason);
|
resolve(this.#reason);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ export class CancelToken {
|
||||||
executor(action);
|
executor(action);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static source(): CancelTokenSource {
|
static source(): CancelTokenSource {
|
||||||
let cancel!: CancelAction;
|
let cancel!: CancelAction;
|
||||||
const token = new CancelToken((action) => {
|
const token = new CancelToken((action) => {
|
||||||
cancel = action;
|
cancel = action;
|
||||||
|
@ -86,9 +86,9 @@ export class CancelToken {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public throwIfRequested(): void {
|
throwIfRequested(): void {
|
||||||
if (this.reason) {
|
if (this.#reason) {
|
||||||
throw this.reason;
|
throw this.#reason;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,11 +5,11 @@ import { AxiosRequestConfig, AxiosResponse, AxiosResponseError } from './Axios';
|
||||||
export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
|
export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
|
||||||
|
|
||||||
class AxiosError extends Error {
|
class AxiosError extends Error {
|
||||||
public config: AxiosRequestConfig;
|
config: AxiosRequestConfig;
|
||||||
public request: AxiosAdapterTask;
|
request: AxiosAdapterTask;
|
||||||
public response: AxiosErrorResponse;
|
response: AxiosErrorResponse;
|
||||||
|
|
||||||
public constructor(
|
constructor(
|
||||||
message: string,
|
message: string,
|
||||||
config: AxiosRequestConfig,
|
config: AxiosRequestConfig,
|
||||||
response: AxiosErrorResponse,
|
response: AxiosErrorResponse,
|
||||||
|
|
Loading…
Reference in New Issue