Skip to content

Commit

Permalink
refactor: use URL.canParse
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyu2001 committed Jul 16, 2024
1 parent d9fe972 commit 94de5ac
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions packages/validators/src/url/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import { Whitelist } from '@/url/options'
const DYNAMIC_ROUTE_SEGMENT_REGEX = /\[\[?([^\]]+)\]?\]/g

export const resolveRelativeUrl = (url: string, baseUrl?: URL): URL => {
if (!baseUrl) {
try {
return new URL(url)
} catch (error: TypeError) {
throw new UrlValidationError(`Invalid URL: ${url}`)
}
if (!baseUrl && !URL.canParse(url)) {
throw new UrlValidationError(`Invalid URL: ${url}`)
}

let normalizedUrl: URL
Expand All @@ -27,6 +23,7 @@ export const resolveRelativeUrl = (url: string, baseUrl?: URL): URL => {
}
return normalizedUrl
}

/* As of Next.js 14.2.5, router.push() resolves dynamic routes using query parameters. */
const resolveNextDynamicRoute = (url: URL): URL => {
const pathname = url.pathname
Expand Down

0 comments on commit 94de5ac

Please sign in to comment.