import { isNullOrUndefined , isObject } from 'util' ; import { InstanceOptions , RequestTracingConfig } from '../../HttpClient' ; import { IOContext } from '../../service/worker/runtime/typings' ; import { InfraClient } from './InfraClient' ; const ANY_APP = '' ; const eventRoute = ( route ) => `/events/${ route }` ; const isResourceVRN = ( appIdOrResource ) : appIdOrResource is ResourceVRN => isObject ( appIdOrResource ) && ! isNullOrUndefined ( ( appIdOrResource as ResourceVRN ) . service ) ; export class Events extends InfraClient { constructor ( { recorder , ... context } , options ? ) { super ( 'courier@0.x' , context , options ) ; } public sendEvent = ( appIdOrResource , route , message ? , tracingConfig ? ) => { const resource = this . resourceFor ( appIdOrResource ) ; return this . http . put ( eventRoute ( route ) , message , { metric : 'events-send' , params : { resource } , tracing : { requestSpanNameSuffix : 'events-send' , ... tracingConfig ?. tracing , } , } ) ; } ; private resourceFor = ( appIdOrResource ) => { if ( appIdOrResource === ANY_APP ) { return ANY_APP ; } const { service , path } = isResourceVRN ( appIdOrResource ) ? appIdOrResource : { service : 'apps' , path : `/apps/${ appIdOrResource }` } ; return `vrn:${ service }:${ this . context . region }:${ this . context . account }:${ this . context . workspace }:${ path }` ; } ; } export interface ResourceVRN { service : string ; path : string ; }