import _ from 'lodash' ; import { SavedObjectsClientContract } from 'opensearch-dashboards/public' ; import { SavedObject , SavedObjectConfig } from '../../types' ; /**
 * Initialize saved object
 */ export async function intializeSavedObject ( savedObject , savedObjectsClient , config ) { const opensearchType = config . type ; if ( ! opensearchType ) throw new Error ( 'You must define a type name to use SavedObject objects.' ) ; if ( ! savedObject . id ) { _ . assign ( savedObject , savedObject . defaults ) ; await savedObject . hydrateIndexPattern ! ( ) ; if ( typeof config . afterOpenSearchResp === 'function' ) { savedObject = await config . afterOpenSearchResp ( savedObject ) ; } return savedObject ; } const resp = await savedObjectsClient . get ( opensearchType , savedObject . id ) ; const respMapped = { _id : resp . id , _type : resp . type , _source : _ . cloneDeep ( resp . attributes ) , references : resp . references , found : ! ! resp . _version , } ; await savedObject . applyOpenSearchResp ( respMapped ) ; if ( typeof config . init === 'function' ) { await config . init . call ( savedObject ) ; } return savedObject ; }