matchPath method
Matches an incoming requestPath against this contract's pathTemplate on the server side.
If the path matches the template pattern, extracts and returns the percent-decoded path
parameters as a Map<String, String>. Returns null if the path does not match.
final params = contract.matchPath('/users/42/posts/100');
// Returns: {'userId': '42', 'postId': '100'}
Implementation
Map<String, String>? matchPath(String requestPath) {
// Strip query strings or trailing whitespace
final cleanPath = requestPath.split('?').first.trim();
final templateSegments = pathTemplate
.split('/')
.where((s) => s.isNotEmpty)
.toList(growable: false);
final actualSegments =
cleanPath.split('/').where((s) => s.isNotEmpty).toList(growable: false);
if (templateSegments.length != actualSegments.length) {
return null;
}
final extracted = <String, String>{};
for (var i = 0; i < templateSegments.length; i++) {
final tSegment = templateSegments[i];
final aSegment = actualSegments[i];
if (tSegment.startsWith(':')) {
final paramName = tSegment.substring(1);
extracted[paramName] = Uri.decodeComponent(aSegment);
} else if (tSegment != aSegment) {
return null;
}
}
return extracted;
}