import { RuntimeConfiguration } from '../skill/RuntimeConfiguration' ; import { createAskSdkError } from '../util/AskSdkUtils' ; import { ErrorHandler } from './error/handler/ErrorHandler' ; import { ErrorMapper } from './error/mapper/ErrorMapper' ; import { HandlerAdapter } from './request/handler/HandlerAdapter' ; import { RequestHandlerChain } from './request/handler/RequestHandlerChain' ; import { RequestInterceptor } from './request/interceptor/RequestInterceptor' ; import { ResponseInterceptor } from './request/interceptor/ResponseInterceptor' ; import { RequestMapper } from './request/mapper/RequestMapper' ; import { RequestDispatcher } from './RequestDispatcher' ; /**
 * Generic implementation of  RequestDispatcher .
 *  param Input param Output export class GenericRequestDispatcher < Input , Output > implements RequestDispatcher < Input , Output > { protected requestMappers : Array < RequestMapper < Input , Output > > ; protected errorMapper : ErrorMapper < Input , Output > ; protected handlerAdapters : Array < HandlerAdapter < Input , Output > > ; protected requestInterceptors : Array < RequestInterceptor < Input > > ; protected responseInterceptors : Array < ResponseInterceptor < Input , Output > > ; constructor ( options ) { this . requestMappers = options . requestMappers ; this . handlerAdapters = options . handlerAdapters ; this . errorMapper = options . errorMapper ; this . requestInterceptors = options . requestInterceptors ; this . responseInterceptors = options . responseInterceptors ; } param input public async dispatch ( input ) { let output ; try { if ( this . requestInterceptors ) { for ( const requestInterceptor of this . requestInterceptors ) { await requestInterceptor . process ( input ) ; } } output = await this . dispatchRequest ( input ) ; if ( this . responseInterceptors ) { for ( const responseInterceptor of this . responseInterceptors ) { await responseInterceptor . process ( input , output ) ; } } } catch ( err ) { output = await this . dispatchError ( input , err ) ; } return output ; } param input protected async dispatchRequest ( input ) { let handlerChain ; for ( const requestMapper of this . requestMappers ) { handlerChain = await requestMapper . getRequestHandlerChain ( input ) ; if ( handlerChain ) { break ; } } if ( ! handlerChain ) { throw createAskSdkError ( this . constructor . name , `Unable to find a suitable request handler.` ) ; } const handler = handlerChain . getRequestHandler ( ) ; const requestInterceptors = handlerChain . getRequestInterceptors ( ) ; const responseInterceptors = handlerChain . getResponseInterceptors ( ) ; let adapter ; for ( const handlerAdapter of this . handlerAdapters ) { if ( handlerAdapter . supports ( handler ) ) { adapter = handlerAdapter ; break ; } } if ( ! adapter ) { throw createAskSdkError ( this . constructor . name , `Unable to find a suitable handler adapter.` ) ; } if ( requestInterceptors ) { for ( const requestInterceptor of requestInterceptors ) { await requestInterceptor . process ( input ) ; } } const output = await adapter . execute ( input , handler ) ; if ( responseInterceptors ) { for ( const responseInterceptor of responseInterceptors ) { await responseInterceptor . process ( input , output ) ; } } return output ; } param input param error protected async dispatchError ( input , error ) { if ( this . errorMapper ) { const handler = await this . errorMapper . getErrorHandler ( input , error ) ; if ( handler ) { return handler . handle ( input , error ) ; } } throw error ; } }