import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { unwrapEsResponse } from '../../../../../../observability/server' ; import { APMRouteHandlerResources } from '../../../../routes/typings' ; import { ESSearchResponse , ESSearchRequest , } from '../../../../../../../../src/core/types/elasticsearch' ; import { callAsyncWithDebug , getDebugBody , getDebugTitle , } from '../call_async_with_debug' ; import { cancelEsRequestOnAbort } from '../cancel_es_request_on_abort' ; export type APMIndexDocumentParams < T > = estypes . IndexRequest < T > ; export type APMInternalClient = ReturnType < typeof createInternalESClient > ; export function createInternalESClient ( { context , debug , request , } ) { const { asInternalUser } = context . core . elasticsearch . client ; function callEs < T extends { body : any ; } > ( operationName , { cb , requestType , params , } ) { return callAsyncWithDebug ( { cb : ( ) => { const controller = new AbortController ( ) ; return unwrapEsResponse ( cancelEsRequestOnAbort ( cb ( controller . signal ) , request , controller ) ) ; } , getDebugMessage : ( ) => ( { title : getDebugTitle ( request ) , body : getDebugBody ( { params , requestType , operationName } ) , } ) , debug , isCalledWithInternalUser : true , request , requestType , requestParams : params , operationName , } ) ; } return { search : async < TDocument = unknown , TSearchRequest extends ESSearchRequest = ESSearchRequest > ( operationName , params ) : Promise < ESSearchResponse < TDocument , TSearchRequest > > => { return callEs ( operationName , { requestType : 'search' , cb : ( signal ) => asInternalUser . search ( params , { signal } ) , params , } ) ; } , index : < T > ( operationName , params ) => { return callEs ( operationName , { requestType : 'index' , cb : ( signal ) => asInternalUser . index ( params , { signal } ) , params , } ) ; } , delete : ( operationName , params ) : Promise < { result : string ; } > => { return callEs ( operationName , { requestType : 'delete' , cb : ( signal ) => asInternalUser . delete ( params , { signal } ) , params , } ) ; } , indicesCreate : ( operationName , params ) => { return callEs ( operationName , { requestType : 'indices.create' , cb : ( signal ) => asInternalUser . indices . create ( params , { signal } ) , params , } ) ; } , } ; }