diff --git a/packages/validators/src/__tests__/url.test.ts b/packages/validators/src/__tests__/url.test.ts index fad1d8e..e804502 100644 --- a/packages/validators/src/__tests__/url.test.ts +++ b/packages/validators/src/__tests__/url.test.ts @@ -249,7 +249,7 @@ describe('UrlValidatorOptions.parsePathname', () => { it('should fallback to fallbackUrl if it is provided', () => { const pathname = validator.parsePathname('https://b.com/hello', 'bye') - expect(pathname).toStrictEqual('bye') + expect(pathname).toStrictEqual('/bye') }) }) diff --git a/packages/validators/src/url/index.ts b/packages/validators/src/url/index.ts index 7ca7df4..abd8139 100644 --- a/packages/validators/src/url/index.ts +++ b/packages/validators/src/url/index.ts @@ -63,22 +63,21 @@ export class UrlValidator { * Parses a URL string with a fallback option. * * @param url - The URL to validate - * @param fallbackUrl - The fallback URL to return if the URL is invalid. This is NOT validated. + * @param fallbackUrl - The fallback URL to return if the URL is invalid. * @throws {@link UrlValidationError} if the URL is invalid and fallbackUrl is not provided. * @returns The URL object if the URL is valid, else the fallbackUrl (if provided). * * @public */ - parse(url: string, fallbackUrl: T): URL | T + parse(url: string, fallbackUrl: string | URL): URL parse(url: string): URL parse(url: string, fallbackUrl: undefined): URL - parse(url: string, fallbackUrl?: T): URL | T { + parse(url: string, fallbackUrl?: string | URL): URL { try { return this.#parse(url) } catch (error) { if (error instanceof UrlValidationError && fallbackUrl !== undefined) { // URL validation failed, return the fallback URL - // This is NOT validated. return this.#parse(fallbackUrl instanceof URL ? fallbackUrl.href : fallbackUrl) } // otherwise rethrow @@ -90,16 +89,16 @@ export class UrlValidator { * Parses a URL string and returns the pathname with a fallback option. * * @param url - The URL to validate and extract pathname from - * @param fallbackUrl - The fallback URL to use if the URL is invalid. This is NOT validated. + * @param fallbackUrl - The fallback URL to use if the URL is invalid. * @throws {@link UrlValidationError} if the URL is invalid and fallbackUrl is not provided. * @returns The pathname of the URL or the fallback URL * * @public */ - parsePathname(url: string, fallbackUrl: T): string + parsePathname(url: string, fallbackUrl: string | URL): string parsePathname(url: string): string parsePathname(url: string, fallbackUrl: undefined): string - parsePathname(url: string, fallbackUrl?: T): string { + parsePathname(url: string, fallbackUrl?: string | URL): string { const parsedUrl = fallbackUrl ? this.parse(url, fallbackUrl) : this.parse(url) if (parsedUrl instanceof URL) return parsedUrl.pathname return parsedUrl