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 => {
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 {
try {
return new URL(
relativePath.replace(/^\//, ''),
baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`,
).toString();
} catch {
throw new Error(`Invalid base URL: ${baseUrl}`);
}
}
};

View File

@ -28,16 +28,17 @@ 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 {
try {
return new URL(
relativePath.replace(/^\//, ""),
baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`,
).toString();
} catch {
throw new Error(`Invalid base URL: ${baseUrl}`);
}
}
};
export const isAbsoluteUrl = (value: string = ""): boolean => {