processDeepLink static method

SwiftRouting? processDeepLink(
  1. String url, {
  2. DeepLinkCallback? onDeepLink,
})

Processes a deep link URL and returns routing information.

url - The deep link URL to process onDeepLink - Optional callback for custom routing logic. If provided and returns null, will use default parsing.

Returns a SwiftRouting object with route and payload, or null if callback explicitly returns null.

Implementation

static SwiftRouting? processDeepLink(
  String url, {
  DeepLinkCallback? onDeepLink,
}) {
  debugPrint('[DeepLinkHandler] Processing deep link: $url');

  // Parse the URL
  final parsed = DeepLinkParser.parse(url);

  // If custom callback is provided, use it
  if (onDeepLink != null) {
    try {
      final customRouting = onDeepLink(
        url: url,
        route: parsed.route,
        queryParams: parsed.queryParams,
      );

      if (customRouting != null) {
        // Safety check: if callback returned a URL-like route, use parsed route instead
        if (customRouting.route.contains('://')) {
          debugPrint('[DeepLinkHandler] ⚠️ Callback returned URL-like route "${customRouting.route}", using parsed route "${parsed.route}" instead');
          return SwiftRouting(
            route: parsed.route,
            payload: customRouting.payload ?? (parsed.queryParams.isNotEmpty ? parsed.queryParams : null),
          );
        }
        debugPrint('[DeepLinkHandler] Custom callback returned route: ${customRouting.route}');
        return customRouting;
      } else {
        // Callback returned null, skip navigation
        debugPrint('[DeepLinkHandler] Custom callback returned null, skipping navigation');
        return null;
      }
    } catch (e, stackTrace) {
      debugPrint('[DeepLinkHandler] Error in onDeepLink callback: $e');
      debugPrint('[DeepLinkHandler] Stack trace: $stackTrace');
      // On error, fall back to default parsing
    }
  }

  // Default: use parsed route and query params as payload (only if no callback or callback failed)
  return SwiftRouting(
    route: parsed.route,
    payload: parsed.queryParams.isNotEmpty ? parsed.queryParams : null,
  );
}