Skip to content

Commit

Permalink
fix: URL compatibility (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyu2001 authored Aug 20, 2024
1 parent f6049d3 commit 9f26611
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/validators/src/url/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ const DYNAMIC_ROUTE_SEGMENT_REGEX = /\[\[?([^\]]+)\]?\]/g

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

if (!URL.canParse(url, baseOrigin.href)) {
let normalizedUrl
try {
normalizedUrl = new URL(url, baseOrigin)
} catch (error) {
throw new UrlValidationError(`Invalid URL: ${url}`)
}
const normalizedUrl = new URL(url, baseOrigin)

if (new URL(baseOrigin).origin !== normalizedUrl.origin) {
throw new UrlValidationError(
Expand Down

0 comments on commit 9f26611

Please sign in to comment.