diff --git a/src/hooks/AuthHooks.res b/src/hooks/AuthHooks.res index 8c1266d9d..29995a58a 100644 --- a/src/hooks/AuthHooks.res +++ b/src/hooks/AuthHooks.res @@ -10,44 +10,35 @@ type sessionStorage = { external dictToObj: Js.Dict.t<'a> => {..} = "%identity" @val external atob: string => string = "atob" +let headersForXFeature = (~uri, ~headers) => { + if uri->String.includes("lottie-files") || uri->String.includes("config/merchant-access") { + headers->Dict.set("Content-Type", `application/json`) + } else { + headers->Dict.set("x-feature", "hyperswitch-custom") + } +} + let getHeaders = (~uri, ~headers, ~xFeatureRoute, ()) => { let hyperSwitchToken = LocalStorage.getItem("login")->Js.Nullable.toOption let isMixpanel = uri->String.includes("mixpanel") - let headersForXFeature = (~token) => - if ( - uri->Js.String2.includes("lottie-files") || uri->Js.String2.includes("config/merchant-access") - ) { - headers->Dict.set("Content-Type", `application/json`) - headers->Dict.set("authorization", `Bearer ${token}`) - headers->Dict.set("api-key", `hyperswitch`) - headers - } else { - headers->Dict.set("authorization", `Bearer ${token}`) - headers->Dict.set("api-key", `hyperswitch`) - headers->Dict.set("x-feature", "hyperswitch-custom") - headers - } - let headerObj = if isMixpanel { [ ("Content-Type", "application/x-www-form-urlencoded"), ("accept", "application/json"), ]->Dict.fromArray } else { - let headersDict = switch hyperSwitchToken { - | Some(token) => - xFeatureRoute - ? headersForXFeature(~token) - : { - headers->Dict.set("authorization", `Bearer ${token}`) - headers->Dict.set("api-key", `hyperswitch`) - headers - } - - | None => xFeatureRoute ? headersForXFeature(~token="") : headers + if xFeatureRoute { + headersForXFeature(~headers, ~uri) + } + switch hyperSwitchToken { + | Some(token) => { + headers->Dict.set("authorization", `Bearer ${token}`) + headers->Dict.set("api-key", `hyperswitch`) + } + | None => () } - headersDict + headers } Fetch.HeadersInit.make(headerObj->dictToObj) }