import { format } from 'url' ; import supertest from 'supertest' ; import request from 'superagent' ; import { parseEndpoint } from '../../../plugins/apm/common/apm_api/parse_endpoint' ; import type { APIReturnType , APIClientRequestParamsOf , } from '../../../plugins/apm/public/services/rest/createCallApmApi' ; import type { APIEndpoint } from '../../../plugins/apm/server' ; export function createApmApiClient ( st ) { return async < TEndpoint extends APIEndpoint > ( options ) : Promise < SupertestReturnType < TEndpoint > > => { const { endpoint } = options ; const params = 'params' in options ? ( options . params as Record < string , any > ) : { } ; const { method , pathname } = parseEndpoint ( endpoint , params ?. path ) ; const url = format ( { pathname , query : params ?. query } ) ; const res = params . body ? await st [ method ] ( url ) . send ( params . body ) . set ( 'kbn-xsrf' , 'foo' ) : await st [ method ] ( url ) . set ( 'kbn-xsrf' , 'foo' ) ; if ( res . status !== 200 ) { throw new ApmApiError ( res , endpoint ) ; } return res ; } ; } export type ApmApiSupertest = ReturnType < typeof createApmApiClient > ; export class ApmApiError extends Error { res : request . Response ; constructor ( res , endpoint ) { super ( `Unhandled ApmApiError.
Status: "${ res . status }"
Endpoint: "${ endpoint }"
Body: ${ JSON . stringify ( res . body ) }` ) ; this . res = res ; } } export interface SupertestReturnType < TEndpoint extends APIEndpoint > { status : number ; body : APIReturnType < TEndpoint > ; }