Skip to content

fix: Error in environments that do not support URL.canParse #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/lib/seam/connect/seam-http-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class SeamHttpRequest<
}

const getUrlPrefix = (input: string): string => {
if (URL.canParse(input)) {
if (canParseUrl(input)) {
const url = new URL(input).toString()
if (url.endsWith('/')) return url.slice(0, -1)
return url
Expand All @@ -166,3 +166,13 @@ const getUrlPrefix = (input: string): string => {
`Cannot resolve origin from ${input} in a non-browser environment`,
)
}

// UPSTREAM: Prefer URL.canParse when it has wider support.
// https://caniuse.com/mdn-api_url_canparse_static
const canParseUrl = (input: string): boolean => {
try {
return new URL(input) != null
} catch {
return false
}
}