11/* eslint-disable no-console */
2+ import { IterableClient } from "@iterable/api" ;
23import chalk from "chalk" ;
34import inquirer from "inquirer" ;
45
6+ import { loadCliConfig } from "./config.js" ;
57import type { ApiKeyMetadata , KeyManager } from "./key-manager.js" ;
68import { getKeyManager } from "./key-manager.js" ;
79import { getSpinner , isTestEnv } from "./utils/cli-env.js" ;
@@ -23,7 +25,6 @@ import {
2325
2426function displayKeyDetails ( meta : ApiKeyMetadata ) : void {
2527 const w = 12 ;
26- console . log ( ) ;
2728 console . log ( ` ${ "Name:" . padEnd ( w ) } ${ chalk . white . bold ( meta . name ) } ` ) ;
2829 console . log ( ` ${ "ID:" . padEnd ( w ) } ${ chalk . gray ( meta . id ) } ` ) ;
2930 console . log ( ` ${ "Endpoint:" . padEnd ( w ) } ${ linkColor ( ) ( meta . baseUrl ) } ` ) ;
@@ -474,6 +475,53 @@ export async function handleKeysCommand(args: string[]): Promise<void> {
474475 break ;
475476 }
476477
478+ case "validate" : {
479+ spinner . start ( "Validating API connection..." ) ;
480+ try {
481+ const config = await loadCliConfig ( ) ;
482+ const client = new IterableClient ( {
483+ apiKey : config . apiKey ,
484+ baseUrl : config . baseUrl ,
485+ } ) ;
486+ try {
487+ await client . getUserFields ( ) ;
488+ spinner . succeed ( "API connection successful" ) ;
489+ const w = 12 ;
490+ if ( process . env . ITERABLE_API_KEY ) {
491+ console . log (
492+ ` ${ "Source:" . padEnd ( w ) } ${ chalk . white ( "ITERABLE_API_KEY environment variable" ) } `
493+ ) ;
494+ } else {
495+ const activeMeta = await keyManager . getActiveKeyMetadata ( ) ;
496+ if ( activeMeta ) {
497+ console . log (
498+ ` ${ "Key:" . padEnd ( w ) } ${ chalk . white . bold ( activeMeta . name ) } `
499+ ) ;
500+ }
501+ }
502+ const endpoint = config . baseUrl . replace ( "https://" , "" ) ;
503+ console . log ( ` ${ "Endpoint:" . padEnd ( w ) } ${ linkColor ( ) ( endpoint ) } ` ) ;
504+ } finally {
505+ client . destroy ( ) ;
506+ }
507+ } catch ( error : unknown ) {
508+ spinner . fail ( "API connection failed" ) ;
509+ if ( process . env . ITERABLE_API_KEY ) {
510+ showInfo ( "Source: ITERABLE_API_KEY environment variable" ) ;
511+ } else {
512+ const activeMeta = await keyManager
513+ . getActiveKeyMetadata ( )
514+ . catch ( ( ) => null ) ;
515+ if ( activeMeta ) {
516+ showInfo ( `Key: ${ activeMeta . name } ` ) ;
517+ }
518+ }
519+ showError ( error instanceof Error ? error . message : "Unknown error" ) ;
520+ process . exit ( 1 ) ;
521+ }
522+ break ;
523+ }
524+
477525 default : {
478526 showSection ( "Available Commands" , icons . key ) ;
479527 console . log ( ) ;
0 commit comments