Best practices for bindings to JS libraries

Hello guys, happy to have discovered Rescript.

The hardest part of switching to Rescript, at least for me, is the lack of a clear how-to on how to bind. What is the best current practice to bind for example a Jsonwebtoken library. I thought at first I needed to use the @send because the verify and sign are methods.

Here’s what I came up with… Is this correct? I seriously doubt it:). Help will be appreciated.

module JsJwt = {
external sign: ({…}, string) => string = “sign”
@module(“jsonwebtoken”) external verify: (string, string) => bool = “verify”

// 1) The idea is to do Jwt.sign({any object}, secret)
let token = JsJwt.sign({“foo”: “bar”}, “12345”)
Js.log(“The actual token generated” ++ token)

// 2) And obviously do Jwt.verify()
let isCorrect = JsJwt.verify(token, “12345”)
Js.log(isCorrect ? “true” : “false”)

I also put it on the playground playground

Hi @el1t1st I would probably do the same as what you have here.

One minor tweak might be to have the sign method return a token type. It’s still a string in JS, but might provide a little extra type safety in ReScript when passing the token to other functions.

module JsJwt = {
  type token

  external sign: ({..}, string) => token = "sign"

  external verify: (token, string) => bool = "verify"
1 Like