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 请求
|
||||
*/
|
||||
public options!: AxiosAliasMethod;
|
||||
options!: AxiosAliasMethod;
|
||||
|
||||
/**
|
||||
* 发送 get 请求
|
||||
*/
|
||||
public get!: AxiosWithParamsAliasMethod;
|
||||
get!: AxiosWithParamsAliasMethod;
|
||||
|
||||
/**
|
||||
* 发送 head 请求
|
||||
*/
|
||||
public head!: AxiosWithParamsAliasMethod;
|
||||
head!: AxiosWithParamsAliasMethod;
|
||||
|
||||
/**
|
||||
* 发送 post 请求
|
||||
*/
|
||||
public post!: AxiosWithDataAliasMethod;
|
||||
post!: AxiosWithDataAliasMethod;
|
||||
|
||||
/**
|
||||
* 发送 put 请求
|
||||
*/
|
||||
public put!: AxiosWithDataAliasMethod;
|
||||
put!: AxiosWithDataAliasMethod;
|
||||
|
||||
/**
|
||||
* 发送 delete 请求
|
||||
*/
|
||||
public delete!: AxiosWithParamsAliasMethod;
|
||||
delete!: AxiosWithParamsAliasMethod;
|
||||
|
||||
/**
|
||||
* 发送 trace 请求
|
||||
*/
|
||||
public trace!: AxiosAliasMethod;
|
||||
trace!: AxiosAliasMethod;
|
||||
|
||||
/**
|
||||
* 发送 connect 请求
|
||||
*/
|
||||
public connect!: AxiosAliasMethod;
|
||||
connect!: AxiosAliasMethod;
|
||||
|
||||
/**
|
||||
* 实例化
|
||||
*
|
||||
* @param defaults 默认配置
|
||||
*/
|
||||
public constructor(defaults: AxiosRequestConfig = {}) {
|
||||
constructor(defaults: AxiosRequestConfig = {}) {
|
||||
this.defaults = defaults;
|
||||
|
||||
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(
|
||||
this.defaults,
|
||||
config,
|
||||
|
@ -363,7 +363,7 @@ export default class Axios {
|
|||
*
|
||||
* @param config 请求配置
|
||||
*/
|
||||
public request<TData = unknown>(
|
||||
request<TData = unknown>(
|
||||
config: AxiosRequestConfig,
|
||||
): Promise<AxiosResponse<TData>> {
|
||||
const requestConfig = mergeConfig(this.defaults, config);
|
||||
|
|
|
@ -16,28 +16,28 @@ export interface InterceptorExecutor {
|
|||
}
|
||||
|
||||
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>,
|
||||
rejected?: InterceptorRejected,
|
||||
): number {
|
||||
this.interceptors[++this.id] = {
|
||||
this.#interceptors[++this.#id] = {
|
||||
resolved,
|
||||
rejected,
|
||||
};
|
||||
|
||||
return this.id;
|
||||
return this.#id;
|
||||
}
|
||||
|
||||
public eject(id: number): void {
|
||||
delete this.interceptors[id];
|
||||
eject(id: number): void {
|
||||
delete this.#interceptors[id];
|
||||
}
|
||||
|
||||
public forEach(executor: InterceptorExecutor, reverse?: boolean): void {
|
||||
let interceptors: Interceptor<any>[] = Object.values(this.interceptors);
|
||||
forEach(executor: InterceptorExecutor, reverse?: boolean): void {
|
||||
let interceptors: Interceptor<any>[] = Object.values(this.#interceptors);
|
||||
if (reverse) interceptors = interceptors.reverse();
|
||||
interceptors.forEach(executor);
|
||||
}
|
||||
|
|
|
@ -33,13 +33,13 @@ export interface CancelTokenConstructor {
|
|||
}
|
||||
|
||||
export class Cancel {
|
||||
public message?: string;
|
||||
message?: string;
|
||||
|
||||
public constructor(message?: string) {
|
||||
constructor(message?: string) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
toString(): string {
|
||||
const message = this.message ? `: ${this.message}` : '';
|
||||
|
||||
return `Cancel${message}`;
|
||||
|
@ -51,21 +51,21 @@ export function isCancel(value: unknown): value is Cancel {
|
|||
}
|
||||
|
||||
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;
|
||||
const promise = new Promise<Cancel>((resolve) => {
|
||||
action = (message) => {
|
||||
if (this.reason) {
|
||||
if (this.#reason) {
|
||||
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);
|
||||
}
|
||||
|
||||
public static source(): CancelTokenSource {
|
||||
static source(): CancelTokenSource {
|
||||
let cancel!: CancelAction;
|
||||
const token = new CancelToken((action) => {
|
||||
cancel = action;
|
||||
|
@ -86,9 +86,9 @@ export class CancelToken {
|
|||
};
|
||||
}
|
||||
|
||||
public throwIfRequested(): void {
|
||||
if (this.reason) {
|
||||
throw this.reason;
|
||||
throwIfRequested(): void {
|
||||
if (this.#reason) {
|
||||
throw this.#reason;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,11 +5,11 @@ import { AxiosRequestConfig, AxiosResponse, AxiosResponseError } from './Axios';
|
|||
export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
|
||||
|
||||
class AxiosError extends Error {
|
||||
public config: AxiosRequestConfig;
|
||||
public request: AxiosAdapterTask;
|
||||
public response: AxiosErrorResponse;
|
||||
config: AxiosRequestConfig;
|
||||
request: AxiosAdapterTask;
|
||||
response: AxiosErrorResponse;
|
||||
|
||||
public constructor(
|
||||
constructor(
|
||||
message: string,
|
||||
config: AxiosRequestConfig,
|
||||
response: AxiosErrorResponse,
|
||||
|
|
Loading…
Reference in New Issue