From 3a7b0ab4fb5cbabb5a05d18db880c82f68c628c7 Mon Sep 17 00:00:00 2001 From: Amit Ranjan Date: Tue, 1 Oct 2024 20:05:07 +0530 Subject: [PATCH] feat(fix): added fix for the relative url too --- api/src/utils/helpers/URL.ts | 17 +++++++++-------- frontend/src/utils/URL.ts | 17 +++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/api/src/utils/helpers/URL.ts b/api/src/utils/helpers/URL.ts index df1426b..6f08386 100644 --- a/api/src/utils/helpers/URL.ts +++ b/api/src/utils/helpers/URL.ts @@ -1,13 +1,14 @@ export const buildURL = (baseUrl: string, relativePath: string): string => { try { - const normalizedBaseUrl = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`; - const normalizedRelativePath = relativePath.startsWith('/') - ? relativePath.slice(1) - : relativePath; - const url = new URL(normalizedRelativePath, normalizedBaseUrl); - - return url.toString(); + return new URL(relativePath).toString(); } catch { - throw new Error(`Invalid base URL: ${baseUrl}`); + try { + return new URL( + relativePath.replace(/^\//, ''), + baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`, + ).toString(); + } catch { + throw new Error(`Invalid base URL: ${baseUrl}`); + } } }; diff --git a/frontend/src/utils/URL.ts b/frontend/src/utils/URL.ts index b38c124..dc1c20d 100644 --- a/frontend/src/utils/URL.ts +++ b/frontend/src/utils/URL.ts @@ -28,15 +28,16 @@ export const getFromQuery = ({ export const buildURL = (baseUrl: string, relativePath: string): string => { try { - const normalizedBaseUrl = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`; - const normalizedRelativePath = relativePath.startsWith("/") - ? relativePath.slice(1) - : relativePath; - const url = new URL(normalizedRelativePath, normalizedBaseUrl); - - return url.toString(); + return new URL(relativePath).toString(); } catch { - throw new Error(`Invalid base URL: ${baseUrl}`); + try { + return new URL( + relativePath.replace(/^\//, ""), + baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`, + ).toString(); + } catch { + throw new Error(`Invalid base URL: ${baseUrl}`); + } } };