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
Prev Previous commit
wip
  • Loading branch information
tim-smart committed Apr 16, 2024
commit 98ff8b580e9237704e8d0d3645ac5112dd049e6a
2 changes: 1 addition & 1 deletion .changeset/stale-bugs-roll.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@effect/platform": patch
---

Fix HTTP Client's compatibility with Deno due to globalThis.location.
Fix UrlParams.makeUrl when globalThis.location is set to `undefined`
10 changes: 1 addition & 9 deletions packages/platform/src/Http/UrlParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,7 @@ export const makeUrl = <E>(url: string, params: UrlParams, onError: (e: unknown)
catch: onError
})

/**
* Get the base URL out of `globalThis.location`.
*
* @internal
*/
export const baseUrl = (): string | undefined => {
// Need to check both "in" and "undefined" for location to support Deno.
// As Deno has "globalThis.location" defined but with value "undefined" by default.
// See https://docs.deno.com/runtime/manual/runtime/location_api
const baseUrl = (): string | undefined => {
if ("location" in globalThis && globalThis.location !== undefined) {
return location.origin + location.pathname
}
Expand Down
36 changes: 18 additions & 18 deletions packages/platform/test/Http/UrlParams.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import * as UrlParams from "@effect/platform/Http/UrlParams"
import { assert, describe, it } from "vitest"
import { assert, describe, it } from "@effect/vitest"
import { Effect } from "effect"

describe("UrlParams", () => {
describe("baseUrl", () => {
it("should return undefined when `location` is not in `globalThis` or `globalThis.location` is undefined", () => {
const originalLocation = globalThis.location
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
assert.strictEqual("location" in globalThis, true)
assert.strictEqual(globalThis.location, undefined)
assert.strictEqual(UrlParams.baseUrl(), undefined)
// `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
assert.strictEqual("location" in globalThis, false)
assert.strictEqual(globalThis.location, undefined)
assert.strictEqual(UrlParams.baseUrl(), undefined)
// `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
})
globalThis.location = originalLocation
}))
})
})