refactor(url): remove is-url dependency and improve URL validation

This commit is contained in:
Srinivas Vaddi
2025-08-13 01:49:56 -04:00
parent d4ba1bb6aa
commit c5c325c594

9
bin/utils/url.ts vendored
View File

@@ -1,5 +1,4 @@
import * as psl from 'psl'; import * as psl from 'psl';
import isUrl from 'is-url';
// Extracts the domain from a given URL. // Extracts the domain from a given URL.
export function getDomain(inputUrl: string): string | null { export function getDomain(inputUrl: string): string | null {
@@ -32,10 +31,10 @@ export function appendProtocol(inputUrl: string): string {
// Normalizes the URL by ensuring it has a protocol and is valid. // Normalizes the URL by ensuring it has a protocol and is valid.
export function normalizeUrl(urlToNormalize: string): string { export function normalizeUrl(urlToNormalize: string): string {
const urlWithProtocol = appendProtocol(urlToNormalize); const urlWithProtocol = appendProtocol(urlToNormalize);
try {
if (isUrl(urlWithProtocol)) { new URL(urlWithProtocol);
return urlWithProtocol; return urlWithProtocol;
} else { } catch (err) {
throw new Error(`Your url "${urlWithProtocol}" is invalid`); throw new Error(`Your url "${urlWithProtocol}" is invalid: ${(err as Error).message}`);
} }
} }