import * as t from 'io-ts' ; import { schema } from '@kbn/config-schema' ; import { CoreSetup , RequestHandler , Logger } from 'kibana/server' ; import { PathReporter } from 'io-ts/lib/PathReporter' ; import { isLeft } from 'fp-ts/lib/Either' ; import { getAnnotationByIdRt , createAnnotationRt , deleteAnnotationRt , } from '../../../common/annotations' ; import { ScopedAnnotationsClient } from './bootstrap_annotations' ; import { createAnnotationsClient } from './create_annotations_client' ; import type { ObservabilityRequestHandlerContext } from '../../types' ; const unknowns = schema . object ( { } , { unknowns : 'allow' } ) ; export function registerAnnotationAPIs ( { core , index , logger , } ) { function wrapRouteHandler < TType extends t . Type < any > > ( types , handler ) { return async ( ... args ) => { const [ context , request , response ] = args ; const rt = types ; const data = { body : request . body , query : request . query , params : request . params , } ; const validation = rt . decode ( data ) ; if ( isLeft ( validation ) ) { return response . badRequest ( { body : PathReporter . report ( validation ) . join ( ', ' ) , } ) ; } const esClient = context . core . elasticsearch . client . asCurrentUser ; const client = createAnnotationsClient ( { index , esClient , logger , license : context . licensing ?. license , } ) ; try { const res = await handler ( { data : validation . right , client , } ) ; return response . ok ( { body : res , } ) ; } catch ( err ) { return response . custom ( { statusCode : err . output ?. statusCode ?? 500 , body : { message : err . output ?. payload ?. message ?? 'An internal server error occured' , } , } ) ; } } ; } const router = core . http . createRouter < ObservabilityRequestHandlerContext > ( ) ; router . post ( { path : '/api/observability/annotation' , validate : { body : unknowns , } , } , wrapRouteHandler ( t . type ( { body : createAnnotationRt } ) , ( { data , client } ) => { return client . create ( data . body ) ; } ) ) ; router . delete ( { path : '/api/observability/annotation/{id}' , validate : { params : unknowns , } , } , wrapRouteHandler ( t . type ( { params : deleteAnnotationRt } ) , ( { data , client } ) => { return client . delete ( data . params ) ; } ) ) ; router . get ( { path : '/api/observability/annotation/{id}' , validate : { params : unknowns , } , } , wrapRouteHandler ( t . type ( { params : getAnnotationByIdRt } ) , ( { data , client } ) => { return client . getById ( data . params ) ; } ) ) ; }