import { schema } from '@osd/config-schema' ; import { IRouter } from 'src/core/server' ; import { getRequestAbortedSignal } from '../../lib' ; import { SearchRouteDependencies } from '../search_service' ; import { shimHitsTotal } from './shim_hits_total' ; export function registerSearchRoute ( router , { getStartServices } ) { router . post ( { path : '/internal/search/{strategy}/{id?}' , validate : { params : schema . object ( { strategy : schema . string ( ) , id : schema . maybe ( schema . string ( ) ) , } ) , query : schema . object ( { } , { unknowns : 'allow' } ) , body : schema . object ( { } , { unknowns : 'allow' } ) , } , } , async ( context , request , res ) => { const searchRequest = request . body ; const { strategy , id } = request . params ; const abortSignal = getRequestAbortedSignal ( request . events . aborted$ ) ; const [ , , selfStart ] = await getStartServices ( ) ; try { const response = await selfStart . search . search ( context , { ... searchRequest , id } , { abortSignal , strategy , } ) ; return res . ok ( { body : { ... response , ... { rawResponse : shimHitsTotal ( response . rawResponse ) , } , } , } ) ; } catch ( err ) { return res . customError ( { statusCode : err . statusCode || 500 , body : { message : err . message , attributes : { error : err . body ?. error || err . message , } , } , } ) ; } } ) ; router . delete ( { path : '/internal/search/{strategy}/{id}' , validate : { params : schema . object ( { strategy : schema . string ( ) , id : schema . string ( ) , } ) , query : schema . object ( { } , { unknowns : 'allow' } ) , } , } , async ( context , request , res ) => { const { strategy , id } = request . params ; const [ , , selfStart ] = await getStartServices ( ) ; const searchStrategy = selfStart . search . getSearchStrategy ( strategy ) ; if ( ! searchStrategy . cancel ) return res . ok ( ) ; try { await searchStrategy . cancel ( context , id ) ; return res . ok ( ) ; } catch ( err ) { return res . customError ( { statusCode : err . statusCode , body : { message : err . message , attributes : { error : err . body . error , } , } , } ) ; } } ) ; }