Skip to content

Commit 4f75d1e

Browse files
committed
refactor: update ESLint configuration and enhance RedisService with cluster mode checks
1 parent 7157142 commit 4f75d1e

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

examples/nest-redis-example/eslint.config.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ export default tseslint.config(
1717
...globals.node,
1818
...globals.jest,
1919
},
20-
ecmaVersion: 5,
21-
sourceType: 'module',
20+
sourceType: 'commonjs',
2221
parserOptions: {
2322
projectService: true,
2423
tsconfigRootDir: import.meta.dirname,

packages/node-redis/lib/redis.service.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,34 @@ export class RedisService<
2222

2323
/**
2424
* Get the redis cluster client instance.
25+
* Returns null if the client is not in cluster mode.
2526
*/
26-
getCluster(): RedisClusterType<M, F, S> {
27-
return this.client as unknown as RedisClusterType<M, F, S>;
27+
getCluster(): RedisClusterType<M, F, S> | null {
28+
if (!this.isClusterMode()) {
29+
return null;
30+
}
31+
// Safe type assertion after checking cluster mode
32+
return this.client as RedisClusterType<M, F, S>;
33+
}
34+
35+
36+
/***
37+
* Get the redis cluster client instance.
38+
* Throws an error if the client is not in cluster mode.
39+
*/
40+
getClusterOrThrow(): RedisClusterType<M, F, S> {
41+
if (!this.isClusterMode()) {
42+
throw new Error('Client is not in cluster mode');
43+
}
44+
return this.client as RedisClusterType<M, F, S>;
2845
}
2946

3047
/**
3148
* Check if the client is in cluster mode.
3249
*/
3350
isClusterMode(): boolean {
3451
// check if the client is a cluster client
35-
if ('masters' in this.client) {
36-
return true;
37-
}
38-
return false;
52+
return 'masters' in this.client;
3953
}
4054

4155
/**
@@ -48,11 +62,16 @@ export class RedisService<
4862
/**
4963
* Get cluster information if in cluster mode.
5064
*/
51-
getClusterInfo() {
65+
getClusterInfo(): 'single' | { type: 'cluster'; masters: number; nodes: string[] } {
5266
if (!this.isClusterMode()) {
5367
return 'single';
5468
}
69+
5570
const cluster = this.getCluster();
71+
if (!cluster) {
72+
return 'single';
73+
}
74+
5675
try {
5776
const masters = cluster.masters;
5877
return {

0 commit comments

Comments
 (0)