import { Middleware } from './types' ; export interface PreEffectFnParams < TEvent , TContext > { event : TEvent ; context : TContext ; } /**
 * An interface for the parameter of a post effect function as defined in
 * the interface
 *  CreateEffectMiddlewareParams member member member export interface PostEffectFnParams < TEvent , TContext , TResult > { event : TEvent ; context : TContext ; result : TResult ; } /**
 * An interface for the lone parameter of the
 *  createEffectMiddleware middleware helper
 * function.
 *
 *  member member post - An async lambda function which takes
 * `PostEffectFnParams<TEvent, TContext, TResult>`  PostEffectFnParams * as its lone parameter and Promises it does something as a side effect with
 * no return after the next handler in the middleware chain has been called
 * but before this middleware has returned.
  export interface CreateEffectMiddlewareParams < TEvent , TContext , TResult > { pre ? : ( params : PreEffectFnParams < TEvent , TContext > ) => Promise < void > ; post ? : ( params : PostEffectFnParams < TEvent , TContext , TResult > ) => Promise < void > ; } param params - parameters of type
 * `CreateEffectMiddlewareParams<TEvent, TResult>`
 *  CreateEffectMiddlewareParams which define the pre and post handler
 * effects to perform. The members of params are not required and if they are
 * undefined, they each default to `async () => {}`
  export function createEffectMiddleware < TEvent , TContext , TResult , TNextEvent = TEvent , TNextContext = TContext , TNextResult = TResult > ( { pre = async ( ) => { } , post = async ( ) => { } , } ) { return ( next ) => async ( event , context ) => { await pre ( { event , context } ) ; const result = await next ( ( event as unknown ) as TNextEvent , ( context as unknown ) as TNextContext ) ; await post ( { event , context , result } ) ; return ( result as unknown ) as TResult ; } ; } /**
 * An interface for the parameter of a pre mapping function as defined in
 * the type  PreMappingFn member member export interface PreMappingFnParams < TEvent , TContext > { event : TEvent ; context : TContext ; } /**
 * An interface for the return type of a pre mapping function as defined in
 * the type  PreMappingFn member member export interface PreMappingFnResult < TEvent , TContext > { event : TEvent ; context : TContext ; } /**
 * A type for pre mapping functions.
 * A pre mapping function is executed before the next middleware and is designed to
 * map incoming values to new values that are expected by the next middleware.
 * It is used by the interface
 *  CreateMappingMiddlewareParams param params - the pre mapping parameters as defined in the interface
 *  PreMappingFnParams returns a promise of the mapped parameters as defined in
 *  PreMappingFnResult export type PreMappingFn < TEventFrom , TContextFrom , TEventTo , TContextTo > = ( params : PreMappingFnParams < TEventFrom , TContextFrom > ) => Promise < PreMappingFnResult < TEventTo , TContextTo > > ; /**
 * An interface for the parameter of a post mapping function as defined in
 * the interface
 *  CreateMappingMiddlewareParams member member member export interface PostMappingFnParams < TEventFrom , TContextFrom , TResultFrom , TEventTo , TContextTo > { event : TEventFrom ; mappedEvent : TEventTo ; context : TContextFrom ; mappedContext : TContextTo ; result : TResultFrom ; } /**
 * A type for post mapping functions
 * A post mapping function is executed after the next middleware and is designed to
 * map result values to new values that are expected higher in the middleware chain.
 * It is used by the interface
 *  CreateMappingMiddlewareParams param params - the post mapping parameters as defined in the interface
 *  PostMappingFnParams returns a promise of the mapped parameters as defined in
 *  PostMappingFnResult export type PostMappingFn < TEventFrom , TContextFrom , TResultFrom , TEventTo , TContextTo , TResultTo > = ( params : PostMappingFnParams < TEventFrom , TContextFrom , TResultFrom , TEventTo , TContextTo > ) => Promise < TResultTo > ; /**
 * An interface for the lone parameter of the
 *  createMappingMiddleware middleware helper
 * function.
 *
 *  member member post - An async lambda function which takes
 * `PostMappingFnParams<TEventFrom, TResultFrom, TEventTo>`  PostMappingFnParams * as its lone parameter and Promises to map the value into `TResultTo`
  export interface CreateMappingMiddlewareParams < TEventFrom , TContextFrom , TResultFrom , TEventTo , TContextTo , TResultTo > { pre : PreMappingFn < TEventFrom , TContextFrom , TEventTo , TContextTo > ; post : PostMappingFn < TEventFrom , TContextFrom , TResultFrom , TEventTo , TContextTo , TResultTo > ; } param params - parameters of type
 * `CreateMappingMiddlewareParams<TEventFrom, TResultFrom, TEventTo, TResultTo>`
 *  CreateMappingMiddlewareParams which define the pre and post handler
 * mapping to perform. The members of params are required, but can be bypassed by
 * setting the from to the same type as the to and using `(from) => from` as a
 * mapping function.
  export function createMappingMiddleware < TEventFrom , TContextFrom , TResultFrom , TEventTo , TContextTo , TResultTo > ( { pre , post , } ) { return ( next ) => async ( event , context ) => { const { event : mappedEvent , context : mappedContext } = await pre ( { event , context , } ) ; const result = await next ( mappedEvent , mappedContext ) ; const mappedResult = await post ( { event , mappedEvent , context , mappedContext , result , } ) ; return mappedResult ; } ; } export function createEventMappingMiddleware < TEventFrom , TContextFrom , TEventTo , TContextTo , TResult > ( preMappingFn ) { return createMappingMiddleware ( { pre : preMappingFn , post : postMapIdentity } ) ; } export function createResultMappingMiddleware < TEvent , TContext , TResultFrom , TResultTo > ( postMappingFn ) { return createMappingMiddleware ( { pre : preMapIdentity , post : postMappingFn } ) ; } export async function preMapIdentity < TEvent , TContext > ( { event , context , } ) { return { event , context } ; } export async function postMapIdentity < TResult > ( { result , } ) { return result ; } param recoveryFn export function createRecoveryMiddleware < TEvent , TContext , TResult > ( recoveryFn ) { return ( next ) => async ( event , context ) => { let response ; try { response = await next ( event , context ) ; } catch ( error ) { response = await recoveryFn ( error , event , context ) ; } return response ; } ; }