I have some rescript code that unwraps form data and saves it to a gist. The Form handles three fields.
{
role: option<string>,
inviteLink: option<string>,
sponsorshipAddress: option<string>
}
Is there any better way to clean up the unwrapping part of this code so I can improve readability.
let action: Remix.actionFunction<'a> = async ({request, params}) => {
open Webapi.Fetch
let data = await Request.formData(request)
let headers = HeadersInit.make({
"Authorization": `Bot ${botToken}`,
})
let init = RequestInit.make(~method_=Patch, ~headers, ())
/// Unwrapping
let role = data->Webapi.FormData.get("role")
let inviteLink = data->Webapi.FormData.get("inviteLink")
let sponsorshipAddress = data->Webapi.FormData.get("sponsorshipAddress")
//Unwrapping
switch (role, inviteLink, sponsorshipAddress) {
| (Some(role), Some(inviteLink), Some(sponsorshipAddress)) =>
switch (
role->Webapi.FormData.EntryValue.classify,
inviteLink->Webapi.FormData.EntryValue.classify,
sponsorshipAddress->Webapi.FormData.EntryValue.classify,
) {
// Unwrapping
| (#String(role), #String(inviteLink), #String(sponsorshipAddress)) => {
open WebUtils_Gist
let config = makeGistConfig(
~id=Remix.process["env"]["GIST_ID"],
~name="guildData.json",
~token=Remix.process["env"]["GITHUB_ACCESS_TOKEN"],
)
let content = await ReadGist.content(~config, ~decoder=Shared.Decode.Gist.brightIdGuilds)
let prevEntry = switch content->Js.Dict.get(guildId) {
| Some(entry) => entry
| None => GuildDoesNotExist(guildId)->raise
}
await UpdateGist.updateEntry(
~content,
~key=guildId,
~config,
~entry={
...prevEntry,
role: Some(role),
inviteLink: Some(inviteLink),
sponsorshipAddress: Some(sponsorshipAddress),
},
)
}
| _ =>
Remix.redirect(`/guilds/${guildId}/admin`)->ignore
EmptySubmit->raise
}
| _ =>
Remix.redirect(`/guilds/${guildId}/admin`)->ignore
EmptySubmit->raise
}
}