registerInterceptor method

void registerInterceptor(
  1. String? route,
  2. dynamic action(
    1. Request req
    )
)

Registers a middleware action for the given route.

  • route the route to register in this object's REST server (service).
  • action the middleware action to perform at the given route.

Implementation

void registerInterceptor(String? route, Function(Request req) action) {
  route = route != null && route.startsWith('/') ? route.substring(1) : route;
  _interceptors.add((Request req) async {
    var regExp = RegExp(
      route ?? '',
      caseSensitive: true,
      multiLine: false,
    );

    var match = regExp.hasMatch(req.url.path);
    if (route != null && route != '' && !match) {
      return null;
    } else {
      return await action(req);
    }
  });
}