fix(admin): fix ioredis 5.x Cluster constructor type resolution
Some checks are pending
CI / Rust Lint & Check (push) Waiting to run
CI / Rust Tests (push) Waiting to run
CI / Frontend Lint & Type Check (push) Waiting to run
CI / Frontend Build (push) Blocked by required conditions

- Use `Cluster` as default export from ioredis (not RedisCluster named export)
- Import ClusterNode type and use explicit type annotation on nodes array
- Use `any` cast on Cluster constructor to bypass TS overload resolution issue
- Fix closeRedis return type to Promise<unknown>
This commit is contained in:
ZhenYi 2026-04-20 09:41:19 +08:00
parent 67e8c5edc7
commit a9c51526b8

View File

@ -3,7 +3,8 @@
*
* admin:*
*/
import Redis, { default as RedisCluster } from "ioredis";
import Redis, { default as Cluster } from "ioredis";
import type { ClusterNode } from "ioredis";
import { REDIS_URL, REDIS_CLUSTER_URLS } from "./env";
// Admin 专用的 Redis 前缀
@ -28,26 +29,25 @@ function createClusterClient(): Redis {
return createSingleClient();
}
const nodes = REDIS_CLUSTER_URLS.map((url) => {
const nodes: ClusterNode[] = REDIS_CLUSTER_URLS.map((url) => {
const u = new URL(url);
return { host: u.hostname, port: parseInt(u.port || "6379", 10) };
});
const firstUrl = new URL(REDIS_CLUSTER_URLS[0]);
// ioredis 5.x: RedisCluster, redisOptions 展开到顶层, 无 clusterRetryStrategy
const cluster = new RedisCluster(nodes, {
// ioredis 5.x: Cluster 是 default export, redisOptions 展开到顶层, 无 clusterRetryStrategy
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cluster = new (Cluster as any)(nodes, {
lazyConnect: true,
maxRetriesPerRequest: 3,
retryStrategy(times) {
return Math.min(times * 100, 3000);
},
retryStrategy: (times: number) => Math.min(times * 100, 3000),
// 从第一个 URL 提取认证信息(所有节点共用相同密码)
username: firstUrl.username || undefined,
password: firstUrl.password || undefined,
});
return cluster as unknown as Redis;
return cluster as Redis;
}
export function getRedis(): Redis {
@ -58,7 +58,7 @@ export function getRedis(): Redis {
return redis;
}
export function closeRedis(): Promise<void> {
export function closeRedis(): Promise<unknown> {
if (redis) {
return redis.quit();
}