Fable interop 101: generate-password

François Nicaise
4 min readJan 9, 2019

--

Hi everybody! This is yet another post related to interop for Fable 2! Today we’ll play with the generate-password library for Node.js.

Some doc for starters

Before we begin, be sure to have a look at the following articles related to interop issues with Fable:

What is generate-password?

From the official doc,

Generate Password is a (relatively) extensive library for generating random and unique passwords.

Step 1 : RTFM

As I previously wrote in my latest article, for starters, we need to read the doc and see how the library works. Here, it’s pretty straightforward.

We’ve got two functions: generate

and generateMultiple

And last but not least: some options.

So let’s start with this!

Step 2 : add the lib to our project

So we’ll first add our lib using yarn add generate-password or npm install generate-password --save (or whatever the tool you want to use)

Step 3 : analyse the requirements

So we’ll need basically three things:

  • A: an import statement, the equivalent of the require directive
  • B: an interface to be able to use my import with two functions: generate and generateMultiple
  • C: an interface to be able to set my optionobject

Step 4 : let’s do the thing

A. import statement

So we’ve got a brand new module called Fable.Import.GenPass with the equivalent our our js require statement:

VS

We’ve already defined that our Fable.Import.GenPass.generator will be of type IExports

B. IExports interface

Here it’s again pretty straightforward: we just need to add our two functions generate and generateMultiple They both take and IOptions object parameter.

So we’re already able to make calls likegenerator.generate options or enerator.generateMultiple (3,options) .

C. IOptions interface

Now we just need our IOptionsinterface to be able to set an object with only relevant. Why using an Interface instead of a Record. Because if you take a look at the table below, some values are already set by default. So we don’t need to set all the values for all the members. We can just use what we need.

Here we go for our IOptions interface

Step 5 : let’s play!

So now everything’s ready, let’s use generate-password from our Fable project!

Basically there’s two things we do here:

  • prepare our options using Fable nice jsOptions instruction
  • call the generate functions with our options parameter.

That’s it!

The complete code

A last word

it’s often fast to do things manually

So the main idea I tried to develop in this article was: when the lib is not complicated or when you don’t need to use all features, it’s often faster to do things manually. It often just requires to read the doc.

That’s it for this little article about interop. Don’t hesitate to ask me questions. I will try to answer them all.

Thanks for your patience. See you on twitter and Github.

--

--