Request fallback name in header

This commit is contained in:
Gregory Schier
2023-11-09 17:34:57 -08:00
parent 29e0b9e5d2
commit 895fdce1b7
3 changed files with 31 additions and 22 deletions

View File

@@ -0,0 +1,25 @@
import type { HttpRequest } from './models';
export function fallbackRequestName(r: HttpRequest | null): string {
if (r == null) return '';
if (r.name) {
return r.name;
}
if (r.url.trim() === '') {
return 'New Request';
}
const fixedUrl = r.url.match(/^https?:\/\//) ? r.url : 'http://' + r.url;
try {
const url = new URL(fixedUrl);
const pathname = url.pathname === '/' ? '' : url.pathname;
return `${url.host}${pathname}`;
} catch (_) {
// Nothing
}
return '';
}