mirror of
https://github.com/Routstr/routstrd.git
synced 2026-10-05 12:28:23 +00:00
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 --<integration>
paths to use the new helper.
- Return { client, created } so callers can decide whether to log.
- Remove IntegrationClient alias and ensureIntegrationClient export.
This commit is contained in:
+10
-2
@@ -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}:`);
|
||||
|
||||
+11
-36
@@ -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<string> {
|
||||
process.stdout.write(question);
|
||||
|
||||
@@ -45,37 +42,6 @@ function parseChoice(input: string): number {
|
||||
return 1;
|
||||
}
|
||||
|
||||
export async function ensureIntegrationClient(
|
||||
integrationConfig: IntegrationConfig,
|
||||
): Promise<IntegrationClient> {
|
||||
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<void> {
|
||||
@@ -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);
|
||||
|
||||
@@ -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<RoutstrdConfig> {
|
||||
try {
|
||||
if (existsSync(CONFIG_FILE)) {
|
||||
|
||||
Reference in New Issue
Block a user