31 lines
987 B
Dart
31 lines
987 B
Dart
/// Helper class for URL manipulation
|
|
class UrlHelper {
|
|
/// Resolve a potentially relative URL against a base URL
|
|
static String resolveUrl(String baseUrl, String url) {
|
|
// If URL is already absolute, return as is
|
|
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
return url;
|
|
}
|
|
|
|
// Parse base URL
|
|
final baseUri = Uri.parse(baseUrl);
|
|
|
|
// If URL starts with /, it's relative to the origin
|
|
if (url.startsWith('/')) {
|
|
return '${baseUri.scheme}://${baseUri.authority}$url';
|
|
}
|
|
|
|
// Otherwise, resolve relative to the base path
|
|
final basePath = baseUri.path.endsWith('/')
|
|
? baseUri.path
|
|
: baseUri.path.substring(0, baseUri.path.lastIndexOf('/') + 1);
|
|
|
|
return '${baseUri.scheme}://${baseUri.authority}$basePath$url';
|
|
}
|
|
|
|
/// Get the base URL (scheme + authority) from a full URL
|
|
static String getBaseUrl(String url) {
|
|
final uri = Uri.parse(url);
|
|
return '${uri.scheme}://${uri.authority}';
|
|
}
|
|
}
|