Anything about building binding for Bucklescript

interop

#1

Building BuckleScript bindings is not an easy task for new comers. I’ve struggled a lot when I was building my first bindings. I want to start this thread to make it a realworld reference for Bucklescript binding pattern.

Please follow this pattern when you ask a question:

  1. What JS code do you want to produce?
  2. What you’ve got so far? (Please don’t sayit didn’t work without providing any code. This question is optional)

Writing Bindings
#2

I think this is a great thread to start. Thanks @thangngoc89.

I am trying to write bindings for AWS sdk. I have the CDN in my html, so the AWS constructor object is available as a global variable.

  1. I am trying to compile into js something like this:
AWS.config.region = "us-west-2";
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
   IdentityPoolId: "xxx"
});
var lambda = new AWS.Lambda();
  1. So far I have been able to successful compile the AWS.config.region and var lambda = new AWS.Lambda();, but for AWS.config.credentials I am getting an error mesage:

This type constructor's parameter,awsCognito, can't be found. Is it a typo?

Here’s my reason code so far:

type identityPool = {
  identityPoolId: string
};
[@bs.new] external awsCognito : identityPool => string = "AWS.CognitoIdentityCredentials";
type awsConfig = {. [@bs.set]
                  "region": string,
                  "credentials": awsCognito(identityPool)};
type awsSDK = {. [@bs.set] "config": awsConfig};
[@bs.val] external aws : awsSDK = "AWS";

aws##config##region #= "us-west-2";

aws## config##credentials #= awsCognito({ identityPoolId: "xxx"});

type awsLambdat;
[@bs.new] external awsLambda : unit => awsLambdat = "AWS.Lambda";
let lambda = awsLambda();

What I am stumped on is, I feel that I have provided the parameter as identityPool. Anyone have experience writing bindings for a class constructor taking an argument?


#3

Unfortunately, binding for a constructor is still in the “brainstorming” progress and is not available at the moment.

You can track it here

By the way:

type identityPool = {
  identityPoolId: string
};

is a record. you need a Js.Object by doing something like this:

type identityPool = {
  .
  "_IdentityPoolId": string
};

And the type error is because of this line :

    "credentials": awsCognito(identityPool)}

you can’t mix type and function together


#4

Hey! Thanks for your quick reply @thangngoc89!

“Looks like no one is in my timezone ¯\_(ツ)_/¯
time for bed”

You said this 3 hours ago! Haha. Hope you get some good sleep.

That’s good to know about the open issue.

Here’s some questions:

  1. Whats the difference between:
type identityPool = {
  .
  "_IdentityPoolId": string
};

and

type identityPool = Js.t({
  .
  "_IdentityPoolId": string
});

?

  1. And what role does the underscore in "_IdentityPoolId"? Is that just a convention?
  1. In ReasonReact, action is a user-defined type, and it can take an argument, right?

#5

Hi, have you taken a look at https://redex.github.io/package/@ahrefs/bs-aws-lambda ? You may want to collaborate on that.


#6
  1. Under Reason syntax, they are basically the same thing.

It turns to IdentityPoolId in Js object. See name mangling convetion : https://bucklescript.github.io/bucklescript/Manual.html#_object_label_translation_convention

yes. But judging from your code snippet, i would say you’re mixing them.

Example:

type t('a) = list('a) ;

later:

type a = t(string);

#7

yes I have! I am still new to using AWS, but I would like to write bindings for the aws-js-sdk. I am interested to see how BuckleScript will support bindings for constructors. That will really help, as the SDK is full of constructors.


#8

I’m writing bindings for aws-js-sdk for work. The bindings are of course incomplete as I’m binding only the parts I need. But I can ask to get it open source so that we could gather our efforts if a few people are interested.


#9

Hey @Khady! Any news on open-sourcing bindings for aws-js-sdk from your company? :grinning:


#10

I didn’t receive many responses when I talked about this. So I didn’t take time to cleanup the bindings and release them. The subject still interest me. But I don’t think i will focus on it unless there is some cooperation emerging. It doesn’t worth spending time in it if people are creating other versions of the bindings on their side, as it happens with bs-aws-lambda.


#11

@Khady I’m responding, sir. I tried reading through bs-aws and applying ot aws-appsync but cant quite get it to do anything yet even though its compiling. Would love to see you all share that if its feasible. Thanks!


#12

I am also writing aws-js-sdk for work. I am interested in collaborating with anyone who is doing the same. I am still new to Reason and struggling with the bindings.