describeRoutes function

String describeRoutes()

Serialises the registered routes as JSON.

Implementation

String describeRoutes() {
  final routes = Router().routes.map((route) {
    final prefix = route.prefix;
    final path = route.path.startsWith('/') ? route.path : '/${route.path}';
    final full = (prefix == null || prefix.isEmpty)
        ? path
        : '/${prefix.replaceAll(RegExp(r'^/+|/+$'), '')}$path';

    return <String, dynamic>{
      'method': route.method.toUpperCase(),
      'uri': full.replaceAll(RegExp('/+'), '/'),
      'name': route.name,
      'domain': route.domain,
      'middleware': route.preMiddleware
          .map((m) => m.runtimeType.toString())
          .toList(),
      'constraints': route.regex,
    };
  }).toList();

  return jsonEncode(routes);
}