import { PriorityCollection } from './priority_collection' ; import { SavedObjectsClientContract } from '../../types' ; import { SavedObjectsRepositoryFactory } from '../../saved_objects_service' ; import { ISavedObjectTypeRegistry } from '../../saved_objects_type_registry' ; import { OpenSearchDashboardsRequest } from '../../../http' ; public export interface SavedObjectsClientWrapperOptions { client : SavedObjectsClientContract ; typeRegistry : ISavedObjectTypeRegistry ; request : OpenSearchDashboardsRequest ; } public export type SavedObjectsClientWrapperFactory = ( options : SavedObjectsClientWrapperOptions ) => SavedObjectsClientContract ; public export type SavedObjectsClientFactory = ( { request , includedHiddenTypes , } : { request : OpenSearchDashboardsRequest ; includedHiddenTypes ? : string [ ] ; } ) => SavedObjectsClientContract ; /**
 * Provider to invoke to retrieve a  SavedObjectsClientFactory .
 *  public export type SavedObjectsClientFactoryProvider = ( repositoryFactory : SavedObjectsRepositoryFactory ) => SavedObjectsClientFactory ; public export interface SavedObjectsClientProviderOptions { excludedWrappers ? : string [ ] ; includedHiddenTypes ? : string [ ] ; } internal export type ISavedObjectsClientProvider = Pick < SavedObjectsClientProvider , keyof SavedObjectsClientProvider > ; internal export class SavedObjectsClientProvider { private readonly _wrapperFactories = new PriorityCollection < { id : string ; factory : SavedObjectsClientWrapperFactory ; } > ( ) ; private _clientFactory : SavedObjectsClientFactory ; private readonly _originalClientFactory : SavedObjectsClientFactory ; private readonly _typeRegistry : ISavedObjectTypeRegistry ; constructor ( { defaultClientFactory , typeRegistry , } ) { this . _originalClientFactory = this . _clientFactory = defaultClientFactory ; this . _typeRegistry = typeRegistry ; } addClientWrapperFactory ( priority , id , factory ) { if ( this . _wrapperFactories . has ( ( entry ) => entry . id === id ) ) { throw new Error ( `wrapper factory with id ${ id } is already defined` ) ; } this . _wrapperFactories . add ( priority , { id , factory } ) ; } setClientFactory ( customClientFactory ) { if ( this . _clientFactory !== this . _originalClientFactory ) { throw new Error ( `custom client factory is already set, unable to replace the current one` ) ; } this . _clientFactory = customClientFactory ; } getClient ( request , { includedHiddenTypes , excludedWrappers = [ ] } = { } ) { const client = this . _clientFactory ( { request , includedHiddenTypes , } ) ; return this . _wrapperFactories . toPrioritizedArray ( ) . reduceRight ( ( clientToWrap , { id , factory } ) => { if ( excludedWrappers . includes ( id ) ) { return clientToWrap ; } return factory ( { request , client : clientToWrap , typeRegistry : this . _typeRegistry , } ) ; } , client ) ; } }