toMiddleware method

Middleware toMiddleware({
  1. bool rear = false,
})

As the static to a spry middleware.

rear - Whether to add the handler to the rear of the middleware chain.

Implementation

Middleware toMiddleware({bool rear = false}) {
  return (Context context, Next next) async {
    if (rear) await next();

    try {
      final Response response = context.response;
      await call(context);
      response.close();
    } on SpryHttpException catch (e) {
      if (e.statusCode == HttpStatus.notFound && !rear) {
        return await next();
      }

      rethrow;
    }
  };
}