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

fix: changes for supporting form data type in api #257

Merged
merged 13 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading