From 711a04f09d14e43519cc742de269a1cd4f391851 Mon Sep 17 00:00:00 2001 From: redshift <213178690+1ftredsh@users.noreply.github.com> Date: Tue, 28 Apr 2026 15:04:52 +0530 Subject: [PATCH] refactor: consolidate ensureDaemonClient in daemon-client.ts - Move get-or-create client logic from integrations/index.ts into ensureDaemonClient() in daemon-client.ts as a generic utility. - Update setupIntegration() and the CLI clients add -- paths to use the new helper. - Return { client, created } so callers can decide whether to log. - Remove IntegrationClient alias and ensureIntegrationClient export. --- src/cli.ts | 12 ++++++++-- src/integrations/index.ts | 47 +++++++++----------------------------- src/utils/daemon-client.ts | 29 +++++++++++++++++++++++ 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index adba3a8..cf14187 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -9,6 +9,7 @@ import { getDaemonBaseUrl, getNpubSuffix, addDaemonClient, + ensureDaemonClient, } from "./utils/daemon-client"; import { existsSync, mkdirSync } from "fs"; import { execSync } from "child_process"; @@ -25,7 +26,6 @@ import { setupIntegration, CLIENT_CONFIGS, CLIENT_INTEGRATIONS, - ensureIntegrationClient, } from "./integrations"; import * as QRCode from "qrcode"; import { normalizeNostrPubkey, npubFromPubkey } from "./utils/nip98"; @@ -855,7 +855,15 @@ clientsCmd if (!integrationFn || !integrationConfig) continue; try { - const client = await ensureIntegrationClient(integrationConfig); + const { client, created } = await ensureDaemonClient( + integrationConfig.name, + integrationConfig.clientId, + ); + if (created) { + logger.log(`Created new API key for ${integrationConfig.name}`); + } else { + logger.log(`Using existing API key for ${integrationConfig.name}`); + } await integrationFn(config, client.apiKey, integrationConfig); console.log(`\n ${integrationConfig.name}:`); diff --git a/src/integrations/index.ts b/src/integrations/index.ts index b61d5f0..fecfc2d 100644 --- a/src/integrations/index.ts +++ b/src/integrations/index.ts @@ -1,8 +1,7 @@ import type { RoutstrdConfig } from "../utils/config"; import { logger } from "../utils/logger"; import { - addDaemonClient, - callDaemon, + ensureDaemonClient, type DaemonClient, } from "../utils/daemon-client"; import { installOpencodeIntegration } from "./opencode"; @@ -13,8 +12,6 @@ import type { IntegrationConfig } from "./registry"; import { CLIENT_CONFIGS } from "./registry"; export { CLIENT_INTEGRATIONS, CLIENT_CONFIGS, runIntegrationsForClients } from "./registry"; -export type IntegrationClient = DaemonClient; - function ask(question: string): Promise { process.stdout.write(question); @@ -45,37 +42,6 @@ function parseChoice(input: string): number { return 1; } -export async function ensureIntegrationClient( - integrationConfig: IntegrationConfig, -): Promise { - try { - const { client } = await addDaemonClient(integrationConfig.name); - - logger.log(`Created new API key for ${integrationConfig.name}`); - return client; - } catch (error) { - const message = (error as Error).message || ""; - if (!message.includes("already exists")) { - throw error; - } - - const clientsResult = await callDaemon("/clients"); - const clients = - (clientsResult.output as { clients?: IntegrationClient[] } | undefined) - ?.clients || []; - const client = clients.find((c) => c.id === integrationConfig.clientId); - - if (!client?.apiKey) { - throw new Error( - `Client '${integrationConfig.clientId}' already exists but could not be fetched from the daemon.`, - ); - } - - logger.log(`Using existing API key for ${integrationConfig.name}`); - return client; - } -} - export async function setupIntegration( config: RoutstrdConfig, ): Promise { @@ -103,7 +69,16 @@ export async function setupIntegration( } const integrationConfig = CLIENT_CONFIGS[key]!; - const client = await ensureIntegrationClient(integrationConfig); + const { client, created } = await ensureDaemonClient( + integrationConfig.name, + integrationConfig.clientId, + ); + + if (created) { + logger.log(`Created new API key for ${integrationConfig.name}`); + } else { + logger.log(`Using existing API key for ${integrationConfig.name}`); + } if (key === "opencode") { await installOpencodeIntegration(config, client.apiKey, integrationConfig); diff --git a/src/utils/daemon-client.ts b/src/utils/daemon-client.ts index 49893b1..c492938 100644 --- a/src/utils/daemon-client.ts +++ b/src/utils/daemon-client.ts @@ -44,6 +44,35 @@ export async function addDaemonClient( return { message: output.message, client: output.client }; } +export async function ensureDaemonClient( + name: string, + clientId: string, +): Promise<{ client: DaemonClient; created: boolean }> { + try { + const { client } = await addDaemonClient(name); + return { client, created: true }; + } catch (error) { + const message = (error as Error).message || ""; + if (!message.includes("already exists")) { + throw error; + } + + const clientsResult = await callDaemon("/clients"); + const clients = + (clientsResult.output as { clients?: DaemonClient[] } | undefined) + ?.clients || []; + const client = clients.find((c) => c.id === clientId); + + if (!client?.apiKey) { + throw new Error( + `Client '${clientId}' already exists but could not be fetched from the daemon.`, + ); + } + + return { client, created: false }; + } +} + export async function loadConfig(): Promise { try { if (existsSync(CONFIG_FILE)) {