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

refactor: moving declarations in window into a sub-module #503

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
3 changes: 1 addition & 2 deletions src/Hooks/Fetcher.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@val @scope(("window", "location")) external hostname: string = "hostname"
type match
type pathname = {match: match}
type url = {pathname: pathname}
Expand All @@ -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))
Expand Down
5 changes: 1 addition & 4 deletions src/Utilities/PaymentBody.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
@val @scope("window")
external btoa: string => string = "btoa"

let billingDetailsTuple = (
~fullName,
~email,
Expand Down Expand Up @@ -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 = [
Expand Down
60 changes: 35 additions & 25 deletions src/Window.res
Original file line number Diff line number Diff line change
Expand Up @@ -103,45 +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(("window", "location"))
external hostname: string = "hostname"

@val @scope(("window", "location"))
external origin: string = "origin"
@val @scope(("window", "navigator"))
external userAgent: string = "userAgent"

@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"
4 changes: 2 additions & 2 deletions src/orca-loader/Hyper.res
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ let make = (publishableKey, options: option<JSON.t>, analyticsInfo: option<JSON.
)
}
}->Sentry.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, ())
Expand Down
6 changes: 3 additions & 3 deletions src/orca-log-catcher/ErrorBoundary.res
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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: "",
Expand Down
31 changes: 17 additions & 14 deletions src/orca-log-catcher/OrcaLogger.res
Original file line number Diff line number Diff line change
Expand Up @@ -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=?,
Expand Down Expand Up @@ -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 => ()
}
}
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: "",
Expand Down
Loading