import { reduce , map } from 'rxjs/operators' ; import { schema } from '@kbn/config-schema' ; import { GlobalSearchRouter } from '../types' ; import { GlobalSearchFindError } from '../../common/errors' ; export const registerInternalFindRoute = ( router ) => { router . post ( { path : '/internal/global_search/find' , validate : { body : schema . object ( { params : schema . object ( { term : schema . maybe ( schema . string ( ) ) , types : schema . maybe ( schema . arrayOf ( schema . string ( ) ) ) , tags : schema . maybe ( schema . arrayOf ( schema . string ( ) ) ) , } ) , options : schema . maybe ( schema . object ( { preference : schema . maybe ( schema . string ( ) ) , } ) ) , } ) , } , } , async ( ctx , req , res ) => { const { params , options } = req . body ; try { const allResults = await ctx . globalSearch ! . find ( params , { ... options , aborted$ : req . events . aborted$ } ) . pipe ( map ( ( batch ) => batch . results ) , reduce ( ( acc , results ) => [ ... acc , ... results ] ) ) . toPromise ( ) ; return res . ok ( { body : { results : allResults , } , } ) ; } catch ( e ) { if ( e instanceof GlobalSearchFindError && e . type === 'invalid-license' ) { return res . forbidden ( { body : e . message } ) ; } throw e ; } } ) ; } ;