This commit is contained in:
thosakwe 2017-01-27 22:47:00 -05:00
parent 4c04e1193c
commit 0adfdbeb59
7 changed files with 96 additions and 54 deletions

View file

@ -1,6 +1,6 @@
# angel_framework
[![pub 1.0.0-dev.49](https://img.shields.io/badge/pub-1.0.0--dev.49-red.svg)](https://pub.dartlang.org/packages/angel_framework)
[![pub 1.0.0-dev.50](https://img.shields.io/badge/pub-1.0.0--dev.50-red.svg)](https://pub.dartlang.org/packages/angel_framework)
[![build status](https://travis-ci.org/angel-dart/framework.svg)](https://travis-ci.org/angel-dart/framework)
Core libraries for the Angel Framework.

View file

@ -55,7 +55,9 @@ class HookedService extends Service {
if (params == null)
return params;
else
return params..remove('__requestctx')..remove('__responsectx');
return params.keys
.where((key) => key != '__requestctx' && key != '__responsectx')
.fold({}, (map, key) => map..[key] = params[key]);
}
/// Adds hooks to this instance.
@ -102,17 +104,20 @@ class HookedService extends Service {
// Add global middleware if declared on the instance itself
Middleware before = getAnnotation(inner, Middleware);
final handlers = [];
final handlers = [
(RequestContext req, ResponseContext res) async {
req.query
..['__requestctx'] = req
..['__responsectx'] = res;
return true;
}
];
if (before != null) handlers.addAll(before.handlers);
Middleware indexMiddleware = getAnnotation(inner.index, Middleware);
get('/', (req, res) async {
return await this.index(mergeMap([
req.query,
restProvider,
{'__requestctx': req, '__responsectx': res}
]));
return await this.index(mergeMap([req.query, restProvider]));
},
middleware: []
..addAll(handlers)
@ -121,13 +126,8 @@ class HookedService extends Service {
Middleware createMiddleware = getAnnotation(inner.create, Middleware);
post(
'/',
(req, res) async => await this.create(
req.body,
mergeMap([
req.query,
restProvider,
{'__requestctx': req, '__responsectx': res}
])),
(req, res) async =>
await this.create(req.body, mergeMap([req.query, restProvider])),
middleware: []
..addAll(handlers)
..addAll(
@ -137,13 +137,8 @@ class HookedService extends Service {
get(
'/:id',
(req, res) async => await this.read(
req.params['id'],
mergeMap([
req.query,
restProvider,
{'__requestctx': req, '__responsectx': res}
])),
(req, res) async => await this
.read(req.params['id'], mergeMap([req.query, restProvider])),
middleware: []
..addAll(handlers)
..addAll((readMiddleware == null) ? [] : readMiddleware.handlers));
@ -152,13 +147,7 @@ class HookedService extends Service {
patch(
'/:id',
(req, res) async => await this.modify(
req.params['id'],
req.body,
mergeMap([
req.query,
restProvider,
{'__requestctx': req, '__responsectx': res}
])),
req.params['id'], req.body, mergeMap([req.query, restProvider])),
middleware: []
..addAll(handlers)
..addAll(
@ -168,13 +157,7 @@ class HookedService extends Service {
post(
'/:id',
(req, res) async => await this.update(
req.params['id'],
req.body,
mergeMap([
req.query,
restProvider,
{'__requestctx': req, '__responsectx': res}
])),
req.params['id'], req.body, mergeMap([req.query, restProvider])),
middleware: []
..addAll(handlers)
..addAll(
@ -183,13 +166,8 @@ class HookedService extends Service {
Middleware removeMiddleware = getAnnotation(inner.remove, Middleware);
delete(
'/:id',
(req, res) async => await this.remove(
req.params['id'],
mergeMap([
req.query,
restProvider,
{'__requestctx': req, '__responsectx': res}
])),
(req, res) async => await this
.remove(req.params['id'], mergeMap([req.query, restProvider])),
middleware: []
..addAll(handlers)
..addAll(
@ -198,6 +176,33 @@ class HookedService extends Service {
addHooks();
}
/// Runs the [listener] before every service method;
void beforeAll(HookedServiceEventListener listener) {
beforeIndexed.listen(listener);
beforeRead.listen(listener);
beforeCreated.listen(listener);
beforeModified.listen(listener);
beforeUpdated.listen(listener);
beforeRemoved.listen(listener);
}
/// Runs the [listener] after every service method.
void afterAll(HookedServiceEventListener listener) {
afterIndexed.listen(listener);
afterRead.listen(listener);
afterCreated.listen(listener);
afterModified.listen(listener);
afterUpdated.listen(listener);
afterRemoved.listen(listener);
}
/// Runs the [listener] before [create], [modify] and [update].
void beforeModify(HookedServiceEventListener listener) {
beforeCreated.listen(listener);
beforeModified.listen(listener);
beforeUpdated.listen(listener);
}
@override
Future<List> index([Map _params]) async {
var params = _stripReq(_params);
@ -415,7 +420,7 @@ class HookedServiceEvent {
/// The inner service whose method was hooked.
Service service;
HookedServiceEvent._base(this._result, this._response, Service this.service,
HookedServiceEvent._base(this._request, this._response, Service this.service,
String this._eventName,
{id, this.data, Map params, result}) {
_id = id;

View file

@ -70,7 +70,9 @@ class Routable extends Router {
super.registerMiddleware(name, middleware);
/// Retrieves the service assigned to the given path.
Service service(Pattern path) => _services[path];
Service service(Pattern path) =>
_services[path] ??
_services[path.toString().replaceAll(_straySlashes, '')];
/// Retrieves the controller with the given name.
Controller controller(String name) => controllers[name];

View file

@ -26,6 +26,9 @@ class Providers {
/// Represents a request over WebSockets.
static final Providers WEBSOCKET = const Providers(VIA_WEBSOCKET);
@override
bool operator ==(other) => other is Providers && other.via == via;
}
/// A front-facing interface that can present data to and operate on data on behalf of the user.
@ -83,7 +86,10 @@ class Service extends Routable {
..addAll((indexMiddleware == null) ? [] : indexMiddleware.handlers));
Middleware createMiddleware = getAnnotation(this.create, Middleware);
post('/', (req, res) async => await this.create(req.body, mergeMap([req.query, restProvider])),
post(
'/',
(req, res) async =>
await this.create(req.body, mergeMap([req.query, restProvider])),
middleware: []
..addAll(handlers)
..addAll(
@ -94,7 +100,7 @@ class Service extends Routable {
get(
'/:id',
(req, res) async => await this
.read(req.params['id'], mergeMap([req.query, restProvider])),
.read(req.params['id'], mergeMap([req.query, restProvider])),
middleware: []
..addAll(handlers)
..addAll((readMiddleware == null) ? [] : readMiddleware.handlers));
@ -102,8 +108,8 @@ class Service extends Routable {
Middleware modifyMiddleware = getAnnotation(this.modify, Middleware);
patch(
'/:id',
(req, res) async =>
await this.modify(req.params['id'], req.body, mergeMap([req.query, restProvider])),
(req, res) async => await this.modify(
req.params['id'], req.body, mergeMap([req.query, restProvider])),
middleware: []
..addAll(handlers)
..addAll(
@ -112,8 +118,8 @@ class Service extends Routable {
Middleware updateMiddleware = getAnnotation(this.update, Middleware);
post(
'/:id',
(req, res) async =>
await this.update(req.params['id'], req.body, mergeMap([req.query, restProvider])),
(req, res) async => await this.update(
req.params['id'], req.body, mergeMap([req.query, restProvider])),
middleware: []
..addAll(handlers)
..addAll(
@ -123,7 +129,7 @@ class Service extends Routable {
delete(
'/:id',
(req, res) async => await this
.remove(req.params['id'], mergeMap([req.query, restProvider])),
.remove(req.params['id'], mergeMap([req.query, restProvider])),
middleware: []
..addAll(handlers)
..addAll(

View file

@ -1,5 +1,5 @@
name: angel_framework
version: 1.0.0-dev.49
version: 1.0.0-dev.50
description: Core libraries for the Angel framework.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_framework

View file

@ -9,6 +9,17 @@ class Todo extends MemoryModel {
Todo({String this.text, String this.over});
}
class BookService extends Service {
@override
index([params]) async {
print('Book params: $params');
return [
{'foo': 'bar'}
];
}
}
incrementTodoTimes(e) {
IncrementService.TIMES++;
}

View file

@ -21,9 +21,10 @@ main() {
app = new Angel();
client = new http.Client();
app.use('/todos', new MemoryService<Todo>());
app.use('/books', new BookService());
Todos = app.service("todos");
// app.dumpTree(showMatchers: true);
app.fatalErrorStream.listen((e) => throw e.error);
server = await app.startServer();
url = "http://${app.httpServer.address.host}:${app.httpServer.port}";
@ -95,4 +96,21 @@ main() {
await service.index();
expect(IncrementService.TIMES, equals(2));
});
test('inject request + response', () async {
HookedService books = app.service('books');
books.beforeIndexed.listen((e) {
expect([e.request, e.response], everyElement(isNotNull));
print('Indexing books at path: ${e.request.path}');
});
var response = await client.get('$url/books');
print(response.body);
var result = god.deserialize(response.body);
expect(result, isList);
expect(result, isNotEmpty);
expect(result[0], equals({'foo': 'bar'}));
});
}