Skip to content

Commit

Permalink
Merge branch 'main' into utils-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Pritish Budhiraja authored Jan 23, 2024
2 parents 19b1102 + 84c283b commit 457fe84
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
57 changes: 19 additions & 38 deletions src/hooks/AuthHooks.res
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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 _=?,
Expand All @@ -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 => {
Expand Down
19 changes: 17 additions & 2 deletions src/screens/HyperSwitch/APIUtils/APIUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ 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 @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion src/screens/HyperSwitch/Order/OrderUIUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,14 @@ let setData = (

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

0 comments on commit 457fe84

Please sign in to comment.