|
| 1 | +import type { RedisClientType } from "@redis/client"; |
| 2 | +import type { Redis } from "ioredis"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Adapter to make an ioredis client compatible with the interface expected by createRedisHandler. |
| 6 | + * |
| 7 | + * @param client - The ioredis client instance. |
| 8 | + * @returns A proxy that implements the subset of RedisClientType used by this library. |
| 9 | + */ |
| 10 | +export function ioredisAdapter(client: Redis): RedisClientType { |
| 11 | + return new Proxy(client, { |
| 12 | + get(target, prop, receiver) { |
| 13 | + if (prop === "isReady") { |
| 14 | + return target.status === "ready"; |
| 15 | + } |
| 16 | + |
| 17 | + if (prop === "hScan") { |
| 18 | + return async ( |
| 19 | + key: string, |
| 20 | + cursor: string, |
| 21 | + options?: { COUNT?: number }, |
| 22 | + ) => { |
| 23 | + let result: [string, string[]]; |
| 24 | + |
| 25 | + if (options?.COUNT) { |
| 26 | + result = await target.hscan(key, cursor, "COUNT", options.COUNT); |
| 27 | + } else { |
| 28 | + result = await target.hscan(key, cursor); |
| 29 | + } |
| 30 | + |
| 31 | + const [newCursor, items] = result; |
| 32 | + |
| 33 | + const entries = []; |
| 34 | + for (let i = 0; i < items.length; i += 2) { |
| 35 | + entries.push({ field: items[i], value: items[i + 1] }); |
| 36 | + } |
| 37 | + |
| 38 | + return { |
| 39 | + cursor: newCursor, |
| 40 | + entries, |
| 41 | + }; |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + if (prop === "hDel") { |
| 46 | + return async (key: string, fields: string | string[]) => { |
| 47 | + const args = Array.isArray(fields) ? fields : [fields]; |
| 48 | + if (args.length === 0) { |
| 49 | + return 0; |
| 50 | + } |
| 51 | + return target.hdel(key, ...args); |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + if (prop === "unlink") { |
| 56 | + return async (keys: string | string[]) => { |
| 57 | + const args = Array.isArray(keys) ? keys : [keys]; |
| 58 | + if (args.length === 0) { |
| 59 | + return 0; |
| 60 | + } |
| 61 | + return target.unlink(...args); |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + if (prop === "set") { |
| 66 | + return async (key: string, value: string, options?: any) => { |
| 67 | + if (!options) { |
| 68 | + return target.set(key, value); |
| 69 | + } |
| 70 | + |
| 71 | + const extraArgs: (string | number)[] = []; |
| 72 | + |
| 73 | + // Expiration options (mutually exclusive) |
| 74 | + if (options.EXAT) { |
| 75 | + extraArgs.push("EXAT", options.EXAT); |
| 76 | + } else if (options.PXAT) { |
| 77 | + extraArgs.push("PXAT", options.PXAT); |
| 78 | + } else if (options.EX) { |
| 79 | + extraArgs.push("EX", options.EX); |
| 80 | + } else if (options.PX) { |
| 81 | + extraArgs.push("PX", options.PX); |
| 82 | + } else if (options.KEEPTTL) { |
| 83 | + extraArgs.push("KEEPTTL"); |
| 84 | + } |
| 85 | + |
| 86 | + // Condition options (mutually exclusive with each other, but can be combined with expiration) |
| 87 | + if (options.NX) { |
| 88 | + extraArgs.push("NX"); |
| 89 | + } else if (options.XX) { |
| 90 | + extraArgs.push("XX"); |
| 91 | + } |
| 92 | + |
| 93 | + // GET option (can be combined with others) |
| 94 | + if (options.GET) { |
| 95 | + extraArgs.push("GET"); |
| 96 | + } |
| 97 | + |
| 98 | + // Cast to a generic signature to avoid overload mismatch issues with dynamic args |
| 99 | + const setFn = target.set as unknown as ( |
| 100 | + key: string, |
| 101 | + value: string | number, |
| 102 | + ...args: (string | number)[] |
| 103 | + ) => Promise<string | null>; |
| 104 | + |
| 105 | + return setFn(key, value, ...extraArgs); |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + if (prop === "hmGet") { |
| 110 | + return async (key: string, fields: string | string[]) => { |
| 111 | + const args = Array.isArray(fields) ? fields : [fields]; |
| 112 | + if (args.length === 0) { |
| 113 | + return []; |
| 114 | + } |
| 115 | + return target.hmget(key, ...args); |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + if (prop === "get") { |
| 120 | + return target.get.bind(target); |
| 121 | + } |
| 122 | + |
| 123 | + if (prop === "expireAt") { |
| 124 | + return target.expireat.bind(target); |
| 125 | + } |
| 126 | + |
| 127 | + if (prop === "hSet") { |
| 128 | + return target.hset.bind(target); |
| 129 | + } |
| 130 | + |
| 131 | + if (prop === "hExists") { |
| 132 | + return target.hexists.bind(target); |
| 133 | + } |
| 134 | + |
| 135 | + if (prop === "on") { |
| 136 | + return target.on.bind(target); |
| 137 | + } |
| 138 | + |
| 139 | + if (prop === "destroy") { |
| 140 | + return async () => { |
| 141 | + await target.quit(); |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + return Reflect.get(target, prop, receiver); |
| 146 | + }, |
| 147 | + }) as unknown as RedisClientType; |
| 148 | +} |
0 commit comments