refactor(url): remove is-url dependency and improve URL validation
This commit is contained in:
9
bin/utils/url.ts
vendored
9
bin/utils/url.ts
vendored
@@ -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}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user