import { Lifecycle , Request , ResponseToolkit as HapiResponseToolkit } from '@hapi/hapi' ; import { Logger } from '../../logging' ; import { HapiResponseAdapter , OpenSearchDashboardsRequest , OpenSearchDashboardsResponse , lifecycleResponseFactory , LifecycleResponseFactory , } from '../router' ; enum ResultType { next = 'next' } interface Next { type : ResultType . next ; } type OnPostAuthResult = Next ; const postAuthResult = { next ( ) { return { type : ResultType . next } ; } , isNext ( result ) { return result && result . type === ResultType . next ; } , } ; public export interface OnPostAuthToolkit { /** To pass request to the next handler */ next : ( ) => OnPostAuthResult ; } /**
 * See  OnPostAuthToolkit .
 *  public export type OnPostAuthHandler = ( request : OpenSearchDashboardsRequest , response : LifecycleResponseFactory , toolkit : OnPostAuthToolkit ) => OnPostAuthResult | OpenSearchDashboardsResponse | Promise < OnPostAuthResult | OpenSearchDashboardsResponse > ; const toolkit = { next : postAuthResult . next , } ; public param fn export function adoptToHapiOnPostAuthFormat ( fn , log ) { return async function interceptRequest ( request , responseToolkit ) : Promise < Lifecycle . ReturnValue > { const hapiResponseAdapter = new HapiResponseAdapter ( responseToolkit ) ; try { const result = await fn ( OpenSearchDashboardsRequest . from ( request ) , lifecycleResponseFactory , toolkit ) ; if ( result instanceof OpenSearchDashboardsResponse ) { return hapiResponseAdapter . handle ( result ) ; } if ( postAuthResult . isNext ( result ) ) { return responseToolkit . continue ; } throw new Error ( `Unexpected result from OnPostAuth. Expected OnPostAuthResult or OpenSearchDashboardsResponse, but given: ${ result }.` ) ; } catch ( error ) { log . error ( error ) ; return hapiResponseAdapter . toInternalError ( ) ; } } ; }