import { SavedObject } from '../../types' ; import { SavedObjectsImportHook , SavedObjectsImportWarning } from '../types' ; export interface ExecuteImportHooksOptions { objects : SavedObject [ ] ; importHooks : Record < string , SavedObjectsImportHook [ ] > ; } export const executeImportHooks = async ( { objects , importHooks , } ) : Promise < SavedObjectsImportWarning [ ] > => { const objsByType = splitByType ( objects ) ; let warnings = [ ] ; for ( const [ type , typeObjs ] of Object . entries ( objsByType ) ) { const hooks = importHooks [ type ] ?? [ ] ; for ( const hook of hooks ) { const hookResult = await hook ( typeObjs ) ; if ( hookResult . warnings ) { warnings = [ ... warnings , ... hookResult . warnings ] ; } } } return warnings ; } ; const splitByType = ( objects ) : Record < string , SavedObject [ ] > => { return objects . reduce ( ( memo , obj ) => { memo [ obj . type ] = [ ... ( memo [ obj . type ] ?? [ ] ) , obj ] ; return memo ; } , { } as Record < string , SavedObject [ ] > ) ; } ;