Skip to content
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

Fix UrlParams.makeUrl when globalThis.location is set to undefined #2514

Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/stale-bugs-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": patch
---

Fix UrlParams.makeUrl when globalThis.location is set to `undefined`
2 changes: 1 addition & 1 deletion packages/platform/src/Http/UrlParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export const makeUrl = <E>(url: string, params: UrlParams, onError: (e: unknown)
})

const baseUrl = (): string | undefined => {
if ("location" in globalThis) {
if ("location" in globalThis && globalThis.location !== undefined) {
return location.origin + location.pathname
}
return undefined
Expand Down
26 changes: 26 additions & 0 deletions packages/platform/test/Http/UrlParams.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as UrlParams from "@effect/platform/Http/UrlParams"
import { assert, describe, it } from "@effect/vitest"
import { Effect } from "effect"

describe("UrlParams", () => {
describe("makeUrl", () => {
it.effect("does not throw if `location` is set to `undefined`", () =>
Effect.gen(function*(_) {
const originalLocation = globalThis.location

// `globalThis.location` is undefined
// @ts-expect-error
globalThis.location = undefined
let url = yield* _(UrlParams.makeUrl("http://example.com", [], () => "error"))
assert.strictEqual(url.toString(), "http://example.com/")

// `location` is not in globalThis
// @ts-expect-error
delete globalThis.location
url = yield* _(UrlParams.makeUrl("http://example.com", [], () => "error"))
assert.strictEqual(url.toString(), "http://example.com/")

globalThis.location = originalLocation
}))
})
})