import { inject, injectable } from "@codemation/core"; import type { Hono } from "hono"; import type { Logger, LoggerFactory } from "../application/logging/Logger"; import { ApplicationTokens } from "../applicationTokens"; import { ApplicationRequestError } from "../application/ApplicationRequestError"; import type { QueryBus } from "../application/bus/QueryBus"; import { GetCredentialMaterialQuery } from "../application/queries/GetCredentialMaterialQuery"; import { InternalHmacAuthMiddleware } from "../pairing/InternalHmacAuthMiddleware"; import type { InternalHonoApiRouteRegistrar } from "../presentation/http/hono/InternalHonoApiRouteRegistrar"; @injectable() export class InternalCredentialMaterialRegistrar implements InternalHonoApiRouteRegistrar { private readonly logger: Logger; constructor( @inject(InternalHmacAuthMiddleware) private readonly hmacMiddleware: InternalHmacAuthMiddleware, @inject(ApplicationTokens.QueryBus) private readonly queryBus: QueryBus, @inject(ApplicationTokens.LoggerFactory) loggerFactory: LoggerFactory, ) { this.logger = loggerFactory.create("InternalCredentialMaterialRegistrar"); } register(app: Hono): void { app.get("/internal/credentials/material", this.hmacMiddleware.handle(), async (c) => { const name = c.req.query("name"); if (!name || typeof name !== "string" || name.trim().length === 0) { return c.json({ error: "name query parameter is required" }, 400); } try { const result = await this.queryBus.execute(new GetCredentialMaterialQuery(name)); this.logger.info(`Credential material resolved for instance ${result.instanceId} (name: ${name})`); return c.json(result); } catch (error) { if (error instanceof ApplicationRequestError) { return c.json({ error: error.message }, error.status as 400 | 404 | 409); } this.logger.error("Credential material resolution failed", error instanceof Error ? error : undefined); return c.json({ error: "Internal server error" }, 500); } }); } }