From 3f4a8a12e0a344b2b729af139169a8abf7d2097d Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Sun, 14 May 2023 21:44:08 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E9=9A=90=E8=97=8F=E5=86=85=E9=83=A8?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/Axios.ts | 24 +++++++++++++++++++++++- src/core/InterceptorManager.ts | 4 ++++ src/core/MiddlewareManager.ts | 30 ++++++------------------------ src/request/cancel.ts | 6 ++++++ tsconfig.json | 1 + 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/core/Axios.ts b/src/core/Axios.ts index afc91cb..d2fb981 100644 --- a/src/core/Axios.ts +++ b/src/core/Axios.ts @@ -501,7 +501,29 @@ export default class Axios { /** * 注册中间件 * - * @param middleware 中间件 + * 示例1:注册一个中间件 + * ```ts + * axios.use(async function middleware(ctx, next) { + * console.log(ctx.req); + * await next(); + * console.log(ctx.res); + * }); + * ``` + * + * 示例2:链式注册多个中间件 + * ```ts + * axios + * .use(async function middleware1(ctx, next) { + * console.log(ctx.req); + * await next(); + * console.log(ctx.res); + * }) + * .use(async function middleware2(ctx, next) { + * console.log(ctx.req); + * await next(); + * console.log(ctx.res); + * }); + * ``` */ use = (middleware: MiddlewareCallback) => { this.#middleware.use(middleware); diff --git a/src/core/InterceptorManager.ts b/src/core/InterceptorManager.ts index 7919930..815b573 100644 --- a/src/core/InterceptorManager.ts +++ b/src/core/InterceptorManager.ts @@ -39,6 +39,8 @@ export default class InterceptorManager { #interceptors = new Map>(); /** + * @internal + * * 拦截器数量 */ get size() { @@ -81,6 +83,8 @@ export default class InterceptorManager { } /** + * @internal + * * 遍历拦截器 * * @param executor 执行器 diff --git a/src/core/MiddlewareManager.ts b/src/core/MiddlewareManager.ts index bc077f7..0b99f0f 100644 --- a/src/core/MiddlewareManager.ts +++ b/src/core/MiddlewareManager.ts @@ -40,30 +40,6 @@ export default class MiddlewareManager { /** * 注册中间件 - * - * 示例1:注册一个中间件 - * ```ts - * axios.use(async function middleware(ctx, next) { - * console.log(ctx.req); - * await next(); - * console.log(ctx.res); - * }); - * ``` - * - * 示例2:链式注册多个中间件 - * ```ts - * axios - * .use(async function middleware1(ctx, next) { - * console.log(ctx.req); - * await next(); - * console.log(ctx.res); - * }) - * .use(async function middleware2(ctx, next) { - * console.log(ctx.req); - * await next(); - * console.log(ctx.res); - * }); - * ``` */ use(middleware: MiddlewareCallback) { assert(isFunction(middleware), 'middleware 不是一个 function'); @@ -71,6 +47,8 @@ export default class MiddlewareManager { } /** + * @internal + * * 创建中间件上下文 */ createContext(config: AxiosRequestConfig): MiddlewareContext { @@ -81,6 +59,8 @@ export default class MiddlewareManager { } /** + * @internal + * * 运行中间件 * * @param ctx 中间件上下文 @@ -95,6 +75,8 @@ export default class MiddlewareManager { } /** + * @internal + * * 强化运行中间件 * * @param enhancer 强化器 diff --git a/src/request/cancel.ts b/src/request/cancel.ts index bfd81c3..96af486 100644 --- a/src/request/cancel.ts +++ b/src/request/cancel.ts @@ -46,6 +46,9 @@ export function isCancel(value: unknown): value is Cancel { export class CancelToken { #reason?: Cancel; + /** + * @internal + */ onCancel: Promise['then']; constructor(executor: CancelExecutor) { @@ -79,6 +82,9 @@ export class CancelToken { }; } + /** + * @internal + */ throwIfRequested(): void { if (this.#reason) { throw this.#reason; diff --git a/tsconfig.json b/tsconfig.json index c6ad63a..c6d5086 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "module": "ESNext", "strict": true, "noEmit": true, + "stripInternal": true, "moduleResolution": "node", "skipLibCheck": true, "skipDefaultLibCheck": true,