From e4bc187f60067154faac83d5f965bc6a61419511 Mon Sep 17 00:00:00 2001 From: Michael Heuberger Date: Sun, 3 Nov 2024 11:47:57 +1300 Subject: [PATCH] better handling of window global --- package-lock.json | 8 ++++++++ package.json | 1 + src/resource.ts | 11 ++++++----- src/util/Browser.ts | 6 +----- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 91a5e012..aab143a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43,6 +43,7 @@ "@storybook/theming": "8.4.1", "@tsconfig/node22": "22.0.0", "@tsconfig/strictest": "2.0.5", + "@types/defined": "1.0.2", "@types/node": "22.8.6", "@types/superagent": "8.1.9", "@types/ua-parser-js": "0.7.39", @@ -3333,6 +3334,13 @@ "@types/node": "*" } }, + "node_modules/@types/defined": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@types/defined/-/defined-1.0.2.tgz", + "integrity": "sha512-fj9QIrpkwMHVgCKFF75xR59l43r6DbmBCcW0mJ0Rx+hrtoXrHl1uoZNLi6NRk/Q6TtqnXsLV+qN6A8fE0b5JgA==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/eslint": { "version": "9.6.1", "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-9.6.1.tgz", diff --git a/package.json b/package.json index cae0fa99..b9452449 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,7 @@ "@storybook/theming": "8.4.1", "@tsconfig/node22": "22.0.0", "@tsconfig/strictest": "2.0.5", + "@types/defined": "1.0.2", "@types/node": "22.8.6", "@types/superagent": "8.1.9", "@types/ua-parser-js": "0.7.39", diff --git a/src/resource.ts b/src/resource.ts index ef1d8fd5..e3843aad 100644 --- a/src/resource.ts +++ b/src/resource.ts @@ -8,13 +8,14 @@ import Response from "superagent/lib/node/response"; import VideomailError from "./util/error/VideomailError"; import { FormInputs, FormMethod } from "./wrappers/form"; -const timezoneId = window.Intl.DateTimeFormat().resolvedOptions().timeZone; - class Resource { private readonly options: VideomailClientOptions; + private readonly timezoneId: string; constructor(options: VideomailClientOptions) { this.options = options; + + this.timezoneId = window.Intl.DateTimeFormat().resolvedOptions().timeZone; } private applyDefaultValue(videomail: PartialVideomail, name: string) { @@ -45,7 +46,7 @@ class Resource { const request = await superagent("get", url) .type("json") .set("Accept", "application/json") - .set("Timezone-Id", timezoneId) + .set("Timezone-Id", this.timezoneId) .set(Constants.SITE_NAME_LABEL, this.options.siteName) .timeout(this.options.timeouts.connection); @@ -69,7 +70,7 @@ class Resource { try { const request = await superagent(method, url) .query(queryParams) - .set("Timezone-Id", timezoneId) + .set("Timezone-Id", this.timezoneId) .send(videomail) .timeout(this.options.timeouts.connection); @@ -158,7 +159,7 @@ class Resource { const res = await superagent .post(url) .type(formType) - .set("Timezone-Id", timezoneId) + .set("Timezone-Id", this.timezoneId) .send(formData) .timeout(this.options.timeouts.connection); diff --git a/src/util/Browser.ts b/src/util/Browser.ts index b6f0270f..34c44f3f 100644 --- a/src/util/Browser.ts +++ b/src/util/Browser.ts @@ -16,11 +16,7 @@ class Browser { public constructor(options: VideomailClientOptions) { this.options = options; - const ua = defined( - options.fakeUaString, - typeof window !== "undefined" && window.navigator.userAgent, - "", - ); + const ua = defined(options.fakeUaString, window.navigator.userAgent, ""); const userAgentParser = new UAParser(ua);