There are types when you have multiple Lifecycle Components that should be grouped together. For example, you may want to group all the Auth components (AuthMiddleware,
AuthExceptionCatcher, AuthGuard, AuthInterceptor, etc) together. You could annotate each one individually, or you can use a
CombineComponents class to group them together.
Execution#
Combine components themselves are not executed, but the components they contain are. The components are executed with their respective types.
Create a Combine Component#
To create a CombineComponent, create a class that extends CombineComponents.
import 'package:revali_router/revali_router.dart';
class AuthComponents implements CombineComponents {
const AuthComponents();
@override
List<ExceptionCatcher<Exception>> get catchers => [AuthExceptionCatcher()];
@override
List<Guard> get guards => [AuthGuard()];
@override
List<Interceptor> get interceptors => [AuthInterceptor()];
@override
List<Middleware> get middlewares => [AuthMiddleware()];
@override
List<RequestWrapper> get requestWrappers => [AuthRequestWrapper()];
}
Register the Combine Components#
To register the CombineComponents, annotate your CombineComponents class on the app, controller, or endpoint level.
import 'package:revali_router/revali_router.dart';
// highlight-next-line
@AuthComponents()
@Get('')
Future<void> myEndpoint() {
...
}
As Type Reference#
If any of your components require parameters that cannot be provided at compile time, resulting in the constructor not being a constant constructor. In this case, you can register the
AuthComponents as a type reference using the @Combines annotation.
import 'package:revali_router/revali_router.dart';
class AuthComponents implements CombineComponents {
const AuthComponents({
required this.service,
});
final AuthService service;
...
}
import 'package:revali_router/revali_router.dart';
// highlight-next-line
@Combines(AuthComponents)
@Get('')
Future<void> myEndpoint() {
...
}