platform/lib/src/core/metadata.dart

141 lines
4.1 KiB
Dart
Raw Normal View History

library angel_framework.http.metadata;
2017-10-10 16:55:42 +00:00
import 'package:angel_http_exception/angel_http_exception.dart';
import 'hooked_service.dart' show HookedServiceEventListener;
2017-10-10 16:55:42 +00:00
import 'request_context.dart';
import 'routable.dart';
2016-12-10 14:05:40 +00:00
2016-06-21 22:56:04 +00:00
/// Annotation to map middleware onto a handler.
class Middleware {
final Iterable<RequestHandler> handlers;
2016-12-10 14:05:40 +00:00
const Middleware(this.handlers);
}
2016-12-10 14:05:40 +00:00
/// Attaches hooks to a [HookedService].
class Hooks {
final List<HookedServiceEventListener> before;
final List<HookedServiceEventListener> after;
const Hooks({this.before: const [], this.after: const []});
}
/// Exposes a [Controller] to the Internet.
2016-06-27 00:20:42 +00:00
class Expose {
final String method;
final String path;
final Iterable<RequestHandler> middleware;
2016-06-27 00:20:42 +00:00
final String as;
final List<String> allowNull;
2016-12-10 14:05:40 +00:00
const Expose(this.path,
{this.method: "GET",
this.middleware: const [],
this.as: null,
this.allowNull: const []});
}
2017-10-10 16:55:42 +00:00
/// Used to apply special dependency injections or functionality to a function parameter.
class Parameter {
/// Inject the value of a request cookie.
final String cookie;
/// Inject the value of a request header.
final String header;
/// Inject the value of a key from the session.
final String session;
/// Inject the value of a key from the query.
final String query;
/// Only execute the handler if the value of this parameter matches the given value.
final match;
/// Specify a default value.
final defaultValue;
/// If `true` (default), then an error will be thrown if this parameter is not present.
final bool required;
const Parameter(
{this.cookie,
this.query,
this.header,
this.session,
this.match,
this.defaultValue,
this.required});
/// Returns an error that can be thrown when the parameter is not present.
get error {
if (cookie?.isNotEmpty == true)
return new AngelHttpException.badRequest(
message: 'Missing required cookie "$cookie".');
if (header?.isNotEmpty == true)
return new AngelHttpException.badRequest(
message: 'Missing required header "$header".');
if (query?.isNotEmpty == true)
return new AngelHttpException.badRequest(
message: 'Missing required query parameter "$query".');
if (session?.isNotEmpty == true)
return new StateError(
'Session does not contain required key "$session".');
}
/// Obtains a value for this parameter from a [RequestContext].
getValue(RequestContext req) {
if (cookie?.isNotEmpty == true)
return req.cookies.firstWhere((c) => c.name == cookie)?.value ??
defaultValue;
if (header?.isNotEmpty == true)
return req.headers.value(header) ?? defaultValue;
if (session?.isNotEmpty == true)
return req.session[session] ?? defaultValue;
if (query?.isNotEmpty == true)
return req.uri.queryParameters[query] ?? defaultValue;
2017-10-10 16:55:42 +00:00
return defaultValue;
}
}
/// Shortcut for declaring a request header [Parameter].
class Header extends Parameter {
const Header(String header, {match, defaultValue, bool required: true})
: super(
header: header,
match: match,
defaultValue: defaultValue,
required: required);
}
/// Shortcut for declaring a request session [Parameter].
class Session extends Parameter {
const Session(String session, {match, defaultValue, bool required: true})
: super(
session: session,
match: match,
defaultValue: defaultValue,
required: required);
}
/// Shortcut for declaring a request query [Parameter].
class Query extends Parameter {
const Query(String query, {match, defaultValue, bool required: true})
: super(
query: query,
match: match,
defaultValue: defaultValue,
required: required);
}
/// Shortcut for declaring a request cookie [Parameter].
class CookieValue extends Parameter {
const CookieValue(String cookie, {match, defaultValue, bool required: true})
: super(
cookie: cookie,
match: match,
defaultValue: defaultValue,
required: required);
}