mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-14 21:23:40 +01:00
27 lines
594 B
TypeScript
27 lines
594 B
TypeScript
import type { HttpRequest } from './models';
|
|
|
|
export function fallbackRequestName(r: HttpRequest | null): string {
|
|
if (r == null) return '';
|
|
|
|
if (r.name) {
|
|
return r.name;
|
|
}
|
|
|
|
const withoutVariables = r.url.replace(/\$\{\[[^\]]+]}/g, '');
|
|
if (withoutVariables.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;
|
|
}
|