From 43c18d172387477f6b2521b5300ce357aee8491d Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Thu, 6 Apr 2023 16:00:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E7=A7=81=E6=9C=89=E5=B1=9E=E6=80=A7=E8=AF=AD=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/Axios.ts | 32 ++++++++++++++++---------------- src/core/InterceptorManager.ts | 18 +++++++++--------- src/core/cancel.ts | 26 +++++++++++++------------- src/core/createError.ts | 8 ++++---- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/core/Axios.ts b/src/core/Axios.ts index ae6fb21..6871960 100644 --- a/src/core/Axios.ts +++ b/src/core/Axios.ts @@ -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( + request( config: AxiosRequestConfig, ): Promise> { const requestConfig = mergeConfig(this.defaults, config); diff --git a/src/core/InterceptorManager.ts b/src/core/InterceptorManager.ts index 7e95785..3c3ce65 100644 --- a/src/core/InterceptorManager.ts +++ b/src/core/InterceptorManager.ts @@ -16,28 +16,28 @@ export interface InterceptorExecutor { } export default class InterceptorManager { - private id = 0; + #id = 0; - private interceptors: AnyObject> = {}; + #interceptors: AnyObject> = {}; - public use( + use( resolved: InterceptorResolved, 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[] = Object.values(this.interceptors); + forEach(executor: InterceptorExecutor, reverse?: boolean): void { + let interceptors: Interceptor[] = Object.values(this.#interceptors); if (reverse) interceptors = interceptors.reverse(); interceptors.forEach(executor); } diff --git a/src/core/cancel.ts b/src/core/cancel.ts index 452ea37..98c0475 100644 --- a/src/core/cancel.ts +++ b/src/core/cancel.ts @@ -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['then']; + onCancel: Promise['then']; - public constructor(executor: CancelExecutor) { + constructor(executor: CancelExecutor) { let action!: CancelAction; const promise = new Promise((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; } } } diff --git a/src/core/createError.ts b/src/core/createError.ts index 72e508d..235944c 100644 --- a/src/core/createError.ts +++ b/src/core/createError.ts @@ -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,