mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 09:18:30 +02:00
Better fallback request name generation
This commit is contained in:
@@ -3,31 +3,25 @@ import type { GrpcRequest, HttpRequest } from './models';
|
|||||||
export function fallbackRequestName(r: HttpRequest | GrpcRequest | null): string {
|
export function fallbackRequestName(r: HttpRequest | GrpcRequest | null): string {
|
||||||
if (r == null) return '';
|
if (r == null) return '';
|
||||||
|
|
||||||
|
// Return name if it has one
|
||||||
if (r.name) {
|
if (r.name) {
|
||||||
return r.name;
|
return r.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
const withoutVariables = r.url.replace(/\$\{\[\s*([^\]]+)\s*]}/g, '$1');
|
// Replace variable syntax with variable name
|
||||||
|
const withoutVariables = r.url.replace(/\$\{\[\s*([^\]\s]+)\s*]}/g, '$1');
|
||||||
if (withoutVariables.trim() === '') {
|
if (withoutVariables.trim() === '') {
|
||||||
return r.model === 'http_request' ? 'New HTTP Request' : 'new gRPC Request';
|
return r.model === 'http_request' ? 'New HTTP Request' : 'new gRPC Request';
|
||||||
}
|
}
|
||||||
|
|
||||||
const fixedUrl = withoutVariables.match(/^https?:\/\//)
|
// GRPC gets nice short names
|
||||||
? withoutVariables
|
|
||||||
: 'http://' + withoutVariables;
|
|
||||||
|
|
||||||
if (r.model === 'grpc_request' && r.service != null && r.method != null) {
|
if (r.model === 'grpc_request' && r.service != null && r.method != null) {
|
||||||
const shortService = r.service.split('.').pop();
|
const shortService = r.service.split('.').pop();
|
||||||
return `${shortService}/${r.method}`;
|
return `${shortService}/${r.method}`;
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
const url = new URL(fixedUrl);
|
|
||||||
const pathname = url.pathname === '/' ? '' : url.pathname;
|
|
||||||
return `${url.host}${pathname}`;
|
|
||||||
} catch (_) {
|
|
||||||
// Nothing
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.url;
|
// Strip unnecessary protocol
|
||||||
|
const withoutProto = withoutVariables.replace(/^https?:\/\//, '');
|
||||||
|
|
||||||
|
return withoutProto;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user