Skip to content

Commit

Permalink
Added MiddlewareExtensions which provides addMiddleware and `addH…
Browse files Browse the repository at this point in the history
…andler` (dart-lang#196)

...to `Middleware` instances.
Provides easier access to the composition capabilities offered by `Pipeline`.
  • Loading branch information
kevmoo authored Jul 2, 2021
1 parent a16ea03 commit 46483f8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.2.0

* Added `MiddlewareExtensions` which provides `addMiddleware` and `addHandler`
to `Middleware` instances. Provides easier access to the composition
capabilities offered by `Pipeline`.

## 1.1.4

* Documentation improvements.
Expand Down
1 change: 1 addition & 0 deletions lib/shelf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export 'src/hijack_exception.dart';
export 'src/middleware.dart';
export 'src/middleware/add_chunked_encoding.dart';
export 'src/middleware/logger.dart';
export 'src/middleware_extensions.dart';
export 'src/pipeline.dart';
export 'src/request.dart';
export 'src/response.dart';
Expand Down
14 changes: 14 additions & 0 deletions lib/src/middleware_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'handler.dart';
import 'middleware.dart';

/// Extensions on [Middleware] to aid in composing [Middleware] and [Handlers].
///
/// These members can be used in place of [Pipeline].
extension MiddlewareExtensions on Middleware {
/// Merges `this` and [other] into a new [Middleware].
Middleware addMiddleware(Middleware other) =>
(Handler handler) => this(other(handler));

/// Merges `this` and [handler] into a new [Handler].
Handler addHandler(Handler handler) => this(handler);
}
13 changes: 9 additions & 4 deletions lib/src/pipeline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import 'middleware.dart';
/// A helper that makes it easy to compose a set of [Middleware] and a
/// [Handler].
///
/// var handler = const Pipeline()
/// .addMiddleware(loggingMiddleware)
/// .addMiddleware(cachingMiddleware)
/// .addHandler(application);
/// ```dart
/// var handler = const Pipeline()
/// .addMiddleware(loggingMiddleware)
/// .addMiddleware(cachingMiddleware)
/// .addHandler(application);
/// ```
///
/// Note: this package also provides `addMiddleware` and `addHandler` extensions
// members on [Middleware], which may be easier to use.
class Pipeline {
const Pipeline();

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: shelf
version: 1.1.4
version: 1.2.0
description: >-
A model for web server middleware that encourages composition and easy reuse
repository: https://github.com/dart-lang/shelf
Expand Down
9 changes: 9 additions & 0 deletions test/pipeline_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ void main() {
expect(accessLocation, 5);
});

test('extensions for composition', () async {
var handler =
middlewareA.addMiddleware(middlewareB).addHandler(innerHandler);

final response = await makeSimpleRequest(handler);
expect(response, isNotNull);
expect(accessLocation, 5);
});

test('Pipeline can be used as middleware', () async {
var innerPipeline =
const Pipeline().addMiddleware(middlewareA).addMiddleware(middlewareB);
Expand Down

0 comments on commit 46483f8

Please sign in to comment.