返回 AiToEarn
normalize-endpoint.ts
根目录 / project / aitoearn-backend / libs / nest-mcp / src / utils / normalize-endpoint.ts
1 /**
2 * Normalizes an endpoint by removing double slashes and ensuring it does not start with a slash.
3 */
4 export function normalizeEndpoint(endpoint?: string | null): string {
5 try {
6 if (!endpoint) {
7 return ''
8 }
9
10 // Check if the endpoint has a protocol
11 const protocolMatch = endpoint.match(/^([a-z][a-z0-9+.-]*):\/\//i)
12
13 if (protocolMatch) {
14 // Has protocol: preserve protocol slashes, collapse slashes in the path
15 const protocol = protocolMatch[0] // e.g., "http://"
16 const pathPart = endpoint.slice(protocol.length)
17 const normalizedPath = pathPart.replace(/\/+/g, '/')
18 const result = protocol + normalizedPath
19 return result
20 }
21 else {
22 // No protocol: collapse all slashes and remove leading slash
23 const normalized = endpoint.replace(/\/+/g, '/')
24 const result = normalized.startsWith('/')
25 ? normalized.slice(1)
26 : normalized
27 return result
28 }
29 }
30 catch {
31 return ''
32 }
33 }
34
34 lines TYPESCRIPT