MCPcopy
hub / github.com/redis/node-redis / attachConfig

Function attachConfig

packages/client/lib/commander.ts:23–76  ·  view source on GitHub ↗
({
  BaseClass,
  commands,
  createCommand,
  createModuleCommand,
  createFunctionCommand,
  createScriptCommand,
  config
}: AttachConfigOptions<M, F, S, RESP>)

Source from the content-addressed store, hash-verified

21}
22
23export function attachConfig<
24 M extends RedisModules,
25 F extends RedisFunctions,
26 S extends RedisScripts,
27 RESP extends RespVersions
28>({
29 BaseClass,
30 commands,
31 createCommand,
32 createModuleCommand,
33 createFunctionCommand,
34 createScriptCommand,
35 config
36}: AttachConfigOptions<M, F, S, RESP>) {
37 const RESP = config?.RESP ?? DEFAULT_RESP,
38 // eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic prototype patching
39 Class: any = class extends BaseClass {};
40
41 for (const [name, command] of Object.entries(commands)) {
42 Class.prototype[name] = createCommand(command, RESP);
43 }
44
45 if (config?.modules) {
46 for (const [moduleName, module] of Object.entries(config.modules)) {
47 // eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic command namespace
48 const fns: Record<string, (...args: Array<any>) => any> = {};
49 for (const [name, command] of Object.entries(module)) {
50 fns[name] = createModuleCommand(command, RESP);
51 }
52
53 attachNamespace(Class.prototype, moduleName, fns);
54 }
55 }
56
57 if (config?.functions) {
58 for (const [library, commands] of Object.entries(config.functions)) {
59 // eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic command namespace
60 const fns: Record<string, (...args: Array<any>) => any> = {};
61 for (const [name, command] of Object.entries(commands)) {
62 fns[name] = createFunctionCommand(name, command, RESP);
63 }
64
65 attachNamespace(Class.prototype, library, fns);
66 }
67 }
68
69 if (config?.scripts) {
70 for (const [name, script] of Object.entries(config.scripts)) {
71 Class.prototype[name] = createScriptCommand(script, RESP);
72 }
73 }
74
75 return Class;
76}
77
78// Per-receiver namespace cache. Keyed by the receiver (original instance or any
79// `withCommandOptions(...)` proxy) so each one gets a namespace bound to itself

Callers 8

factoryMethod · 0.90
createMethod · 0.90
extendMethod · 0.90
factoryMethod · 0.90
factoryMethod · 0.90
extendMethod · 0.90
factoryMethod · 0.90
extendMethod · 0.90

Calls 5

createCommandFunction · 0.85
createModuleCommandFunction · 0.85
attachNamespaceFunction · 0.85
createFunctionCommandFunction · 0.85
createScriptCommandFunction · 0.85

Tested by

no test coverage detected