Hi folks,
I’m trying to finish my simple Nodejs backend with Rescript, and so far the whole process is successful expect one element.
I use Passport local authentication. It requires to define callback function:
(username, password, done) => ()
where done
can be:
// success
done(null, user)
or
// failure
done(null, false)
or
//error
done(err)
It wouldn’t be a problem for polymorphic functions, but unfortunately, I have callbacks here. I thought I will be able to handle it with @unwrap
but it doesn’t work.
I typed done
as:
type passType<'a> =
@unwrap
[
| #Bool(bool)
| #Str(string)
]
type done<'a, 'b> = (
.
Js.Nullable.t<'b>,
passType<'a>
) => unit
and later used it like this:
let strategy = Passport.makeLocalStrategy((. username, password, done) => {
UserDao.Get.verifyUser(~name=username, ~password)
->thenResolve(user => {
switch user {
| Ok(user) => done(. Js.Nullable.null, #Str(user.id))
| Error(_) => {
Js.log("error")
done(. Js.Nullable.null, #Bool(false))
}
}
})
->ignore
})
but unfortunately, @unwrap doesn’t work here and the output code for #Bool
looks like this:
return done(null, {
NAME: "Bool",
VAL: false
});
To be honest I have no idea how to handle it. I could write the whole module as %%raw
but it would look pretty bad. Is it even possible with Rescript?