@@ -3,22 +3,21 @@ import { colors } from '../utils/colors';
33import { createSpinner } from '../utils/spinner' ;
44import { logger } from '../utils/logger' ;
55import { config } from '../utils/config' ;
6- import { mongoApiClient } from '../services/mongoApi ' ;
6+ import { ApiClient } from '../services/api ' ;
77
88/**
99 * Ping command to test API connectivity
1010 */
1111export const pingCommand = new Command ( 'ping' )
12- . description ( 'Test connectivity to vulnerability analysis APIs ' )
12+ . description ( 'Test connectivity to vulnerability analysis API ' )
1313 . option ( '--verbose' , 'show detailed connection information' )
1414 . action ( async ( options ) => {
1515 console . log ( colors . title ( '🏓 Vulnify API Connectivity Test' ) ) ;
1616 console . log ( '' ) ;
1717
1818 if ( options . verbose ) {
1919 console . log ( colors . muted ( 'Configuration:' ) ) ;
20- console . log ( colors . muted ( ` MongoDB API URL: ${ config . getMongoApiUrl ( ) || 'not set' } ` ) ) ;
21- console . log ( colors . muted ( ` Fallback API URL: ${ config . getApiUrl ( ) } ` ) ) ;
20+ console . log ( colors . muted ( ` API URL: ${ config . getApiUrl ( ) } ` ) ) ;
2221 console . log ( colors . muted ( ` Timeout: ${ config . getTimeout ( ) } ms` ) ) ;
2322 console . log ( '' ) ;
2423 }
@@ -27,72 +26,72 @@ export const pingCommand = new Command('ping')
2726 spinner . start ( ) ;
2827
2928 try {
30- // Test connectivity to both APIs
31- const connectivity = await mongoApiClient . testConnectivity ( ) ;
29+ // Test connectivity to API - simple HTTP check
30+ const apiClient = new ApiClient ( ) ;
31+ const startTime = Date . now ( ) ;
32+
33+ // Try to make a simple request to test connectivity
34+ // We'll catch the error but if we get a response (even an error), it means the API is reachable
35+ try {
36+ await apiClient . analyze ( {
37+ ecosystem : 'npm' ,
38+ dependencies : [ ]
39+ } ) ;
40+ } catch ( error ) {
41+ // If we get a validation error, it means the API is reachable
42+ if ( error instanceof Error && (
43+ error . message . includes ( 'dependencies' ) ||
44+ error . message . includes ( 'At least one dependency is required' )
45+ ) ) {
46+ // This is expected - empty dependencies array causes validation error
47+ // but it means the API is responding
48+ } else {
49+ throw error ;
50+ }
51+ }
52+
53+ const responseTime = Date . now ( ) - startTime ;
3254
3355 spinner . stop ( ) ;
3456 console . log ( '' ) ;
3557
36- // Display MongoDB API results
37- console . log ( colors . info ( '📡 MongoDB API Service:' ) ) ;
38- if ( connectivity . mongodb . available ) {
39- console . log ( colors . success ( ` ✅ Available (${ connectivity . mongodb . responseTime } ms)` ) ) ;
40- } else {
41- console . log ( colors . error ( ` ❌ Unavailable: ${ connectivity . mongodb . error } ` ) ) ;
42- }
58+ // Display API results
59+ console . log ( colors . info ( '📡 API Service:' ) ) ;
60+ console . log ( colors . success ( ` ✅ Available (${ responseTime } ms)` ) ) ;
4361
44- // Display Fallback API results
62+ // Overall status
4563 console . log ( '' ) ;
46- console . log ( colors . info ( '🔄 Fallback API Service:' ) ) ;
47- if ( connectivity . fallback . available ) {
48- console . log ( colors . success ( ` ✅ Available (${ connectivity . fallback . responseTime } ms)` ) ) ;
49- } else {
50- console . log ( colors . error ( ` ❌ Unavailable: ${ connectivity . fallback . error || 'Not configured' } ` ) ) ;
51- }
64+ console . log ( colors . success ( '🎉 API service is available!' ) ) ;
65+ console . log ( colors . info ( '💡 Ready to analyze dependencies for vulnerabilities' ) ) ;
5266
53- // Overall status
5467 console . log ( '' ) ;
55- if ( connectivity . mongodb . available || connectivity . fallback . available ) {
56- console . log ( colors . success ( '🎉 At least one API service is available!' ) ) ;
57-
58- if ( connectivity . mongodb . available ) {
59- console . log ( colors . info ( '💡 MongoDB API will be used for faster analysis' ) ) ;
60- } else {
61- console . log ( colors . warning ( '⚠️ Using fallback API (may be slower)' ) ) ;
62- }
68+ console . log ( colors . muted ( 'Use "vulnify test" to start analyzing your project' ) ) ;
69+
70+ } catch ( error ) {
71+ spinner . fail ( '❌ Connectivity test failed' ) ;
72+
73+ console . log ( '' ) ;
74+ console . log ( colors . error ( 'Error details:' ) ) ;
75+ if ( error instanceof Error ) {
76+ console . log ( colors . error ( ` ${ error . message } ` ) ) ;
6377 } else {
64- console . log ( colors . error ( '❌ No API services are available' ) ) ;
65- console . log ( '' ) ;
66- console . log ( colors . warning ( '💡 Troubleshooting tips:' ) ) ;
67- console . log ( ' • Check your internet connection' ) ;
68- console . log ( ' • Verify API URLs are correct' ) ;
69- console . log ( ' • Check if services are running' ) ;
70- console . log ( ' • Try increasing timeout with --timeout option' ) ;
71- }
72-
73- // Show configuration help
74- if ( ! connectivity . mongodb . available && ! connectivity . fallback . available ) {
75- console . log ( '' ) ;
76- console . log ( colors . info ( '🔧 Configuration:' ) ) ;
77- console . log ( ' Set MongoDB API URL:' ) ;
78- console . log ( colors . muted ( ' export VULNIFY_MONGO_API_URL="https://your-lambda-url"' ) ) ;
79- console . log ( ' Set fallback API URL:' ) ;
80- console . log ( colors . muted ( ' export VULNIFY_API_URL="https://api.vulnify.io"' ) ) ;
78+ console . log ( colors . error ( ' Unknown error occurred' ) ) ;
8179 }
8280
83- } catch ( error ) {
84- spinner . stop ( ) ;
8581 console . log ( '' ) ;
86- console . log ( colors . error ( '❌ Connectivity test failed' ) ) ;
87- console . log ( colors . error ( `Error: ${ error instanceof Error ? error . message : 'Unknown error' } ` ) ) ;
88-
82+ console . log ( colors . warning ( '💡 Troubleshooting:' ) ) ;
83+ console . log ( ' • Check your internet connection' ) ;
84+ console . log ( ' • Verify the API endpoint is accessible' ) ;
85+ console . log ( ' • Try again in a few moments' ) ;
86+ console . log ( ' • Use --verbose for more details' ) ;
87+
8988 if ( options . verbose ) {
9089 logger . error ( 'Ping command failed' , {
9190 error : error instanceof Error ? error . message : 'Unknown error' ,
9291 stack : error instanceof Error ? error . stack : undefined
9392 } ) ;
9493 }
95-
94+
9695 process . exit ( 1 ) ;
9796 }
9897 } ) ;
0 commit comments