(baseUrl?: string | null, service: URLType = "router")
| 38 | * @returns A valid Requesty service URL |
| 39 | */ |
| 40 | export const toRequestyServiceUrl = (baseUrl?: string | null, service: URLType = "router"): string => { |
| 41 | // Handle null, undefined, empty string, or non-string values |
| 42 | const urlToUse = baseUrl && typeof baseUrl === "string" && baseUrl.trim() ? baseUrl.trim() : REQUESTY_BASE_URL |
| 43 | |
| 44 | try { |
| 45 | // Validate the URL first |
| 46 | const validatedUrl = new URL(urlToUse).toString() |
| 47 | // Apply service type transformation |
| 48 | return replaceCname(validatedUrl, service) |
| 49 | } catch (error) { |
| 50 | // If the provided baseUrl is invalid, fall back to the default |
| 51 | if (baseUrl && baseUrl !== REQUESTY_BASE_URL) { |
| 52 | console.warn(`Invalid base URL "${baseUrl}", falling back to default`) |
| 53 | } |
| 54 | return replaceCname(REQUESTY_BASE_URL, service) |
| 55 | } |
| 56 | } |
no test coverage detected