From 07025403f46c94c8e00cfaf97ea487b78878b9bd Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sun, 12 Feb 2017 19:38:33 -0500 Subject: [PATCH] 55 --- README.md | 2 +- lib/common.dart | 8 ++++++ lib/hooks.dart | 16 ++++++++--- lib/src/defs.dart | 5 +++- lib/src/http/hooked_service.dart | 47 ++++++++++++++++++++++++-------- lib/src/http/memory_service.dart | 26 +++++++++--------- lib/src/http/server.dart | 10 +++++-- lib/src/http/service.dart | 47 ++++++++++++++++++++++++-------- test/common.dart | 2 +- test/services_test.dart | 2 +- 10 files changed, 120 insertions(+), 45 deletions(-) create mode 100644 lib/common.dart diff --git a/README.md b/README.md index 827a6ee2..1b3afc18 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # angel_framework -[![pub 1.0.0-dev.54](https://img.shields.io/badge/pub-1.0.0--dev.54-red.svg)](https://pub.dartlang.org/packages/angel_framework) +[![pub 1.0.0-dev.55](https://img.shields.io/badge/pub-1.0.0--dev.55-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. diff --git a/lib/common.dart b/lib/common.dart new file mode 100644 index 00000000..c07e0596 --- /dev/null +++ b/lib/common.dart @@ -0,0 +1,8 @@ +/// Represents arbitrary data, with an associated ID and timestamps. +class Model { + String id; + DateTime createdAt; + DateTime updatedAt; + + Model({this.id, this.createdAt, this.updatedAt}); +} \ No newline at end of file diff --git a/lib/hooks.dart b/lib/hooks.dart index c16408a4..4364f8a7 100644 --- a/lib/hooks.dart +++ b/lib/hooks.dart @@ -36,10 +36,14 @@ AngelConfigurer hookAllServices(callback(Service service)) { HookedServiceEventListener toJson() { return (HookedServiceEvent e) { normalize(obj) { - if (obj != null && obj is! Map) obj = god.serializeObject(obj); + if (obj != null && obj is! Map) return god.serializeObject(obj); + return obj; } - normalize(e.isBefore ? e.data : e.result); + if (e.isBefore) { + return e.data = normalize(e.data); + } else + e.result = normalize(e.result); }; } @@ -49,10 +53,14 @@ HookedServiceEventListener toType(Type type) { return (HookedServiceEvent e) { normalize(obj) { if (obj != null && obj.runtimeType != type) - obj = god.deserializeDatum(obj, outputType: type); + return god.deserializeDatum(obj, outputType: type); + return obj; } - normalize(e.isBefore ? e.data : e.result); + if (e.isBefore) { + return e.data = normalize(e.data); + } else + e.result = normalize(e.result); }; } diff --git a/lib/src/defs.dart b/lib/src/defs.dart index 53d6074d..fdf3da2b 100644 --- a/lib/src/defs.dart +++ b/lib/src/defs.dart @@ -1,6 +1,9 @@ library angel_framework.defs; +import '../common.dart'; +export '../common.dart'; -/// Represents data that can be serialized into a MemoryService; +/// Use [Model] instead. +@deprecated class MemoryModel { int id; } \ No newline at end of file diff --git a/lib/src/http/hooked_service.dart b/lib/src/http/hooked_service.dart index e46d0103..b0285529 100644 --- a/lib/src/http/hooked_service.dart +++ b/lib/src/http/hooked_service.dart @@ -117,7 +117,10 @@ class HookedService extends Service { Middleware indexMiddleware = getAnnotation(inner.index, Middleware); get('/', (req, res) async { - return await this.index(mergeMap([req.query, restProvider])); + return await this.index(mergeMap([ + {'query': req.query}, + restProvider + ])); }, middleware: [] ..addAll(handlers) @@ -126,8 +129,12 @@ class HookedService extends Service { Middleware createMiddleware = getAnnotation(inner.create, Middleware); post( '/', - (req, res) async => - await this.create(req.body, mergeMap([req.query, restProvider])), + (req, res) async => await this.create( + req.body, + mergeMap([ + {'query': req.query}, + restProvider + ])), middleware: [] ..addAll(handlers) ..addAll( @@ -137,8 +144,12 @@ class HookedService extends Service { get( '/:id', - (req, res) async => await this - .read(toId(req.params['id']), mergeMap([req.query, restProvider])), + (req, res) async => await this.read( + toId(req.params['id']), + mergeMap([ + {'query': req.query}, + restProvider + ])), middleware: [] ..addAll(handlers) ..addAll((readMiddleware == null) ? [] : readMiddleware.handlers)); @@ -146,8 +157,13 @@ class HookedService extends Service { Middleware modifyMiddleware = getAnnotation(inner.modify, Middleware); patch( '/:id', - (req, res) async => await this.modify(toId(req.params['id']), req.body, - mergeMap([req.query, restProvider])), + (req, res) async => await this.modify( + toId(req.params['id']), + req.body, + mergeMap([ + {'query': req.query}, + restProvider + ])), middleware: [] ..addAll(handlers) ..addAll( @@ -156,8 +172,13 @@ class HookedService extends Service { Middleware updateMiddleware = getAnnotation(inner.update, Middleware); post( '/:id', - (req, res) async => await this.update(toId(req.params['id']), req.body, - mergeMap([req.query, restProvider])), + (req, res) async => await this.update( + toId(req.params['id']), + req.body, + mergeMap([ + {'query': req.query}, + restProvider + ])), middleware: [] ..addAll(handlers) ..addAll( @@ -167,7 +188,11 @@ class HookedService extends Service { delete( '/:id', (req, res) async => await this.remove( - toId(req.params['id']), mergeMap([req.query, restProvider])), + toId(req.params['id']), + mergeMap([ + {'query': req.query}, + restProvider + ])), middleware: [] ..addAll(handlers) ..addAll( @@ -251,7 +276,7 @@ class HookedService extends Service { } @override - Future index([Map _params]) async { + Future index([Map _params]) async { var params = _stripReq(_params); HookedServiceEvent before = await beforeIndexed._emit( new HookedServiceEvent._base(false, _getRequest(_params), diff --git a/lib/src/http/memory_service.dart b/lib/src/http/memory_service.dart index 1d6d1b6b..724bd1a4 100644 --- a/lib/src/http/memory_service.dart +++ b/lib/src/http/memory_service.dart @@ -4,7 +4,7 @@ import 'dart:async'; import 'dart:mirrors'; import 'package:json_god/json_god.dart' as god; import 'package:merge_map/merge_map.dart'; -import '../defs.dart'; +import '../../common.dart'; import 'angel_http_exception.dart'; import 'service.dart'; @@ -24,17 +24,17 @@ class MemoryService extends Service { final bool allowRemoveAll; //// The data contained in this service. - final Map items = {}; + final Map items = {}; MemoryService({this.allowRemoveAll: false}) : super() { - if (!reflectType(T).isAssignableTo(reflectType(MemoryModel))) { + if (!reflectType(T).isAssignableTo(reflectType(Model))) { throw new Exception( - "MemoryServices only support classes that inherit from MemoryModel."); + "MemoryServices only support classes that inherit from Model."); } } - _makeJson(int index, MemoryModel t) { - return t..id = index; + _makeJson(int index, Model t) { + return t..id = index.toString(); } Future index([Map params]) async { @@ -47,7 +47,7 @@ class MemoryService extends Service { Future read(id, [Map params]) async { int desiredId = _getId(id); if (items.containsKey(desiredId)) { - MemoryModel found = items[desiredId]; + Model found = items[desiredId]; if (found != null) { return _makeJson(desiredId, found); } else @@ -58,12 +58,12 @@ class MemoryService extends Service { Future create(data, [Map params]) async { //try { - MemoryModel created = (data is MemoryModel) - ? data - : god.deserializeDatum(data, outputType: T); + Model created = + (data is Model) ? data : god.deserializeDatum(data, outputType: T); - created.id = items.length; - items[created.id] = created; + int size = items.length; + created.id = size.toString(); + items[size] = created; return created; /*} catch (e) { throw new AngelHttpException.BadRequest(message: 'Invalid data.'); @@ -111,7 +111,7 @@ class MemoryService extends Service { int desiredId = _getId(id); if (items.containsKey(desiredId)) { - MemoryModel item = items[desiredId]; + Model item = items[desiredId]; items[desiredId] = null; return _makeJson(desiredId, item); } else diff --git a/lib/src/http/server.dart b/lib/src/http/server.dart index c66c4c67..ec4ef8be 100644 --- a/lib/src/http/server.dart +++ b/lib/src/http/server.dart @@ -275,7 +275,7 @@ class Angel extends AngelBase { } try { - await sendRequest(request, req, res); + await sendResponse(request, req, res); } catch (e, st) { _fatalErrorStream .add(new AngelFatalError(request: request, error: e, stack: st)); @@ -332,8 +332,14 @@ class Angel extends AngelBase { // return await closureMirror.apply(args).reflectee; } - /// Sends a response. + /// Use [sendResponse] instead. + @deprecated Future sendRequest( + HttpRequest request, RequestContext req, ResponseContext res) => + sendResponse(request, req, res); + + /// Sends a response. + Future sendResponse( HttpRequest request, RequestContext req, ResponseContext res) async { _afterProcessed.add(request); diff --git a/lib/src/http/service.dart b/lib/src/http/service.dart index 227856b2..a8ee4ef2 100644 --- a/lib/src/http/service.dart +++ b/lib/src/http/service.dart @@ -39,7 +39,7 @@ class Service extends Routable { AngelBase app; /// Retrieves all resources. - Future index([Map params]) { + Future index([Map params]) { throw new AngelHttpException.methodNotAllowed(); } @@ -88,7 +88,10 @@ class Service extends Routable { Middleware indexMiddleware = getAnnotation(this.index, Middleware); get('/', (req, res) async { - return await this.index(mergeMap([req.query, restProvider])); + return await this.index(mergeMap([ + {'query': req.query}, + restProvider + ])); }, middleware: [] ..addAll(handlers) @@ -97,8 +100,12 @@ class Service extends Routable { Middleware createMiddleware = getAnnotation(this.create, Middleware); post( '/', - (req, res) async => - await this.create(req.body, mergeMap([req.query, restProvider])), + (req, res) async => await this.create( + req.body, + mergeMap([ + {'query': req.query}, + restProvider + ])), middleware: [] ..addAll(handlers) ..addAll( @@ -108,8 +115,12 @@ class Service extends Routable { get( '/:id', - (req, res) async => await this - .read(toId(req.params['id']), mergeMap([req.query, restProvider])), + (req, res) async => await this.read( + toId(req.params['id']), + mergeMap([ + {'query': req.query}, + restProvider + ])), middleware: [] ..addAll(handlers) ..addAll((readMiddleware == null) ? [] : readMiddleware.handlers)); @@ -117,8 +128,13 @@ class Service extends Routable { Middleware modifyMiddleware = getAnnotation(this.modify, Middleware); patch( '/:id', - (req, res) async => await this.modify(toId(req.params['id']), req.body, - mergeMap([req.query, restProvider])), + (req, res) async => await this.modify( + toId(req.params['id']), + req.body, + mergeMap([ + {'query': req.query}, + restProvider + ])), middleware: [] ..addAll(handlers) ..addAll( @@ -127,8 +143,13 @@ class Service extends Routable { Middleware updateMiddleware = getAnnotation(this.update, Middleware); post( '/:id', - (req, res) async => await this.update(toId(req.params['id']), req.body, - mergeMap([req.query, restProvider])), + (req, res) async => await this.update( + toId(req.params['id']), + req.body, + mergeMap([ + {'query': req.query}, + restProvider + ])), middleware: [] ..addAll(handlers) ..addAll( @@ -138,7 +159,11 @@ class Service extends Routable { delete( '/:id', (req, res) async => await this.remove( - toId(req.params['id']), mergeMap([req.query, restProvider])), + toId(req.params['id']), + mergeMap([ + {'query': req.query}, + restProvider + ])), middleware: [] ..addAll(handlers) ..addAll( diff --git a/test/common.dart b/test/common.dart index 6e17aaa3..724b96cc 100644 --- a/test/common.dart +++ b/test/common.dart @@ -2,7 +2,7 @@ library angel_framework.test.common; import 'package:angel_framework/angel_framework.dart'; -class Todo extends MemoryModel { +class Todo extends Model { String text; String over; diff --git a/test/services_test.dart b/test/services_test.dart index 70cf063e..a590d372 100644 --- a/test/services_test.dart +++ b/test/services_test.dart @@ -4,7 +4,7 @@ import 'package:http/http.dart' as http; import 'package:json_god/json_god.dart' as god; import 'package:test/test.dart'; -class Todo extends MemoryModel { +class Todo extends Model { String text; String over; }