Files
yaak/src-web/lib/fallbackRequestName.ts
2023-11-21 09:24:13 -08:00

26 lines
518 B
TypeScript

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 r.url;
}