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(); });