MiddlewarePipeline class
Enhanced middleware pipeline with priority-based execution and better error handling.
This class manages a collection of middleware handlers that process HTTP requests in a specific order based on their priority levels. It supports:
- Priority-based execution (global -> routing -> auth -> preprocessing -> business -> terminating)
- Named middleware for easy reference and ordering
- Error handling with terminating middleware
- Dynamic addition/removal of middleware
- Before/after insertion relative to named middleware
Example usage:
final pipeline = MiddlewarePipeline();
// Add middleware with different priorities
pipeline.add((req, res, next) async {
print('Global middleware');
await next();
}, priority: MiddlewarePriority.global, name: 'logger');
pipeline.add((req, res, next) async {
print('Auth middleware');
await next();
}, priority: MiddlewarePriority.auth, name: 'auth');
// Process request
await pipeline.process(request, response);
Constructors
Properties
- count → int
-
Gets the count of middleware in the pipeline.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- isEmpty → bool
-
Checks if the pipeline is empty.
no setter
- isNotEmpty → bool
-
Checks if the pipeline has any middleware.
no setter
-
middleware
→ List<
Middleware> -
Gets all middleware in the pipeline.
no setter
-
middlewareNames
→ List<
String> -
Gets all middleware names.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
add(
MiddlewareHandler handler, {MiddlewarePriority priority = MiddlewarePriority.business, String? name}) → void - Adds a middleware handler function to the pipeline.
-
addAfter(
String targetName, MiddlewareHandler handler, {String? name}) → void - Adds a middleware after a specific named middleware.
-
addAll(
List< MiddlewareHandler> handlers, {MiddlewarePriority priority = MiddlewarePriority.business}) → void - Adds multiple middleware handlers to the pipeline.
-
addBefore(
String targetName, MiddlewareHandler handler, {String? name}) → void - Adds a middleware before a specific named middleware.
-
addConditional(
MiddlewareHandler handler, {required bool condition(Request request), MiddlewarePriority priority = MiddlewarePriority.business, String? name}) → void - Executes middleware conditionally based on a predicate.
-
addMiddleware(
Middleware middleware) → void - Adds a full middleware object to the pipeline.
-
addMiddlewares(
List< Middleware> middlewares) → void - Adds multiple middleware objects to the pipeline.
-
clear(
) → void - Clears all middleware from the pipeline.
-
getByName(
String name) → Middleware? - Gets a middleware by name.
-
getGroup(
String name) → List< Middleware> - Gets a registered group of middleware.
-
getMiddlewareByPriority(
MiddlewarePriority priority) → List< Middleware> - Gets middleware by priority level.
-
group(
String name, List< Middleware> middlewares) → void - Registers a group of middleware.
-
hasMiddleware(
String name) → bool - Checks if a named middleware exists.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
process(
Request request, ResponseContract response) → Future< void> - Processes the request through the middleware pipeline.
-
remove(
String name) → void - Removes a middleware by name.
-
toString(
) → String -
A string representation of this object.
inherited
-
useGroup(
String name) → void - Adds a registered group of middleware to the pipeline.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
execute(
List< Middleware> middlewares, Request request, ResponseContract response, FutureOr<void> finalHandler(Request, ResponseContract)) → Future<void> - Executes a list of middleware in order, followed by a final handler.