platform/packages/user_agent/lib/angel_user_agent.dart
Tobe O a5f5661650 Add 'packages/user_agent/' from commit '70b15207d5e746fca954c5c7107ad8e1be059078'
git-subtree-dir: packages/user_agent
git-subtree-mainline: ae8f7a77d2
git-subtree-split: 70b15207d5
2020-02-15 18:29:14 -05:00

23 lines
818 B
Dart

import 'package:angel_framework/angel_framework.dart';
import 'package:user_agent/user_agent.dart';
/// Injects a [UserAgent] factory into requests.
///
/// Because it is an injected factory, the user agent will not be
/// parsed until you request it via `req.container.make<UserAgent>()`.
bool parseUserAgent(RequestContext req, ResponseContext res) {
req.container.registerFactory<UserAgent>((container) {
var agentString = req.headers.value('user-agent');
if (agentString?.trim()?.isNotEmpty != true) {
throw new AngelHttpException.badRequest(
message: 'User-Agent header is required.');
} else if (agentString != null) {
var userAgent = new UserAgent(agentString);
container.registerSingleton<UserAgent>(userAgent);
return userAgent;
}
});
return true;
}