addRoute method

void addRoute(
  1. Route route
)

Add a route

Implementation

void addRoute(Route route) {
  List<dynamic> result = preparePath(route.path);
  String path = result[0];
  Map<String, int> params = result[1];

  if (!_routes.containsKey(route.method)) {
    throw Exception("Method (${route.method}) not supported.");
  }

  if (_routes[route.method]!.containsKey(path)) {
    throw Exception("Route for (${route.method}:$path) already registered.");
  }

  params.forEach((key, index) {
    route.setPathParam(key, index);
  });

  _routes[route.method]![path] = (route);

  for (String alias in route.aliases) {
    List<dynamic> aliasResult = preparePath(alias);
    String aliasPath = aliasResult[0];
    _routes[route.method]![aliasPath] = route;
  }
}