composeMiddleware<T extends TriggerContext<TriggerRequest, TriggerResponse>> function

Handler<T> composeMiddleware<T extends TriggerContext<TriggerRequest, TriggerResponse>>(
  1. List<Handler<T>> handlers,
  2. T converter(
    1. T ctx,
    2. Handler<T> next
    )
)

Implementation

Handler<T> composeMiddleware<T extends TriggerContext>(
        List<Handler<T>> handlers,
        T Function(T ctx, Handler<T> next) converter) =>
    (T ctx) async {
      final Handler<T> composedHandler = handlers.reversed
          .fold((ctx) async => ctx, (nextHandler, currHandler) {
        return (ctx) => currHandler(converter(ctx, nextHandler));
      });

      return await composedHandler(ctx);
    };