From bc03275262e0ec467669ee9c1b4694e3c8bf2df1 Mon Sep 17 00:00:00 2001 From: SpecialX <47072643+wangxiner55@users.noreply.github.com> Date: Sun, 5 Jul 2026 17:33:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor(cache):=20=E6=8F=90=E5=8D=87=20upstash?= =?UTF-8?q?-modules=20=E7=B1=BB=E5=9E=8B=E5=A3=B0=E6=98=8E=E8=87=B3=20shar?= =?UTF-8?q?ed/lib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/rate-limit/upstash-modules.d.ts | 71 ++---------------- src/shared/lib/upstash-modules.d.ts | 72 +++++++++++++++++++ 2 files changed, 77 insertions(+), 66 deletions(-) create mode 100644 src/shared/lib/upstash-modules.d.ts diff --git a/src/shared/lib/rate-limit/upstash-modules.d.ts b/src/shared/lib/rate-limit/upstash-modules.d.ts index 9fbe393..e8e4b82 100644 --- a/src/shared/lib/rate-limit/upstash-modules.d.ts +++ b/src/shared/lib/rate-limit/upstash-modules.d.ts @@ -1,69 +1,8 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ /** - * Upstash SDK 可选依赖类型声明(audit-P1-6) + * 向后兼容 shim:实际声明已提升至 shared/lib/upstash-modules.d.ts + * rate-limit 与 cache 模块共用同一份类型声明。 * - * 这两个包通过动态 import 在运行时加载,仅在 `RATE_LIMIT_DRIVER=redis` - * 时才需要安装。默认内存模式下无需安装。 - * - * 安装命令: - * npm install @upstash/ratelimit @upstash/redis - * - * 安装后这两个包自带的类型声明将覆盖此处的 any 声明, - * 提供完整的类型检查。 + * 保留此文件避免破坏现有 import 路径。 + * 新代码请直接从 @/shared/lib/upstash-modules.d.ts 引用。 */ - -declare module "@upstash/ratelimit" { - export class Ratelimit { - constructor(config: { - redis: any - limiter: any - analytics?: boolean - prefix?: string - }) - limit( - identifier: string, - ): Promise<{ - success: boolean - limit: number - remaining: number - reset: number - pending: Promise - }> - reset(identifier: string): Promise - static slidingWindow( - max: number, - window: string, - ): { type: "sliding-window"; max: number; window: string } - static fixedWindow( - max: number, - window: string, - ): { type: "fixed-window"; max: number; window: string } - static tokenBucket( - max: number, - window: string, - refillRate: number, - ): { type: "token-bucket"; max: number; window: string; refillRate: number } - } -} - -declare module "@upstash/redis" { - export class Redis { - constructor(config: { - url: string - token: string - automaticDeserialization?: boolean - responseEncoding?: "base64" | "none" - retry?: { - retries: number - backoff: (retryCount: number) => number - } - }) - get(key: string): Promise - set(key: string, value: string): Promise<"OK"> - del(...keys: string[]): Promise - incr(key: string): Promise - expire(key: string, seconds: number): Promise - pexpire(key: string, milliseconds: number): Promise - [key: string]: any - } -} +export {} diff --git a/src/shared/lib/upstash-modules.d.ts b/src/shared/lib/upstash-modules.d.ts new file mode 100644 index 0000000..020c6d6 --- /dev/null +++ b/src/shared/lib/upstash-modules.d.ts @@ -0,0 +1,72 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/** + * Upstash SDK 可选依赖类型声明(cache + rate-limit 共用) + * + * 这两个包通过动态 import 在运行时加载,仅在 `RATE_LIMIT_DRIVER=redis` + * 或 `CACHE_DRIVER=redis` 时才需要安装。默认内存模式下无需安装。 + * + * 安装命令: + * npm install @upstash/ratelimit @upstash/redis + * + * 安装后这两个包自带的类型声明将覆盖此处的 any 声明, + * 提供完整的类型检查。 + * + * 注意:rate-limit 与 cache 模块共用同一份类型声明, + * 避免在多处重复维护。 + */ + +declare module "@upstash/ratelimit" { + export class Ratelimit { + constructor(config: { + redis: any + limiter: any + analytics?: boolean + prefix?: string + }) + limit( + identifier: string, + ): Promise<{ + success: boolean + limit: number + remaining: number + reset: number + pending: Promise + }> + reset(identifier: string): Promise + static slidingWindow( + max: number, + window: string, + ): { type: "sliding-window"; max: number; window: string } + static fixedWindow( + max: number, + window: string, + ): { type: "fixed-window"; max: number; window: string } + static tokenBucket( + max: number, + window: string, + refillRate: number, + ): { type: "token-bucket"; max: number; window: string; refillRate: number } + } +} + +declare module "@upstash/redis" { + export class Redis { + constructor(config: { + url: string + token: string + automaticDeserialization?: boolean + responseEncoding?: "base64" | "none" + retry?: { + retries: number + backoff: (retryCount: number) => number + } + }) + get(key: string): Promise + set(key: string, value: string): Promise<"OK"> + del(...keys: string[]): Promise + incr(key: string): Promise + expire(key: string, seconds: number): Promise + pexpire(key: string, milliseconds: number): Promise + [key: string]: any + } +}