diff --git a/example/example_middleware_authentication_wildcard_with_mixin.dart b/example/example_middleware_authentication_wildcard_with_mixin.dart new file mode 100644 index 0000000..fcf9b5e --- /dev/null +++ b/example/example_middleware_authentication_wildcard_with_mixin.dart @@ -0,0 +1,25 @@ +import 'dart:async'; + +import 'package:alfred/alfred.dart'; + +class _AuthenticationMiddleware with CallableRequestMixin{ + @override + FutureOr call(HttpRequest req, HttpResponse res) async{ + res.statusCode = 401; + await res.close(); + } +} + +void main() async { + final app = Alfred(); + + app.all('/resource*', _AuthenticationMiddleware()); + + app.get('/resource', (req, res) {}); //Will not be hit + app.post('/resource', (req, res) {}); //Will not be hit + app.post('/resource/1', (req, res) {}); //Will not be hit + + await app.listen(); +} + + diff --git a/example/example_middleware_with_mixin.dart b/example/example_middleware_with_mixin.dart new file mode 100644 index 0000000..0d9c3d9 --- /dev/null +++ b/example/example_middleware_with_mixin.dart @@ -0,0 +1,18 @@ +import 'dart:async'; + +import 'package:alfred/alfred.dart'; + +class _ExampleMiddleware with CallableRequestMixin{ + @override + FutureOr call(HttpRequest req, HttpResponse res) { + if (req.headers.value('Authorization') != 'apikey') { + throw AlfredException(401, {'message': 'authentication failed'}); + } + } +} +void main() async { + final app = Alfred(); + app.all('/example/:id/:name', (req, res) {}, middleware: [_ExampleMiddleware()]); + + await app.listen(); //Listening on port 3000 +} diff --git a/lib/alfred.dart b/lib/alfred.dart index d6d8581..0a23668 100644 --- a/lib/alfred.dart +++ b/lib/alfred.dart @@ -13,3 +13,4 @@ export 'src/middleware/cors.dart'; export 'src/plugins/store_plugin.dart'; export 'src/type_handlers/type_handler.dart'; export 'src/route_param_types/http_route_param_type.dart'; +export 'src/mixins/callable_request_mixin.dart'; diff --git a/lib/src/mixins/callable_request_mixin.dart b/lib/src/mixins/callable_request_mixin.dart new file mode 100644 index 0000000..38701f5 --- /dev/null +++ b/lib/src/mixins/callable_request_mixin.dart @@ -0,0 +1,15 @@ +import 'dart:async'; +import 'package:alfred/alfred.dart'; + +/// A Mixin to Resquest Callback +/// +/// Use this mixin to create a callable class +/// that can be used like a +/// callback enpoint or middlewares + +mixin CallableRequestMixin { + + FutureOr call(HttpRequest req, HttpResponse res); + +} + diff --git a/test/alfred_test.dart b/test/alfred_test.dart index d610888..0c96e00 100644 --- a/test/alfred_test.dart +++ b/test/alfred_test.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:convert'; import 'dart:io'; @@ -30,6 +31,12 @@ void main() { expect(response.headers['content-type'], 'application/json; charset=utf-8'); expect(response.body, '{"test":true}'); }); + test('With Mixin: it should return json', () async { + app.get('/test', _CallableRequest() ); + final response = await http.get(Uri.parse('http://localhost:$port/test')); + expect(response.headers['content-type'], 'application/json; charset=utf-8'); + expect(response.body, '{"ok":true,"msg":"callable class"}'); + }); test('it should return an image', () async { app.get('/test', (req, res) => File('test/files/image.jpg')); @@ -262,6 +269,12 @@ void main() { expect(response.body, 'hit middleware'); }); + test('With Mixin: it executes middleware, but handles it and stops executing', () async { + app.get('/test', (req, res) => 'test route', middleware: [_TestHitMiddleware()]); + final response = await http.get(Uri.parse('http://localhost:$port/test')); + expect(response.body, 'hit middleware'); + }); + test('it closes out a request if you fail to', () async { app.get('/test', (req, res) => null); final response = await http.get(Uri.parse('http://localhost:$port/test')); @@ -661,3 +674,17 @@ class RefNumberTypeParameter implements HttpRouteParamType { return value.toUpperCase(); } } + +class _CallableRequest with CallableRequestMixin{ + @override + FutureOr call(HttpRequest req, HttpResponse res) { + return res.json({'ok':true,'msg':'callable class'}); + } +} + +class _TestHitMiddleware with CallableRequestMixin{ + @override + FutureOr call(HttpRequest req, HttpResponse res) { + return 'hit middleware'; + } +} \ No newline at end of file