Skip to content

Commit 34efe7d

Browse files
committed
Use URL.canParse
1 parent 4da3af6 commit 34efe7d

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/lib/seam/connect/seam-http-request.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,16 @@ export class SeamHttpRequest<
130130
}
131131

132132
const getUrlPrefix = (input: string): string => {
133-
try {
133+
if (URL.canParse(input)) {
134134
const url = new URL(input).toString()
135135
if (url.endsWith('/')) return url.slice(0, -1)
136136
return url
137-
} catch (err: unknown) {
138-
if (globalThis.location != null) {
139-
const pathname = input.startsWith('/') ? input : `/${input}`
140-
return new URL(`${globalThis.location.origin}${pathname}`).toString()
141-
}
142-
throw err
143137
}
138+
if (globalThis.location != null) {
139+
const pathname = input.startsWith('/') ? input : `/${input}`
140+
return new URL(`${globalThis.location.origin}${pathname}`).toString()
141+
}
142+
throw new Error(
143+
`Cannot resolve origin from ${input} in a non-browser environment`,
144+
)
144145
}

test/seam/connect/seam-http-request.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ test('SeamHttpRequest: url throws if unable to resolve origin', async (t) => {
143143

144144
const request = seam.devices.get({ device_id: 'abc123' })
145145

146-
t.throws(() => request.url, { code: 'ERR_INVALID_URL' })
146+
t.throws(() => request.url, { message: /Cannot resolve origin/ })
147147
})
148148

149149
const toPlainUrlObject = (url: URL): Omit<URL, 'searchParams' | 'toJSON'> => {

0 commit comments

Comments
 (0)