From 66f655f18e2859fa93272372db5e893fa8d8e2d0 Mon Sep 17 00:00:00 2001 From: sakksham7 <130480324+sakksham7@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:04:02 +0530 Subject: [PATCH] refactor: moving declarations in window into a sub-module (#503) Co-authored-by: Pritish Budhiraja --- src/CardTheme.res | 2 +- src/Hooks/Fetcher.res | 3 +- src/LoaderController.res | 2 +- src/Utilities/PaymentBody.res | 5 +- src/Window.res | 63 ++++++++++++++------------ src/orca-loader/Hyper.res | 4 +- src/orca-log-catcher/ErrorBoundary.res | 6 +-- src/orca-log-catcher/OrcaLogger.res | 31 +++++++------ 8 files changed, 61 insertions(+), 55 deletions(-) diff --git a/src/CardTheme.res b/src/CardTheme.res index 093d67adf..fb2f8a97b 100644 --- a/src/CardTheme.res +++ b/src/CardTheme.res @@ -72,7 +72,7 @@ type recoilConfig = { let getLocaleObject = async string => { try { let locale = if string == "auto" { - Window.language + Window.Navigator.language } else { string } diff --git a/src/Hooks/Fetcher.res b/src/Hooks/Fetcher.res index 7c193f906..ebd789fdb 100644 --- a/src/Hooks/Fetcher.res +++ b/src/Hooks/Fetcher.res @@ -1,4 +1,3 @@ -@val @scope(("window", "location")) external hostname: string = "hostname" type match type pathname = {match: match} type url = {pathname: pathname} @@ -9,7 +8,7 @@ let useFetcher = fileName => { let (optionalJson, setJson) = React.useState(() => None) React.useEffect(() => { open Promise - Fetch.get(`${hostname}/json/${fileName}.json`) + Fetch.get(`${Window.Location.hostname}/json/${fileName}.json`) ->then(Fetch.Response.json) ->thenResolve(json => { setJson(_ => Some(json)) diff --git a/src/LoaderController.res b/src/LoaderController.res index 05ccc78f0..8aee5a46a 100644 --- a/src/LoaderController.res +++ b/src/LoaderController.res @@ -123,7 +123,7 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger, ~initTime setConfig(_ => { config: { appearance, - locale: config.locale === "auto" ? Window.language : config.locale, + locale: config.locale === "auto" ? Window.Navigator.language : config.locale, fonts: config.fonts, clientSecret: config.clientSecret, ephemeralKey: config.ephemeralKey, diff --git a/src/Utilities/PaymentBody.res b/src/Utilities/PaymentBody.res index e8c10b1e5..bf5ce5217 100644 --- a/src/Utilities/PaymentBody.res +++ b/src/Utilities/PaymentBody.res @@ -1,6 +1,3 @@ -@val @scope("window") -external btoa: string => string = "btoa" - let billingDetailsTuple = ( ~fullName, ~email, @@ -502,7 +499,7 @@ let applePayBody = (~token, ~connectors) => { ->Dict.get("paymentData") ->Option.getOr(Dict.make()->JSON.Encode.object) ->JSON.stringify - ->btoa + ->Window.btoa dict->Dict.set("paymentData", paymentDataString->JSON.Encode.string) let applePayBody = [ diff --git a/src/Window.res b/src/Window.res index 4813fedac..10d4907aa 100644 --- a/src/Window.res +++ b/src/Window.res @@ -103,48 +103,55 @@ external setHyper: (window, Types.hyperInstance) => unit = "HyperMethod" type packageJson = {version: string} @module("/package.json") @val external packageJson: packageJson = "default" let version = packageJson.version -@val @scope("navigator") -external browserName: string = "appName" -@val @scope("navigator") -external browserVersion: string = "appVersion" +module Navigator = { + @val @scope("navigator") + external browserName: string = "appName" -@val @scope("navigator") -external platform: string = "platform" + @val @scope("navigator") + external browserVersion: string = "appVersion" -@val @scope("navigator") -external userAgent: string = "userAgent" + @val @scope(("window", "navigator")) + external language: string = "language" -@val @scope("navigator") -external sendBeacon: (string, string) => unit = "sendBeacon" + @val @scope(("window", "navigator")) + external platform: string = "platform" -@val @scope("navigator") -external language: string = "language" + @val @scope(("window", "navigator")) + external userAgent: string = "userAgent" -@val @scope(("window", "location")) -external hostname: string = "hostname" - -@val @scope(("window", "location")) -external origin: string = "origin" - -@val @scope(("window", "location")) -external protocol: string = "protocol" + @val @scope("navigator") + external sendBeacon: (string, string) => unit = "sendBeacon" +} -@val @scope(("window", "location")) -external pathname: string = "pathname" +module Location = { + @val @scope(("window", "location")) + external replace: string => unit = "replace" -let isSandbox = hostname === "beta.hyperswitch.io" + @val @scope(("window", "location")) + external hostname: string = "hostname" -let isInteg = hostname === "dev.hyperswitch.io" + @val @scope(("window", "location")) + external origin: string = "origin" -let isProd = hostname === "checkout.hyperswitch.io" + @val @scope(("window", "location")) + external protocol: string = "protocol" -module Location = { - @val @scope(("window", "location")) external replace: string => unit = "replace" + @val @scope(("window", "location")) + external pathname: string = "pathname" } module Element = { @get external clientWidth: Dom.element => int = "clientWidth" } -let hrefWithoutSearch = origin ++ pathname +@val @scope("window") +external btoa: string => string = "btoa" + +let hrefWithoutSearch = Location.origin ++ Location.pathname + +let isSandbox = Location.hostname === "beta.hyperswitch.io" + +let isInteg = Location.hostname === "dev.hyperswitch.io" + +let isProd = Location.hostname === "checkout.hyperswitch.io" diff --git a/src/orca-loader/Hyper.res b/src/orca-loader/Hyper.res index 58e66940e..24ef89d39 100644 --- a/src/orca-loader/Hyper.res +++ b/src/orca-loader/Hyper.res @@ -136,10 +136,10 @@ let make = (publishableKey, options: option, analyticsInfo: optionSentry.sentryLogger - let isSecure = Window.protocol === "https:" + let isSecure = Window.Location.protocol === "https:" let isLocal = ["localhost", "127.0.0.1"] - ->Array.find(url => Window.hostname->String.includes(url)) + ->Array.find(url => Window.Location.hostname->String.includes(url)) ->Option.isSome if !isSecure && !isLocal { manageErrorWarning(HTTP_NOT_ALLOWED, ~dynamicStr=Window.hrefWithoutSearch, ~logger, ()) diff --git a/src/orca-log-catcher/ErrorBoundary.res b/src/orca-log-catcher/ErrorBoundary.res index e32213282..aee7de189 100644 --- a/src/orca-log-catcher/ErrorBoundary.res +++ b/src/orca-log-catcher/ErrorBoundary.res @@ -127,7 +127,7 @@ module ErrorCard = { let beaconApiCall = data => { if data->Array.length > 0 { let logData = data->Array.map(OrcaLogger.logFileToObj)->JSON.Encode.array->JSON.stringify - Window.sendBeacon(GlobalVars.logEndpoint, logData) + Window.Navigator.sendBeacon(GlobalVars.logEndpoint, logData) } } @@ -148,8 +148,8 @@ module ErrorCard = { merchantId: "", browserName: OrcaLogger.arrayOfNameAndVersion->Array.get(0)->Option.getOr("Others"), browserVersion: OrcaLogger.arrayOfNameAndVersion->Array.get(1)->Option.getOr("0"), - platform: Window.platform, - userAgent: Window.userAgent, + platform: Window.Navigator.platform, + userAgent: Window.Navigator.userAgent, appId: "", eventName: SDK_CRASH, latency: "", diff --git a/src/orca-log-catcher/OrcaLogger.res b/src/orca-log-catcher/OrcaLogger.res index f673588ef..1489501ac 100644 --- a/src/orca-log-catcher/OrcaLogger.res +++ b/src/orca-log-catcher/OrcaLogger.res @@ -479,7 +479,7 @@ let browserDetect = content => { } } -let arrayOfNameAndVersion = String.split(Window.userAgent->browserDetect, "-") +let arrayOfNameAndVersion = String.split(Window.Navigator.userAgent->browserDetect, "-") let make = ( ~sessionId=?, @@ -542,14 +542,17 @@ let make = ( let counter = eventName->calculateAndUpdateCounterHook if GlobalVars.enableLogging && counter <= maxLogsPushedPerEventName { switch loggingLevel { - | DEBUG => log->Array.push(mainLogFile, _)->ignore + | DEBUG => log->(Array.push(mainLogFile, _))->ignore | INFO => [INFO, WARNING, ERROR]->Array.includes(log.logType) - ? log->Array.push(mainLogFile, _)->ignore + ? log->(Array.push(mainLogFile, _))->ignore : () | WARNING => - [WARNING, ERROR]->Array.includes(log.logType) ? log->Array.push(mainLogFile, _)->ignore : () - | ERROR => [ERROR]->Array.includes(log.logType) ? log->Array.push(mainLogFile, _)->ignore : () + [WARNING, ERROR]->Array.includes(log.logType) + ? log->(Array.push(mainLogFile, _))->ignore + : () + | ERROR => + [ERROR]->Array.includes(log.logType) ? log->(Array.push(mainLogFile, _))->ignore : () | SILENT => () } } @@ -558,7 +561,7 @@ let make = ( let beaconApiCall = data => { if data->Array.length > 0 { let logData = data->Array.map(logFileToObj)->JSON.Encode.array->JSON.stringify - Window.sendBeacon(GlobalVars.logEndpoint, logData) + Window.Navigator.sendBeacon(GlobalVars.logEndpoint, logData) } } @@ -694,8 +697,8 @@ let make = ( merchantId: merchantId.contents, browserName: arrayOfNameAndVersion->Array.get(0)->Option.getOr("Others"), browserVersion: arrayOfNameAndVersion->Array.get(1)->Option.getOr("0"), - platform: Window.platform, - userAgent: Window.userAgent, + platform: Window.Navigator.platform, + userAgent: Window.Navigator.userAgent, appId: "", eventName, latency, @@ -752,8 +755,8 @@ let make = ( merchantId: merchantId.contents, browserName: arrayOfNameAndVersion->Array.get(0)->Option.getOr("Others"), browserVersion: arrayOfNameAndVersion->Array.get(1)->Option.getOr("0"), - platform: Window.platform, - userAgent: Window.userAgent, + platform: Window.Navigator.platform, + userAgent: Window.Navigator.userAgent, appId: "", eventName, latency, @@ -800,8 +803,8 @@ let make = ( merchantId: merchantId.contents, browserName: arrayOfNameAndVersion->Array.get(0)->Option.getOr("Others"), browserVersion: arrayOfNameAndVersion->Array.get(1)->Option.getOr("0"), - platform: Window.platform, - userAgent: Window.userAgent, + platform: Window.Navigator.platform, + userAgent: Window.Navigator.userAgent, appId: "", eventName, latency, @@ -835,8 +838,8 @@ let make = ( merchantId: merchantId.contents, browserName: arrayOfNameAndVersion->Array.get(0)->Option.getOr("Others"), browserVersion: arrayOfNameAndVersion->Array.get(1)->Option.getOr("0"), - platform: Window.platform, - userAgent: Window.userAgent, + platform: Window.Navigator.platform, + userAgent: Window.Navigator.userAgent, appId: "", latency, paymentMethod: "",