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 1 commit
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
Next Next commit
Fix HTTP Client"s compatibility with Deno due to globalThis.location.
  • Loading branch information
rocwang authored and tim-smart committed Apr 16, 2024
commit 533bf87f555ce218980be21447fed79d43747370
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 HTTP Client's compatibility with Deno due to globalThis.location.
7 changes: 5 additions & 2 deletions packages/platform/src/Http/UrlParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,11 @@ export const makeUrl = <E>(url: string, params: UrlParams, onError: (e: unknown)
catch: onError
})

const baseUrl = (): string | undefined => {
if ("location" in globalThis) {
export const baseUrl = (): string | undefined => {
// Need to both "in" and "undefined" for location to support Deno.
// As by default, Deno has "globalThis.location" defined but with value "undefined".
// See https://docs.deno.com/runtime/manual/runtime/location_api
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 "vitest"

describe("UrlParams", () => {
describe("baseUrl", () => {
it("should return undefined when `location` is not in `globalThis` or `globalThis.location` is undefined", () => {
const originalLocation = globalThis.location

// `globalThis.location` is undefined
// @ts-expect-error
globalThis.location = undefined
assert.strictEqual("location" in globalThis, true)
assert.strictEqual(globalThis.location, undefined)
assert.strictEqual(UrlParams.baseUrl(), undefined)

// `location` is not in globalThis
// @ts-expect-error
delete globalThis.location
assert.strictEqual("location" in globalThis, false)
assert.strictEqual(globalThis.location, undefined)
assert.strictEqual(UrlParams.baseUrl(), undefined)

globalThis.location = originalLocation
})
})
})