UNPKG

@kya-os/cli

Version:

CLI for MCP-I setup and management

86 lines 3.31 kB
/** * Normalize KTA registration API response to standard format * Handles both new and legacy API response formats */ export function normalizeRegistrationResponse(response) { const legacyResponse = response; // Check if response is already in new format (but still need to validate keys below) const isNewFormat = response.agent && typeof response.agent === "object" && "id" in response.agent && "slug" in response.agent && "name" in response.agent && "url" in response.agent && "claimUrl" in response && "claimToken" in response; // Normalize agent object (use existing if already in new format) const agent = isNewFormat ? response.agent : { id: legacyResponse.agent?.id || legacyResponse.agentId || "", slug: legacyResponse.agent?.slug || legacyResponse.agentSlug || "", name: legacyResponse.agent?.name || legacyResponse.agentName || "", url: legacyResponse.agent?.url || legacyResponse.agentUrl || "", }; // ALWAYS normalize keys object (even for new format responses) // Only create keys object if both publicKey and privateKey are present // to avoid creating invalid identity configurations with empty strings let keys; if (legacyResponse.keys) { const publicKey = legacyResponse.keys.publicKey || legacyResponse.keys.public_key; const privateKey = legacyResponse.keys.privateKey || legacyResponse.keys.private_key; // Only create keys object if we have actual key values (not empty/undefined) if (publicKey && privateKey) { keys = { publicKey, privateKey, warning: legacyResponse.keys.warning, }; } } return { success: response.success, did: response.did, agent, claimUrl: isNewFormat ? response.claimUrl : legacyResponse.claimUrl || legacyResponse.claim_url || "", claimToken: isNewFormat ? response.claimToken : legacyResponse.claimToken || legacyResponse.claim_token || "", keys, receipt: response.receipt, responseTime: isNewFormat ? response.responseTime : legacyResponse.responseTime || legacyResponse.response_time || 0, error: response.error, }; } /** * Normalize KTA claim API response to standard format * Handles both new and legacy API response formats */ export function normalizeClaimResponse(response) { const legacyResponse = response; // If it's already in new format, return as-is if (response.agent && typeof response.agent === "object" && "id" in response.agent && "name" in response.agent && "url" in response.agent) { return response; } // Normalize agent object const agent = { id: legacyResponse.agent?.id || legacyResponse.agentId || "", name: legacyResponse.agent?.name || legacyResponse.agentName || "", url: legacyResponse.agent?.url || legacyResponse.agentUrl || "", }; return { success: response.success, did: response.did, agent, error: response.error, }; } //# sourceMappingURL=api-response.js.map