It did not generate the right code


#1

Hi all

I have the following code:
type keycloak;
type kcInit;

[@bs.deriving abstract]
type initConfig = {onLoad: string};

type pool = {
  token: string,
  subject: string,
  idToken: string,
};

[@bs.module "keycloak-js"] external keycloak : string => keycloak = "";
[@bs.send] external init : (keycloak, initConfig) => kcInit = "";
[@bs.send] external success : (kcInit, bool => unit) => kcInit = "";
[@bs.send] external error : (kcInit, unit => unit) => unit = "";

let authorize = (kcConfig: string) =>
  Js.Promise.make((~resolve, ~reject) =>
    keycloak(kcConfig)
    |. init(initConfig(~onLoad="login-required"))
    |. success(autheticated =>
         autheticated ?
           resolve(. "authenticated") :
           reject(. Js.Exn.raiseError("Your are not authenticated!"))
       )
    |. error(() =>
         reject(.
           Js.Exn.raiseError("There is something wrong with keycloak"),
         )
       )
  );

it compiles to:

// Generated by BUCKLESCRIPT VERSION 3.1.5, PLEASE EDIT WITH CARE
'use strict';

var Js_exn = require("bs-platform/lib/js/js_exn.js");
var KeycloakJs = require("keycloak-js");

function authorize(kcConfig) {
  return new Promise((function (resolve, reject) {
                KeycloakJs.keycloak(kcConfig).init({
                          onLoad: "login-required"
                        }).success((function (autheticated) {
                          if (autheticated) {
                            return resolve("authenticated");
                          } else {
                            return reject(Js_exn.raiseError("Your are not authenticated!"));
                          }
                        })).error((function () {
                        return reject(Js_exn.raiseError("There is something wrong with keycloak"));
                      }));
                return /* () */0;
              }));
}

exports.authorize = authorize;
/* keycloak-js Not a pure module */

but I was expected :

// Generated by BUCKLESCRIPT VERSION 3.1.5, PLEASE EDIT WITH CARE
'use strict';

var Js_exn = require("bs-platform/lib/js/js_exn.js");
var KeycloakJs = require("keycloak-js");

function authorize(kcConfig) {
  return new Promise((function (resolve, reject) {
                KeycloakJs(kcConfig).init({
                          onLoad: "login-required"
                        }).success((function (autheticated) {
                          if (autheticated) {
                            return resolve("authenticated");
                          } else {
                            return reject(Js_exn.raiseError("Your are not authenticated!"));
                          }
                        })).error((function () {
                        return reject(Js_exn.raiseError("There is something wrong with keycloak"));
                      }));
                return /* () */0;
              }));
}

exports.authorize = authorize;

The difference is here KeycloakJs.keycloak(kcConfig).init({ and KeycloakJs(kcConfig).init({.
How to get it?

Thanks


#2

It looks like you want

[@bs.module] external keycloak : string => keycloak = "keycloak-js";

That binds to the default export of a module, whereas

[@bs.module "keycloak-js"] external keycloak : string => keycloak = "";

binds to an export named keyclock, rather than the default export.

The external cheatsheet is very useful when writing bindings :slight_smile: