You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
V5 bringing RESP3, Sentinel and TypeMapping to node-redis
RESP3 Support - Some commands responses in RESP3 aren't stable yet and therefore return an "untyped" ReplyUnion. Sentinel TypeMapping Correctly types Multi commands Note: some API changes to be further documented in v4-to-v5.md
This commit is contained in:
@@ -1,29 +1,50 @@
|
||||
import { SugGetOptions, transformArguments as transformSugGetArguments } from './SUGGET';
|
||||
import { NullReply, ArrayReply, BlobStringReply, DoubleReply, UnwrapReply, Command, TypeMapping } from '@redis/client/dist/lib/RESP/types';
|
||||
import { isNullReply, transformDoubleReply } from '@redis/client/dist/lib/commands/generic-transformers';
|
||||
import SUGGET from './SUGGET';
|
||||
|
||||
export { IS_READ_ONLY } from './SUGGET';
|
||||
|
||||
export function transformArguments(key: string, prefix: string, options?: SugGetOptions): Array<string> {
|
||||
return [
|
||||
...transformSugGetArguments(key, prefix, options),
|
||||
'WITHSCORES'
|
||||
];
|
||||
type SuggestScore = {
|
||||
suggestion: BlobStringReply;
|
||||
score: DoubleReply;
|
||||
}
|
||||
|
||||
export interface SuggestionWithScores {
|
||||
suggestion: string;
|
||||
score: number;
|
||||
}
|
||||
export default {
|
||||
FIRST_KEY_INDEX: SUGGET.FIRST_KEY_INDEX,
|
||||
IS_READ_ONLY: SUGGET.IS_READ_ONLY,
|
||||
transformArguments(...args: Parameters<typeof SUGGET.transformArguments>) {
|
||||
const transformedArguments = SUGGET.transformArguments(...args);
|
||||
transformedArguments.push('WITHSCORES');
|
||||
return transformedArguments;
|
||||
},
|
||||
transformReply: {
|
||||
2: (reply: NullReply | UnwrapReply<ArrayReply<BlobStringReply>>, preserve?: any, typeMapping?: TypeMapping) => {
|
||||
if (isNullReply(reply)) return null;
|
||||
|
||||
export function transformReply(rawReply: Array<string> | null): Array<SuggestionWithScores> | null {
|
||||
if (rawReply === null) return null;
|
||||
const transformedReply: Array<SuggestScore> = new Array(reply.length / 2);
|
||||
let replyIndex = 0,
|
||||
arrIndex = 0;
|
||||
while (replyIndex < reply.length) {
|
||||
transformedReply[arrIndex++] = {
|
||||
suggestion: reply[replyIndex++],
|
||||
score: transformDoubleReply[2](reply[replyIndex++], preserve, typeMapping)
|
||||
};
|
||||
}
|
||||
|
||||
const transformedReply = [];
|
||||
for (let i = 0; i < rawReply.length; i += 2) {
|
||||
transformedReply.push({
|
||||
suggestion: rawReply[i],
|
||||
score: Number(rawReply[i + 1])
|
||||
});
|
||||
return transformedReply;
|
||||
},
|
||||
3: (reply: UnwrapReply<ArrayReply<BlobStringReply | DoubleReply>>) => {
|
||||
if (isNullReply(reply)) return null;
|
||||
|
||||
const transformedReply: Array<SuggestScore> = new Array(reply.length / 2);
|
||||
let replyIndex = 0,
|
||||
arrIndex = 0;
|
||||
while (replyIndex < reply.length) {
|
||||
transformedReply[arrIndex++] = {
|
||||
suggestion: reply[replyIndex++] as BlobStringReply,
|
||||
score: reply[replyIndex++] as DoubleReply
|
||||
};
|
||||
}
|
||||
|
||||
return transformedReply;
|
||||
}
|
||||
|
||||
return transformedReply;
|
||||
}
|
||||
}
|
||||
} as const satisfies Command;
|
||||
|
Reference in New Issue
Block a user