hexabot/api/src/utils/helpers/URL.ts
2024-10-01 20:05:07 +05:30

15 lines
374 B
TypeScript

export const buildURL = (baseUrl: string, relativePath: string): string => {
try {
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}`);
}
}
};