diff --git a/src/hooks/AuthHooks.res b/src/hooks/AuthHooks.res index bd4440a0b..61ec1c785 100644 --- a/src/hooks/AuthHooks.res +++ b/src/hooks/AuthHooks.res @@ -14,47 +14,23 @@ let getHeaders = (~uri, ~headers, ()) => { let hyperSwitchToken = LocalStorage.getItem("login")->Js.Nullable.toOption let isMixpanel = uri->String.includes("mixpanel") - if isMixpanel { - let headerObj = { - "Content-Type": "application/x-www-form-urlencoded", - "accept": "application/json", - } - Fetch.HeadersInit.make(headerObj) + let headerObj = if isMixpanel { + [ + ("Content-Type", "application/x-www-form-urlencoded"), + ("accept", "application/json"), + ]->Dict.fromArray } else { - let headerObj = headers->Dict.get("api-key")->Option.getWithDefault("")->String.length > 0 - - if headerObj { - let headerObj = { - "Content-Type": "application/json", - "api-key": headers->Dict.get("api-key")->Option.getWithDefault(""), + let res = switch hyperSwitchToken { + | Some(token) => { + headers->Dict.set("authorization", `Bearer ${token}`) + headers } - Fetch.HeadersInit.make(headerObj) - } else { - switch hyperSwitchToken { - | Some(token) => - if token !== "" { - let headerObj = { - "Content-Type": "application/json", - "Authorization": `Bearer ${hyperSwitchToken->Option.getWithDefault("")}`, - "api-key": "hyperswitch", - } - - Fetch.HeadersInit.make(headerObj) - } else { - let headerObj = { - "Content-Type": "application/json", - } - Fetch.HeadersInit.make(headerObj) - } - | None => - let headerObj = { - "Content-Type": "application/json", - } - Fetch.HeadersInit.make(headerObj) - } + | None => headers } + res } + Fetch.HeadersInit.make(headerObj->dictToObj) } @val @scope(("window", "location")) @@ -81,7 +57,8 @@ let useApiFetcher = () => { ( uri, ~bodyStr: string="", - ~headers=Dict.make(), + ~bodyFormData=None, + ~headers=[("Content-Type", "application/json")]->Dict.fromArray, ~bodyHeader as _=?, ~method_: Fetch.requestMethod, ~authToken as _=?, @@ -98,7 +75,11 @@ let useApiFetcher = () => { let body = switch method_ { | Get => resolve(None) - | _ => resolve(Some(Fetch.BodyInit.make(bodyStr))) + | _ => + switch bodyFormData { + | Some(formDataVal) => resolve(Some(Fetch.BodyInit.makeWithFormData(formDataVal))) + | None => resolve(Some(Fetch.BodyInit.make(bodyStr))) + } } body->then(body => { diff --git a/src/screens/HyperSwitch/APIUtils/APIUtils.res b/src/screens/HyperSwitch/APIUtils/APIUtils.res index b466c8d4a..c3195c77e 100644 --- a/src/screens/HyperSwitch/APIUtils/APIUtils.res +++ b/src/screens/HyperSwitch/APIUtils/APIUtils.res @@ -203,6 +203,7 @@ let handleLogout = async ( ~fetchApi as _: ( Js.String2.t, ~bodyStr: string=?, + ~bodyFormData: option=?, ~headers: Js.Dict.t=?, ~bodyHeader: Js.Dict.t=?, ~method_: Fetch.requestMethod, @@ -387,9 +388,23 @@ let useUpdateMethod = (~showErrorToast=true, ()) => { }, }) - async (url, body, method, ()) => { + async ( + url, + body, + method, + ~bodyFormData=?, + ~headers=[("Content-Type", "application/json")]->Dict.fromArray, + (), + ) => { try { - let res = await fetchApi(url, ~method_=method, ~bodyStr=body->Js.Json.stringify, ()) + let res = await fetchApi( + url, + ~method_=method, + ~bodyStr=body->Js.Json.stringify, + ~bodyFormData, + ~headers, + (), + ) await responseHandler( ~res, ~showErrorToast, diff --git a/src/screens/HyperSwitch/Order/OrderUIUtils.res b/src/screens/HyperSwitch/Order/OrderUIUtils.res index a0b9174f5..f34906551 100644 --- a/src/screens/HyperSwitch/Order/OrderUIUtils.res +++ b/src/screens/HyperSwitch/Order/OrderUIUtils.res @@ -213,7 +213,14 @@ let setData = ( let getOrdersList = async ( filterValueJson, - ~updateDetails: (string, Js.Json.t, Fetch.requestMethod, unit) => promise, + ~updateDetails: ( + string, + Js.Json.t, + Fetch.requestMethod, + ~bodyFormData: Fetch.formData=?, + ~headers: Js.Dict.t<'a>=?, + unit, + ) => promise, ~setOrdersData, ~previewOnly, ~setScreenState, diff --git a/src/screens/HyperSwitch/Refunds/RefundUtils.res b/src/screens/HyperSwitch/Refunds/RefundUtils.res index c7b786427..e0267d442 100644 --- a/src/screens/HyperSwitch/Refunds/RefundUtils.res +++ b/src/screens/HyperSwitch/Refunds/RefundUtils.res @@ -1,6 +1,13 @@ let getRefundsList = async ( filterValueJson, - ~updateDetails: (string, Js.Json.t, Fetch.requestMethod, unit) => promise, + ~updateDetails: ( + string, + Js.Json.t, + Fetch.requestMethod, + ~bodyFormData: Fetch.formData=?, + ~headers: Js.Dict.t<'a>=?, + unit, + ) => promise, ~setRefundsData, ~setScreenState, ~offset,