Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enchant: Class-Based Middlewares and Endpoint callback #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions example/example_middleware_authentication_wildcard_with_mixin.dart
Original file line number Diff line number Diff line change
@@ -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();
}


18 changes: 18 additions & 0 deletions example/example_middleware_with_mixin.dart
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions lib/alfred.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
15 changes: 15 additions & 0 deletions lib/src/mixins/callable_request_mixin.dart
Original file line number Diff line number Diff line change
@@ -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<dynamic> call(HttpRequest req, HttpResponse res);

}

27 changes: 27 additions & 0 deletions test/alfred_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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';
}
}