From 511eda1f35013dbfda8321fca6d2436ef344f266 Mon Sep 17 00:00:00 2001 From: Christian Cook <3473396+CookieCookson@users.noreply.github.com> Date: Mon, 15 Apr 2024 18:03:15 +0100 Subject: [PATCH] fix: Improve btoa detection --- src/helpers/toBasicAuth/toBasicAuth.ts | 2 +- src/helpers/toBasicAuth/toBasicAuth.unit.test.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/helpers/toBasicAuth/toBasicAuth.ts b/src/helpers/toBasicAuth/toBasicAuth.ts index 77849062..4c40e492 100644 --- a/src/helpers/toBasicAuth/toBasicAuth.ts +++ b/src/helpers/toBasicAuth/toBasicAuth.ts @@ -1,6 +1,6 @@ export function toBasicAuth(username: string, password: string): string { const credentials = `${username}:${password}`; - if (typeof window !== "undefined" && window.btoa) { + if (typeof btoa === "function") { return `Basic ${btoa(credentials)}`; } else if (typeof Buffer !== "undefined") { return `Basic ${Buffer.from(credentials, "binary").toString("base64")}`; diff --git a/src/helpers/toBasicAuth/toBasicAuth.unit.test.ts b/src/helpers/toBasicAuth/toBasicAuth.unit.test.ts index 81e2e3d2..a9b51595 100644 --- a/src/helpers/toBasicAuth/toBasicAuth.unit.test.ts +++ b/src/helpers/toBasicAuth/toBasicAuth.unit.test.ts @@ -23,8 +23,7 @@ test("converts username and password into Basic Auth header", () => { }); test("throws error if environment not supported", () => { - if (typeof window !== "undefined") window.btoa = undefined; - // eslint-disable-next-line no-global-assign + if (typeof btoa === "function") btoa = undefined; if (Buffer) Buffer = undefined; expect(() => toBasicAuth("", "")).toThrowError(); });