feat(fix): added fix for the relative url too

This commit is contained in:
Amit Ranjan 2024-10-01 20:05:07 +05:30
parent 008257db64
commit 3a7b0ab4fb
2 changed files with 18 additions and 16 deletions

View File

@ -1,13 +1,14 @@
export const buildURL = (baseUrl: string, relativePath: string): string => { export const buildURL = (baseUrl: string, relativePath: string): string => {
try { try {
const normalizedBaseUrl = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`; return new URL(relativePath).toString();
const normalizedRelativePath = relativePath.startsWith('/') } catch {
? relativePath.slice(1) try {
: relativePath; return new URL(
const url = new URL(normalizedRelativePath, normalizedBaseUrl); relativePath.replace(/^\//, ''),
baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`,
return url.toString(); ).toString();
} catch { } catch {
throw new Error(`Invalid base URL: ${baseUrl}`); throw new Error(`Invalid base URL: ${baseUrl}`);
} }
}
}; };

View File

@ -28,16 +28,17 @@ export const getFromQuery = ({
export const buildURL = (baseUrl: string, relativePath: string): string => { export const buildURL = (baseUrl: string, relativePath: string): string => {
try { try {
const normalizedBaseUrl = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`; return new URL(relativePath).toString();
const normalizedRelativePath = relativePath.startsWith("/") } catch {
? relativePath.slice(1) try {
: relativePath; return new URL(
const url = new URL(normalizedRelativePath, normalizedBaseUrl); relativePath.replace(/^\//, ""),
baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`,
return url.toString(); ).toString();
} catch { } catch {
throw new Error(`Invalid base URL: ${baseUrl}`); throw new Error(`Invalid base URL: ${baseUrl}`);
} }
}
}; };
export const isAbsoluteUrl = (value: string = ""): boolean => { export const isAbsoluteUrl = (value: string = ""): boolean => {