@@ -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