@@ -11,18 +11,27 @@ import { Command, CommandArguments, CommandSignature, TypeMapping, CommanderConf
11
11
import RedisClientMultiCommand , { RedisClientMultiCommandType } from './multi-command' ;
12
12
import { RedisMultiQueuedCommand } from '../multi-command' ;
13
13
import HELLO , { HelloOptions } from '../commands/HELLO' ;
14
- import { ReplyWithTypeMapping , CommandReply } from '../RESP/types' ;
15
14
import { ScanOptions , ScanCommonOptions } from '../commands/SCAN' ;
16
15
import { RedisLegacyClient , RedisLegacyClientType } from './legacy-mode' ;
17
16
// import { RedisClientPool } from './pool';
18
17
18
+ interface ClientCommander <
19
+ M extends RedisModules ,
20
+ F extends RedisFunctions ,
21
+ S extends RedisScripts ,
22
+ RESP extends RespVersions ,
23
+ TYPE_MAPPING extends TypeMapping
24
+ > extends CommanderConfig < M , F , S , RESP > {
25
+ commandOptions ?: CommandOptions < TYPE_MAPPING > ;
26
+ }
27
+
19
28
export interface RedisClientOptions <
20
29
M extends RedisModules = RedisModules ,
21
30
F extends RedisFunctions = RedisFunctions ,
22
31
S extends RedisScripts = RedisScripts ,
23
32
RESP extends RespVersions = RespVersions ,
24
33
TYPE_MAPPING extends TypeMapping = TypeMapping
25
- > extends CommanderConfig < M , F , S , RESP > , TypeMappingOption < TYPE_MAPPING > {
34
+ > extends ClientCommander < M , F , S , RESP , TYPE_MAPPING > {
26
35
/**
27
36
* `redis[s]://[[username][:password]@][host][:port][/db-number]`
28
37
* See [`redis`](https://www.iana.org/assignments/uri-schemes/prov/redis) and [`rediss`](https://www.iana.org/assignments/uri-schemes/prov/rediss) IANA registration for more details
@@ -68,13 +77,6 @@ export interface RedisClientOptions<
68
77
pingInterval ?: number ;
69
78
}
70
79
71
- export interface TypeMappingOption < TYPE_MAPPING extends TypeMapping > {
72
- /**
73
- * Maps bettwen RESP types to JavaScript types
74
- */
75
- typeMapping ?: TYPE_MAPPING ;
76
- }
77
-
78
80
type WithCommands <
79
81
RESP extends RespVersions ,
80
82
TYPE_MAPPING extends TypeMapping
@@ -134,7 +136,7 @@ export type RedisClientType<
134
136
RedisClientExtensions < M , F , S , RESP , TYPE_MAPPING >
135
137
) ;
136
138
137
- type ProxyClient = RedisClient < any , any , any , any , any > & { commandOptions ?: CommandOptions } ;
139
+ type ProxyClient = RedisClient < any , any , any , any , any > ;
138
140
139
141
type NamespaceProxyClient = { self : ProxyClient } ;
140
142
@@ -153,7 +155,7 @@ export default class RedisClient<
153
155
const transformReply = getTransformReply ( command , resp ) ;
154
156
return async function ( this : ProxyClient , ...args : Array < unknown > ) {
155
157
const redisArgs = command . transformArguments ( ...args ) ,
156
- reply = await this . sendCommand ( redisArgs , this . commandOptions ) ;
158
+ reply = await this . sendCommand ( redisArgs , this . _commandOptions ) ;
157
159
return transformReply ?
158
160
transformReply ( reply , redisArgs . preserve ) :
159
161
reply ;
@@ -164,7 +166,7 @@ export default class RedisClient<
164
166
const transformReply = getTransformReply ( command , resp ) ;
165
167
return async function ( this : NamespaceProxyClient , ...args : Array < unknown > ) {
166
168
const redisArgs = command . transformArguments ( ...args ) ,
167
- reply = await this . self . sendCommand ( redisArgs , this . self . commandOptions ) ;
169
+ reply = await this . self . sendCommand ( redisArgs , this . self . _commandOptions ) ;
168
170
return transformReply ?
169
171
transformReply ( reply , redisArgs . preserve ) :
170
172
reply ;
@@ -178,7 +180,7 @@ export default class RedisClient<
178
180
const fnArgs = fn . transformArguments ( ...args ) ,
179
181
reply = await this . self . sendCommand (
180
182
prefix . concat ( fnArgs ) ,
181
- this . self . commandOptions
183
+ this . self . _commandOptions
182
184
) ;
183
185
return transformReply ?
184
186
transformReply ( reply , fnArgs . preserve ) :
@@ -192,12 +194,12 @@ export default class RedisClient<
192
194
return async function ( this : ProxyClient , ...args : Array < unknown > ) {
193
195
const scriptArgs = script . transformArguments ( ...args ) ,
194
196
redisArgs = prefix . concat ( scriptArgs ) ,
195
- reply = await this . sendCommand ( redisArgs , this . commandOptions ) . catch ( ( err : unknown ) => {
197
+ reply = await this . sendCommand ( redisArgs , this . _commandOptions ) . catch ( ( err : unknown ) => {
196
198
if ( ! ( err as Error ) ?. message ?. startsWith ?.( 'NOSCRIPT' ) ) throw err ;
197
199
198
200
redisArgs [ 0 ] = 'EVAL' ;
199
201
redisArgs [ 1 ] = script . SCRIPT ;
200
- return this . sendCommand ( redisArgs , this . commandOptions ) ;
202
+ return this . sendCommand ( redisArgs , this . _commandOptions ) ;
201
203
} ) ;
202
204
return transformReply ?
203
205
transformReply ( reply , scriptArgs . preserve ) :
@@ -211,7 +213,7 @@ export default class RedisClient<
211
213
S extends RedisScripts = { } ,
212
214
RESP extends RespVersions = 2 ,
213
215
TYPE_MAPPING extends TypeMapping = { }
214
- > ( config ?: CommanderConfig < M , F , S , RESP > & TypeMappingOption < TYPE_MAPPING > ) {
216
+ > ( config ?: ClientCommander < M , F , S , RESP , TYPE_MAPPING > ) {
215
217
const Client = attachConfig ( {
216
218
BaseClass : RedisClient ,
217
219
commands : COMMANDS ,
@@ -282,10 +284,11 @@ export default class RedisClient<
282
284
283
285
self = this ;
284
286
285
- private readonly _options ?: RedisClientOptions < M , F , S , RESP > ;
287
+ private readonly _options ?: RedisClientOptions < M , F , S , RESP , TYPE_MAPPING > ;
286
288
private readonly _socket : RedisSocket ;
287
289
private readonly _queue : RedisCommandsQueue ;
288
290
private _selectedDB = 0 ;
291
+ private _commandOptions ?: CommandOptions < TYPE_MAPPING > ;
289
292
290
293
get options ( ) : RedisClientOptions < M , F , S , RESP > | undefined {
291
294
return this . _options ;
@@ -303,14 +306,14 @@ export default class RedisClient<
303
306
return this . _queue . isPubSubActive ;
304
307
}
305
308
306
- constructor ( options ?: RedisClientOptions < M , F , S , RESP > ) {
309
+ constructor ( options ?: RedisClientOptions < M , F , S , RESP , TYPE_MAPPING > ) {
307
310
super ( ) ;
308
311
this . _options = this . _initiateOptions ( options ) ;
309
312
this . _queue = this . _initiateQueue ( ) ;
310
313
this . _socket = this . _initiateSocket ( ) ;
311
314
}
312
315
313
- private _initiateOptions ( options ?: RedisClientOptions < M , F , S , RESP > ) : RedisClientOptions < M , F , S , RESP > | undefined {
316
+ private _initiateOptions ( options ?: RedisClientOptions < M , F , S , RESP , TYPE_MAPPING > ) : RedisClientOptions < M , F , S , RESP , TYPE_MAPPING > | undefined {
314
317
if ( options ?. url ) {
315
318
const parsed = RedisClient . parseURL ( options . url ) ;
316
319
if ( options . socket ) {
@@ -324,10 +327,8 @@ export default class RedisClient<
324
327
this . _selectedDB = options . database ;
325
328
}
326
329
327
- if ( options ?. typeMapping ) {
328
- ( this as unknown as ProxyClient ) . commandOptions = {
329
- typeMapping : options . typeMapping
330
- } ;
330
+ if ( options ?. commandOptions ) {
331
+ this . _commandOptions = options . commandOptions ;
331
332
}
332
333
333
334
return options ;
@@ -462,15 +463,18 @@ export default class RedisClient<
462
463
} , this . _options . pingInterval ) ;
463
464
}
464
465
465
- withCommandOptions < T extends CommandOptions > ( options : T ) {
466
+ withCommandOptions <
467
+ OPTIONS extends CommandOptions < TYPE_MAPPING > ,
468
+ TYPE_MAPPING extends TypeMapping
469
+ > ( options : OPTIONS ) {
466
470
const proxy = Object . create ( this . self ) ;
467
- proxy . commandOptions = options ;
471
+ proxy . _commandOptions = options ;
468
472
return proxy as RedisClientType <
469
473
M ,
470
474
F ,
471
475
S ,
472
476
RESP ,
473
- T [ 'typeMapping' ] extends TypeMapping ? T [ 'typeMapping' ] : { }
477
+ TYPE_MAPPING extends TypeMapping ? TYPE_MAPPING : { }
474
478
> ;
475
479
}
476
480
@@ -482,8 +486,8 @@ export default class RedisClient<
482
486
value : V
483
487
) {
484
488
const proxy = Object . create ( this . self ) ;
485
- proxy . commandOptions = Object . create ( ( this as unknown as ProxyClient ) . commandOptions ?? null ) ;
486
- proxy . commandOptions [ key ] = value ;
489
+ proxy . _commandOptions = Object . create ( this . _commandOptions ?? null ) ;
490
+ proxy . _commandOptions [ key ] = value ;
487
491
return proxy as RedisClientType <
488
492
M ,
489
493
F ,
@@ -539,17 +543,11 @@ export default class RedisClient<
539
543
_RESP extends RespVersions = RESP ,
540
544
_TYPE_MAPPING extends TypeMapping = TYPE_MAPPING
541
545
> ( overrides ?: Partial < RedisClientOptions < _M , _F , _S , _RESP , _TYPE_MAPPING > > ) {
542
- const client = new ( Object . getPrototypeOf ( this ) . constructor ) ( {
546
+ return new ( Object . getPrototypeOf ( this ) . constructor ) ( {
543
547
...this . _options ,
548
+ commandOptions : this . _commandOptions ,
544
549
...overrides
545
550
} ) as RedisClientType < _M , _F , _S , _RESP , _TYPE_MAPPING > ;
546
-
547
- const { commandOptions } = this as ProxyClient ;
548
- if ( commandOptions ) {
549
- return client . withCommandOptions ( commandOptions ) ;
550
- }
551
-
552
- return client ;
553
551
}
554
552
555
553
connect ( ) {
@@ -732,7 +730,7 @@ export default class RedisClient<
732
730
733
731
const promise = Promise . all (
734
732
commands . map ( ( { args } ) => this . _queue . addCommand ( args , {
735
- typeMapping : ( this as ProxyClient ) . commandOptions ?. typeMapping
733
+ typeMapping : this . _commandOptions ?. typeMapping
736
734
} ) )
737
735
) ;
738
736
this . _scheduleWrite ( ) ;
@@ -750,7 +748,7 @@ export default class RedisClient<
750
748
return Promise . reject ( new ClientClosedError ( ) ) ;
751
749
}
752
750
753
- const typeMapping = ( this as ProxyClient ) . commandOptions ?. typeMapping ,
751
+ const typeMapping = this . _commandOptions ?. typeMapping ,
754
752
chainId = Symbol ( 'MULTI Chain' ) ,
755
753
promises = [
756
754
this . _queue . addCommand ( [ 'MULTI' ] , { chainId } ) ,
0 commit comments