import { Logger } from 'kibana/server' ; import type { ExtensionPoint , ExtensionPointCallbackArgument , ExtensionPointStorageClientInterface , ExtensionPointStorageInterface , NarrowExtensionPointToType , } from './types' ; import { ExtensionPointError } from './errors' ; export class ExtensionPointStorageClient implements ExtensionPointStorageClientInterface { constructor ( private readonly storage , private readonly logger ) { } param extensionType get < T extends ExtensionPoint [ 'type' ] > ( extensionType ) { return this . storage . get ( extensionType ) ; } param extensionType param initialCallbackInput param callbackResponseValidator async pipeRun < T extends ExtensionPoint [ 'type' ] , D extends NarrowExtensionPointToType < T > = NarrowExtensionPointToType < T > , P extends Parameters < D [ 'callback' ] > = Parameters < D [ 'callback' ] > > ( extensionType , initialCallbackInput , callbackResponseValidator ? ) { let inputArgument = initialCallbackInput ; const externalExtensions = this . get ( extensionType ) ; if ( ! externalExtensions || externalExtensions . size === 0 ) { return inputArgument ; } for ( const externalExtension of externalExtensions ) { const extensionRegistrationSource = this . storage . getExtensionRegistrationSource ( externalExtension ) ; try { inputArgument = await externalExtension . callback ( inputArgument as ExtensionPointCallbackArgument ) ; } catch ( error ) { this . logger ?. error ( new ExtensionPointError ( `Extension point execution error for ${ externalExtension . type }: ${ extensionRegistrationSource }` , error ) ) ; } if ( callbackResponseValidator ) { const validationError = callbackResponseValidator ( inputArgument ) ; if ( validationError ) { this . logger ?. error ( new ExtensionPointError ( `Extension point for ${ externalExtension . type } returned data that failed validation: ${ extensionRegistrationSource }` , { validationError , } ) ) ; throw validationError ; } } } return inputArgument ; } }