import { schema } from '@kbn/config-schema' ; import { RequestHandlerContext , KibanaRequest , KibanaResponseFactory , IKibanaResponse , IRouter , } from 'kibana/server' ; export function initPlugin ( router , path ) { router . post ( { path , options : { authRequired : false , } , validate : { body : schema . object ( { dedup_key : schema . maybe ( schema . string ( ) ) , payload : schema . object ( { summary : schema . string ( ) , } , { unknowns : 'allow' , } ) , } , { unknowns : 'allow' , } ) , } , } , async function ( context , req , res ) : Promise < IKibanaResponse < any > > { const { body } = req ; const summary = body ?. payload ?. summary ; switch ( summary ) { case 'respond-with-429' : return jsonErrorResponse ( res , 429 , new Error ( summary ) ) ; case 'respond-with-502' : return jsonErrorResponse ( res , 502 , new Error ( summary ) ) ; case 'respond-with-418' : return jsonErrorResponse ( res , 418 , new Error ( summary ) ) ; } return jsonResponse ( res , 202 , { status : 'success' , message : 'Event processed' , ... ( body ?. dedup_key ? { dedup_key : body ?. dedup_key } : { } ) , } ) ; } ) ; } function jsonResponse ( res , code , object = { } ) { return res . custom < Record < string , unknown > > ( { body : object , statusCode : code } ) ; } function jsonErrorResponse ( res , code , object ) { return res . custom < Error > ( { body : object , statusCode : code } ) ; }