import * as io from 'socket.io-client' ; import { actions } from './actions' ; import { APIEndpoint , apiKeys } from '../../../server/src/api/api' ; import { SOCKET_PATH , socketTypes , SocketMessageError } from '../../../server/src/api/constants' ; import { AutoconnectConfig } from '../../../server/src/autoconnect/types' ; import { AutoforwardConfig } from '../../../server/src/autoforward/types' ; import { HostConfig } from '../../../server/src/host/types' ; import { ForwardingConfig } from '../../../server/src/forward/types' ; import { State as APIState } from '../../../server/src/types/redux' ; import { Store } from '../types/redux' ; const setup = ( socket , store ) => { socket . on ( 'connect' , ( ) => { console . log ( 'connected to socket server' ) ; store . dispatch ( actions . apiStatusConnected ( ) ) ; socket . emit ( socketTypes . register ) ; } ) ; socket . on ( 'disconnect' , ( ) => { console . log ( 'disconnected from socket server' ) ; store . dispatch ( actions . apiStatusDisconnected ( ) ) ; } ) ; socket . on ( socketTypes . state , ( state ) => { store . dispatch ( actions . apiStateChange ( state ) ) ; if ( state . system . info ) { document . title = `${ state . system . info . hostName } | SSHMon` ; } } ) ; } ; export class APIClient { store : Store ; socket : SocketIOClient . Socket ; constructor ( store ) { this . store = store ; this . socket = io ( { path : `${ window . location . pathname }${ SOCKET_PATH }` . replace ( /\/{2,}/g , '/' ) } ) ; setup ( this . socket , store ) ; } makeAPICall ( e ) { this . socket . emit ( socketTypes . apiCall , e , ( err , result ) => { if ( err ) { console . error ( `API call failure: ${ err . message }` , e ) ; return ; } console . log ( 'API call success:' , e , { result } ) ; } ) ; } hostCreate ( args ) { this . makeAPICall ( { key : apiKeys . hostCreate , args } ) ; } hostEdit ( args ) { this . makeAPICall ( { key : apiKeys . hostEdit , args } ) ; } hostDelete ( args ) { this . makeAPICall ( { key : apiKeys . hostDelete , args } ) ; } hostConnect ( args ) { this . makeAPICall ( { key : apiKeys . hostConnect , args } ) ; } hostDisconnect ( args ) { this . makeAPICall ( { key : apiKeys . hostDisconnect , args } ) ; } forwardingCreate ( args ) { this . makeAPICall ( { key : apiKeys . forwardingCreate , args } ) ; } forwardingEdit ( args ) { this . makeAPICall ( { key : apiKeys . forwardingEdit , args } ) ; } forwardingDelete ( args ) { this . makeAPICall ( { key : apiKeys . forwardingDelete , args } ) ; } forwardingConnect ( args ) { this . makeAPICall ( { key : apiKeys . forwardingConnect , args } ) ; } forwardingDisconnect ( args ) { this . makeAPICall ( { key : apiKeys . forwardingDisconnect , args } ) ; } }