platform/lib/src/http/service.dart

112 lines
3.6 KiB
Dart
Raw Normal View History

2016-04-18 03:27:23 +00:00
part of angel_framework.http;
2016-06-21 22:56:04 +00:00
/// Indicates how the service was accessed.
///
/// This will be passed to the `params` object in a service method.
/// When requested on the server side, this will be null.
2016-06-19 05:02:41 +00:00
class Providers {
2016-06-21 22:56:04 +00:00
/// The transport through which the client is accessing this service.
2016-06-19 05:02:41 +00:00
final String via;
2016-06-21 22:56:04 +00:00
const Providers(String this.via);
2016-06-19 05:02:41 +00:00
2016-06-21 22:56:04 +00:00
static const String VIA_REST = "rest";
static const String VIA_WEBSOCKET = "websocket";
/// Represents a request via REST.
static final Providers REST = const Providers(VIA_REST);
/// Represents a request over WebSockets.
static final Providers WEBSOCKET = const Providers(VIA_WEBSOCKET);
2016-06-19 05:02:41 +00:00
}
2016-06-21 22:56:04 +00:00
/// A front-facing interface that can present data to and operate on data on behalf of the user.
///
/// Heavily inspired by FeathersJS. <3
2016-04-18 03:27:23 +00:00
class Service extends Routable {
/// The [Angel] app powering this service.
Angel app;
2016-04-18 03:27:23 +00:00
/// Retrieves all resources.
Future<List> index([Map params]) {
throw new AngelHttpException.MethodNotAllowed();
2016-04-18 03:27:23 +00:00
}
/// Retrieves the desired resource.
Future read(id, [Map params]) {
throw new AngelHttpException.MethodNotAllowed();
2016-04-18 03:27:23 +00:00
}
/// Creates a resource.
2016-06-19 05:02:41 +00:00
Future create(data, [Map params]) {
throw new AngelHttpException.MethodNotAllowed();
2016-04-18 03:27:23 +00:00
}
/// Modifies a resource.
2016-06-19 05:02:41 +00:00
Future modify(id, data, [Map params]) {
throw new AngelHttpException.MethodNotAllowed();
}
/// Overwrites a resource.
2016-06-19 05:02:41 +00:00
Future update(id, data, [Map params]) {
throw new AngelHttpException.MethodNotAllowed();
2016-04-18 03:27:23 +00:00
}
/// Removes the given resource.
Future remove(id, [Map params]) {
throw new AngelHttpException.MethodNotAllowed();
2016-04-18 03:27:23 +00:00
}
Service() : super() {
2016-06-19 05:02:41 +00:00
Map restProvider = {'provider': Providers.REST};
2016-06-23 19:09:49 +00:00
// Add global middleware if declared on the instance itself
Middleware before = _getAnnotation(this, Middleware);
if (before != null) {
routes.add(new Route("*", "*", before.handlers));
}
Middleware indexMiddleware = _getAnnotation(this.index, Middleware);
2016-06-21 04:19:43 +00:00
get('/', (req, res) async {
return await this.index(mergeMap([req.query, restProvider]));
}, middleware: (indexMiddleware == null) ? [] : indexMiddleware.handlers);
Middleware createMiddleware = _getAnnotation(this.create, Middleware);
post('/', (req, res) async => await this.create(req.body, restProvider),
middleware:
(createMiddleware == null) ? [] : createMiddleware.handlers);
Middleware readMiddleware = _getAnnotation(this.read, Middleware);
get(
'/:id',
(req, res) async => await this
.read(req.params['id'], mergeMap([req.query, restProvider])),
middleware: (readMiddleware == null) ? [] : readMiddleware.handlers);
Middleware modifyMiddleware = _getAnnotation(this.modify, Middleware);
patch(
'/:id',
(req, res) async =>
await this.modify(req.params['id'], req.body, restProvider),
middleware:
(modifyMiddleware == null) ? [] : modifyMiddleware.handlers);
Middleware updateMiddleware = _getAnnotation(this.update, Middleware);
post(
'/:id',
(req, res) async =>
await this.update(req.params['id'], req.body, restProvider),
middleware:
(updateMiddleware == null) ? [] : updateMiddleware.handlers);
Middleware removeMiddleware = _getAnnotation(this.remove, Middleware);
delete(
'/:id',
(req, res) async => await this
.remove(req.params['id'], mergeMap([req.query, restProvider])),
middleware:
(removeMiddleware == null) ? [] : removeMiddleware.handlers);
}
}