import Axios , { AxiosResponse , AxiosInstance } from 'axios' ; export interface MicrofabComponent { id : string ; display_name : string ; type : 'fabric-peer' | 'fabric-orderer' | 'fabric-ca' | 'gateway' | 'identity' ; } export interface MicrofabPeer extends MicrofabComponent { api_url : string ; api_options ? : Map < string , any > ; chaincode_url : string ; chaincode_options ? : Map < string , any > ; operations_url : string ; operations_options ? : Map < string , any > ; msp_id : string ; wallet : string ; identity : string ; } export function isPeer ( component ) { return component . type === 'fabric-peer' ; } export interface MicrofabCertificateAuthority extends MicrofabComponent { api_url : string ; api_options ? : Map < string , any > ; operations_url : string ; operations_options ? : Map < string , any > ; msp_id : string ; wallet : string ; identity : string ; } export function isCertificateAuthority ( component ) { return component . type === 'fabric-ca' ; } export interface MicrofabOrderer extends MicrofabComponent { api_url : string ; api_options ? : Map < string , any > ; operations_url : string ; operations_options ? : Map < string , any > ; msp_id : string ; wallet : string ; identity : string ; } export function isOrderer ( component ) { return component . type === 'fabric-orderer' ; } export interface MicrofabGateway extends MicrofabComponent { wallet : string ; [ propName ] : any ; } export function isGateway ( component ) { return component . type === 'gateway' ; } export interface MicrofabIdentity extends MicrofabComponent { cert : string ; private_key : string ; msp_id : string ; wallet : string ; hide : boolean ; } export function isIdentity ( component ) { return component . type === 'identity' ; } export class MicrofabClient { public url : string ; private axios : AxiosInstance ; constructor ( url ) { this . url = url ; this . axios = Axios . create ( { timeout : 30000 } ) ; } public async isAlive ( ) { const url = `${ this . url }/ak/api/v1/health` ; const request = this . axios . get ( url . toString ( ) ) . then ( ( ) => { return true ; } ) . catch ( ( ) => { return false ; } ) ; const timeout = new Promise ( ( resolve ) => { setTimeout ( ( ) => { resolve ( false ) ; } , 30000 ) ; } ) ; return Promise . race ( [ request , timeout ] ) ; } public async getComponents ( ) { const url = `${ this . url }/ak/api/v1/components` ; const response = await this . axios . get ( url . toString ( ) ) ; return response . data as MicrofabComponent [ ] ; } public async getComponent ( id ) { const url = `${ this . url }/ak/api/v1/components/${ id }` ; const response = await this . axios . get ( url . toString ( ) ) ; return response . data as MicrofabComponent ; } }