import { i18n } from '@kbn/i18n' ; import { KibanaRequest , SavedObjectsClientContract } from 'src/core/server' ; import { fromNullable , getOrElse } from 'fp-ts/lib/Option' ; import { pipe } from 'fp-ts/lib/pipeable' ; export type SavedObjectGetter = ( ... params : Parameters < SavedObjectsClientContract [ 'get' ] > ) => Promise < unknown > ; export type SavedObjectBulkGetter = ( ... params : Parameters < SavedObjectsClientContract [ 'bulkGet' ] > ) => Promise < unknown > ; export type SavedObjectBulkGetterResult = ( type : string , ids : string [ ] ) => Promise < unknown > ; export type SavedObjectProvider = ( request : KibanaRequest ) => SavedObjectBulkGetter ; export class SavedObjectProviderRegistry { private providers = new Map < string , SavedObjectProvider > ( ) ; private defaultProvider ? : SavedObjectProvider ; constructor ( ) { } public registerDefaultProvider ( provider ) { this . defaultProvider = provider ; } public registerProvider ( type , provider ) { if ( this . providers . has ( type ) ) { throw new Error ( `The Event Log has already registered a Provider for the Save Object type "${ type }".` ) ; } this . providers . set ( type , provider ) ; } public getProvidersClient ( request ) { if ( ! this . defaultProvider ) { throw new Error ( i18n . translate ( 'xpack.eventLog.savedObjectProviderRegistry.getProvidersClient.noDefaultProvider' , { defaultMessage : 'The Event Log requires a default Provider.' , } ) ) ; } const scopedProviders = new Map < string , SavedObjectBulkGetter > ( ) ; const defaultGetter = this . defaultProvider ( request ) ; return ( type , ids ) => { const objects = ids . map ( ( id ) => ( { type , id } ) ) ; const getter = pipe ( fromNullable ( scopedProviders . get ( type ) ) , getOrElse ( ( ) => { const client = this . providers . has ( type ) ? this . providers . get ( type ) ! ( request ) : defaultGetter ; scopedProviders . set ( type , client ) ; return client ; } ) ) ; return getter ( objects ) ; } ; } }