55
This commit is contained in:
parent
6eebc7afa8
commit
07025403f4
10 changed files with 120 additions and 45 deletions
|
@ -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.
|
||||
|
|
8
lib/common.dart
Normal file
8
lib/common.dart
Normal file
|
@ -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});
|
||||
}
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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<List> index([Map _params]) async {
|
||||
Future index([Map _params]) async {
|
||||
var params = _stripReq(_params);
|
||||
HookedServiceEvent before = await beforeIndexed._emit(
|
||||
new HookedServiceEvent._base(false, _getRequest(_params),
|
||||
|
|
|
@ -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<T> extends Service {
|
|||
final bool allowRemoveAll;
|
||||
|
||||
//// The data contained in this service.
|
||||
final Map<int, MemoryModel> items = {};
|
||||
final Map<int, Model> 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<List> index([Map params]) async {
|
||||
|
@ -47,7 +47,7 @@ class MemoryService<T> 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<T> 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<T> 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
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ class Service extends Routable {
|
|||
AngelBase app;
|
||||
|
||||
/// Retrieves all resources.
|
||||
Future<List> 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(
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue