withoutMiddleware<T extends Object> method

Route withoutMiddleware<T extends Object>()

do not run the middleware of type T on the route added last. It takes out every middleware of type T or a subtype of T on that route. That includes global, router and group middleware, the middleware in the route's own list, and a .timeout() added before. The controller stays. T is required: with no type argument (or Object or dynamic) it would drop everything, auth and CSRF too, so it throws an ArgumentError. So withoutMiddleware<IDoxNextMiddleware>() takes out every next-style middleware. Use it for a route that a global middleware does not fit, like a long stream and RequestTimeout. The OpenAPI docs of the route follow it.

Route.post('events', controller).withoutMiddleware<RequestTimeout>();

Implementation

Route withoutMiddleware<T extends Object>() {
  if (T == Object) {
    throw ArgumentError(
      'Route.withoutMiddleware() needs a type, like '
      'withoutMiddleware<RequestTimeout>(). Object would drop every '
      'middleware, auth and CSRF too.',
    );
  }
  RouteData? last = _last;
  if (last == null) {
    throw StateError('Route.withoutMiddleware(): there is no route.');
  }
  for (RouteData data in _lastGroup ?? <RouteData>[last]) {
    List<dynamic> controllers = data.controllers as List<dynamic>;
    Object? action = controllers.isEmpty ? null : controllers.last;

    /// the controller stays, even when it is a [T]
    bool drop(dynamic item) => item is T && !identical(item, action);
    controllers.removeWhere(drop);

    /// the OpenAPI docs read `preMiddleware` too (it is a read-only copy)
    data.preMiddleware = List<dynamic>.unmodifiable(
      data.preMiddleware.where((dynamic item) => !drop(item)),
    );
  }
  return this;
}