1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00
Files
node-redis/packages/client/lib/commands/GEOSEARCH_WITH.ts
Shaya Potter 4708736f3b new "transform arguments" API for better key and metadata extraction (#2733)
* Parser support with all commands

* remove "dist" from all imports for consistency

* address most of my review comments

* small tweak to multi type mapping handling

* tweak multi commands / fix addScript cases

* nits

* addressed all in person review comments

* revert addCommand/addScript changes to multi-commands

addCommand needs to be there for sendCommand like ability within a multi.

If its there, it might as well be used by createCommand() et al, to avoid repeating code.

addScript is there (even though only used once), but now made private to keep the logic for bookkeeping near each other.
2024-10-31 12:16:59 -04:00

74 lines
2.0 KiB
TypeScript

import { CommandParser } from '../client/parser';
import { RedisArgument, ArrayReply, TuplesReply, BlobStringReply, NumberReply, DoubleReply, UnwrapReply, Command } from '../RESP/types';
import GEOSEARCH, { GeoSearchBy, GeoSearchFrom, GeoSearchOptions } from './GEOSEARCH';
export const GEO_REPLY_WITH = {
DISTANCE: 'WITHDIST',
HASH: 'WITHHASH',
COORDINATES: 'WITHCOORD'
} as const;
export type GeoReplyWith = typeof GEO_REPLY_WITH[keyof typeof GEO_REPLY_WITH];
export interface GeoReplyWithMember {
member: BlobStringReply;
distance?: BlobStringReply;
hash?: NumberReply;
coordinates?: {
longitude: DoubleReply;
latitude: DoubleReply;
};
}
export default {
IS_READ_ONLY: GEOSEARCH.IS_READ_ONLY,
parseCommand(
parser: CommandParser,
key: RedisArgument,
from: GeoSearchFrom,
by: GeoSearchBy,
replyWith: Array<GeoReplyWith>,
options?: GeoSearchOptions
) {
GEOSEARCH.parseCommand(parser, key, from, by, options);
parser.push(...replyWith);
parser.preserve = replyWith;
},
transformReply(
reply: UnwrapReply<ArrayReply<TuplesReply<[BlobStringReply, ...Array<any>]>>>,
replyWith: Array<GeoReplyWith>
) {
const replyWithSet = new Set(replyWith);
let index = 0;
const distanceIndex = replyWithSet.has(GEO_REPLY_WITH.DISTANCE) && ++index,
hashIndex = replyWithSet.has(GEO_REPLY_WITH.HASH) && ++index,
coordinatesIndex = replyWithSet.has(GEO_REPLY_WITH.COORDINATES) && ++index;
return reply.map(raw => {
const unwrapped = raw as unknown as UnwrapReply<typeof raw>;
const item: GeoReplyWithMember = {
member: unwrapped[0]
};
if (distanceIndex) {
item.distance = unwrapped[distanceIndex];
}
if (hashIndex) {
item.hash = unwrapped[hashIndex];
}
if (coordinatesIndex) {
const [longitude, latitude] = unwrapped[coordinatesIndex];
item.coordinates = {
longitude,
latitude
};
}
return item;
});
}
} as const satisfies Command;