import * as t from 'io-ts' ; import { decodeRequestParams , parseEndpoint , routeValidationObject , } from '@kbn/server-route-repository' ; import { CoreSetup , CoreStart , Logger , RouteRegistrar } from 'kibana/server' ; import Boom from '@hapi/boom' ; import { errors } from '@elastic/elasticsearch' ; import { RuleDataPluginService } from '../../../rule_registry/server' ; import { ObservabilityRequestHandlerContext } from '../types' ; import { AbstractObservabilityServerRouteRepository } from './types' ; export function registerRoutes ( { repository , core , logger , ruleDataService , } ) { const routes = repository . getRoutes ( ) ; const router = core . setup . http . createRouter ( ) ; routes . forEach ( ( route ) => { const { endpoint , options , handler , params } = route ; const { pathname , method } = parseEndpoint ( endpoint ) ; ( router [ method ] as RouteRegistrar < typeof method , ObservabilityRequestHandlerContext > ) ( { path : pathname , validate : routeValidationObject , options , } , async ( context , request , response ) => { try { const decodedParams = decodeRequestParams ( { params : request . params , body : request . body , query : request . query , } , params ?? t . strict ( { } ) ) ; const data = ( await handler ( { context , request , core , logger , params : decodedParams , ruleDataService , } ) ) as any ; return response . ok ( { body : data } ) ; } catch ( error ) { logger . error ( error ) ; const opts = { statusCode : 500 , body : { message : error . message , } , } ; if ( Boom . isBoom ( error ) ) { opts . statusCode = error . output . statusCode ; } if ( error instanceof errors . RequestAbortedError ) { opts . statusCode = 499 ; opts . body . message = 'Client closed request' ; } return response . custom ( opts ) ; } } ) ; } ) ; }