2016-09-15 19:53:01 +00:00
|
|
|
library angel_framework.http.service;
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:merge_map/merge_map.dart';
|
|
|
|
import '../util.dart';
|
|
|
|
import 'angel_base.dart';
|
|
|
|
import 'angel_http_exception.dart';
|
|
|
|
import 'metadata.dart';
|
|
|
|
import 'routable.dart';
|
2016-04-18 03:27:23 +00:00
|
|
|
|
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 {
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
/// The [Angel] app powering this service.
|
2016-09-15 19:53:01 +00:00
|
|
|
AngelBase app;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// Retrieves all resources.
|
|
|
|
Future<List> index([Map params]) {
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
throw new AngelHttpException.MethodNotAllowed();
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the desired resource.
|
2016-05-02 22:28:14 +00:00
|
|
|
Future read(id, [Map params]) {
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
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]) {
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
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]) {
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
throw new AngelHttpException.MethodNotAllowed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Overwrites a resource.
|
2016-06-19 05:02:41 +00:00
|
|
|
Future update(id, data, [Map params]) {
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
throw new AngelHttpException.MethodNotAllowed();
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the given resource.
|
2016-05-02 22:28:14 +00:00
|
|
|
Future remove(id, [Map params]) {
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
throw new AngelHttpException.MethodNotAllowed();
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 09:10:47 +00:00
|
|
|
void addRoutes() {
|
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
|
2016-09-15 19:53:01 +00:00
|
|
|
Middleware before = getAnnotation(this, Middleware);
|
2016-10-22 20:41:36 +00:00
|
|
|
final handlers = [];
|
|
|
|
|
2016-11-23 09:10:47 +00:00
|
|
|
if (before != null) handlers.addAll(before.handlers);
|
2016-06-23 19:09:49 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
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]));
|
2016-10-22 20:41:36 +00:00
|
|
|
},
|
|
|
|
middleware: []
|
|
|
|
..addAll(handlers)
|
|
|
|
..addAll((indexMiddleware == null) ? [] : indexMiddleware.handlers));
|
2016-06-23 19:05:55 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
Middleware createMiddleware = getAnnotation(this.create, Middleware);
|
2016-06-23 19:05:55 +00:00
|
|
|
post('/', (req, res) async => await this.create(req.body, restProvider),
|
2016-10-22 20:41:36 +00:00
|
|
|
middleware: []
|
|
|
|
..addAll(handlers)
|
|
|
|
..addAll(
|
|
|
|
(createMiddleware == null) ? [] : createMiddleware.handlers));
|
2016-06-23 19:05:55 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
Middleware readMiddleware = getAnnotation(this.read, Middleware);
|
2016-06-23 19:05:55 +00:00
|
|
|
|
|
|
|
get(
|
|
|
|
'/:id',
|
|
|
|
(req, res) async => await this
|
2016-11-23 09:10:47 +00:00
|
|
|
.read(req.params['id'], mergeMap([req.query, restProvider])),
|
2016-10-22 20:41:36 +00:00
|
|
|
middleware: []
|
|
|
|
..addAll(handlers)
|
|
|
|
..addAll((readMiddleware == null) ? [] : readMiddleware.handlers));
|
2016-06-23 19:05:55 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
Middleware modifyMiddleware = getAnnotation(this.modify, Middleware);
|
2016-06-23 19:05:55 +00:00
|
|
|
patch(
|
|
|
|
'/:id',
|
|
|
|
(req, res) async =>
|
2016-11-23 09:10:47 +00:00
|
|
|
await this.modify(req.params['id'], req.body, restProvider),
|
2016-10-22 20:41:36 +00:00
|
|
|
middleware: []
|
|
|
|
..addAll(handlers)
|
|
|
|
..addAll(
|
|
|
|
(modifyMiddleware == null) ? [] : modifyMiddleware.handlers));
|
2016-06-23 19:05:55 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
Middleware updateMiddleware = getAnnotation(this.update, Middleware);
|
2016-06-23 19:05:55 +00:00
|
|
|
post(
|
|
|
|
'/:id',
|
|
|
|
(req, res) async =>
|
2016-11-23 09:10:47 +00:00
|
|
|
await this.update(req.params['id'], req.body, restProvider),
|
2016-10-22 20:41:36 +00:00
|
|
|
middleware: []
|
|
|
|
..addAll(handlers)
|
|
|
|
..addAll(
|
|
|
|
(updateMiddleware == null) ? [] : updateMiddleware.handlers));
|
2016-06-23 19:05:55 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
Middleware removeMiddleware = getAnnotation(this.remove, Middleware);
|
2016-06-23 19:05:55 +00:00
|
|
|
delete(
|
|
|
|
'/:id',
|
|
|
|
(req, res) async => await this
|
2016-11-23 09:10:47 +00:00
|
|
|
.remove(req.params['id'], mergeMap([req.query, restProvider])),
|
2016-10-22 20:41:36 +00:00
|
|
|
middleware: []
|
|
|
|
..addAll(handlers)
|
|
|
|
..addAll(
|
|
|
|
(removeMiddleware == null) ? [] : removeMiddleware.handlers));
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
}
|
2016-06-23 19:05:55 +00:00
|
|
|
}
|