Skip to content

Commit

Permalink
fix: update useApiFetcher function
Browse files Browse the repository at this point in the history
  • Loading branch information
Riddhiagrawal001 committed Jan 23, 2024
1 parent 7797d20 commit 31bdb2c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 36 deletions.
16 changes: 3 additions & 13 deletions src/hooks/AuthHooks.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type sessionStorage = {
external dictToObj: Js.Dict.t<'a> => {..} = "%identity"
@val external atob: string => string = "atob"

let getHeaders = (~uri, ~headers, ~isFromFormData, ()) => {
let getHeaders = (~uri, ~headers, ()) => {
let hyperSwitchToken = LocalStorage.getItem("login")->Js.Nullable.toOption
let isMixpanel = uri->String.includes("mixpanel")

Expand All @@ -20,11 +20,6 @@ let getHeaders = (~uri, ~headers, ~isFromFormData, ()) => {
"accept": "application/json",
}
Fetch.HeadersInit.make(headerObj)
} else if isFromFormData {
let headerObj = {
"Authorization": `Bearer ${hyperSwitchToken->Belt.Option.getWithDefault("")}`,
}
Fetch.HeadersInit.make(headerObj)
} else {
let headerObj = headers->Dict.get("api-key")->Option.getWithDefault("")->String.length > 0

Expand Down Expand Up @@ -86,7 +81,6 @@ let useApiFetcher = () => {
(
uri,
~bodyStr: string="",
~bodyFormData=None,
~headers=Dict.make(),
~bodyHeader as _=?,
~method_: Fetch.requestMethod,
Expand All @@ -104,11 +98,7 @@ let useApiFetcher = () => {

let body = switch method_ {
| Get => resolve(None)
| _ =>
switch bodyFormData {
| Some(formDataVal) => resolve(Some(Fetch.BodyInit.makeWithFormData(formDataVal)))
| None => resolve(Some(Fetch.BodyInit.make(bodyStr)))
}
| _ => resolve(Some(Fetch.BodyInit.make(bodyStr)))
}

body->then(body => {
Expand All @@ -119,7 +109,7 @@ let useApiFetcher = () => {
~method_,
~body?,
~credentials=SameOrigin,
~headers=getHeaders(~headers, ~uri, ~isFromFormData=bodyFormData->Option.isSome, ()),
~headers=getHeaders(~headers, ~uri, ()),
(),
),
)
Expand Down
11 changes: 2 additions & 9 deletions src/screens/HyperSwitch/APIUtils/APIUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ let handleLogout = async (
~fetchApi as _: (
Js.String2.t,
~bodyStr: string=?,
~bodyFormData: option<Fetch.formData>=?,
~headers: Js.Dict.t<Js.String2.t>=?,
~bodyHeader: Js.Dict.t<Js.Json.t>=?,
~method_: Fetch.requestMethod,
Expand Down Expand Up @@ -388,15 +387,9 @@ let useUpdateMethod = (~showErrorToast=true, ()) => {
},
})

async (url, body, method, ~bodyFormData=?, ()) => {
async (url, body, method, ()) => {
try {
let res = await fetchApi(
url,
~method_=method,
~bodyStr=body->Js.Json.stringify,
~bodyFormData,
(),
)
let res = await fetchApi(url, ~method_=method, ~bodyStr=body->Js.Json.stringify, ())
await responseHandler(
~res,
~showErrorToast,
Expand Down
8 changes: 1 addition & 7 deletions src/screens/HyperSwitch/Order/OrderUIUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,7 @@ let setData = (

let getOrdersList = async (
filterValueJson,
~updateDetails: (
string,
Js.Json.t,
Fetch.requestMethod,
~bodyFormData: Fetch.formData=?,
unit,
) => promise<Js.Json.t>,
~updateDetails: (string, Js.Json.t, Fetch.requestMethod, unit) => promise<Js.Json.t>,
~setOrdersData,
~previewOnly,
~setScreenState,
Expand Down
8 changes: 1 addition & 7 deletions src/screens/HyperSwitch/Refunds/RefundUtils.res
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
let getRefundsList = async (
filterValueJson,
~updateDetails: (
string,
Js.Json.t,
Fetch.requestMethod,
~bodyFormData: Fetch.formData=?,
unit,
) => promise<Js.Json.t>,
~updateDetails: (string, Js.Json.t, Fetch.requestMethod, unit) => promise<Js.Json.t>,
~setRefundsData,
~setScreenState,
~offset,
Expand Down

0 comments on commit 31bdb2c

Please sign in to comment.