Import external Javascript library in Workato Javascript connector

Share this:

If you are familiar with Workato’s Javascript connector, you know that there isn’t an out of the box way to import external javascript library in the Workato javascript connector. The connector only includes Node.js 14 libraries and a few other external libraries. So what do you do when you need to be able to parse a SWIFT MT940 bank statement file on Workato? You figure out how to get the external Javascript to be imported! Read on to see how I finally did exactly that.

Workato Javascript connector

External Javascript library in Workato

Here’s the not-so-secret method, use Browserify!

browserify.org banner

What Browserify does is that it would bundle all the dependencies you need, aka all the external javascript libraries, into a single bundle which you can then include inline in your javascript code! As long as this bundle is within the limits of the Workato Javascript connector, you’re good to go! So, remember to keep the total size of the code to under 1MB and that it can finish executing under 30 seconds.

For this example, I would share how I used the mt940-nodejs on my Workato recipe.

1. Install Browserify

npm install -g browserify

2. Install the external library you need

You will need to install the external library you need for your javascript code. You can install in globally on your machine, or in my case, I will be installing the mt940-nodejs library in a working directory. In your case, you would be replacing mt940-nodejs with the name of the library you need.

npm install mt940-nodejs

3. Browserify the external library to a bundle.js

The Browserify command below is quite self explanatory. -o is the name of the output file. -r is the library name you want to include in the bundle. –node is a alias that turns off a bunch of bundling that’s not required to run the bundle in node.

browserify -o bundle.js -r mt940-nodejs --node

You should then find a file that looks like the screen capture below.

Browserify mt940-nodejs javascript library

4. Time to code in Workato Javascript Connector

The Workato Javascript connector requires your code to be in the exports.main function. So you would include the bundle.js content as the structure below.

exports.main = async ({input}) => {
  
  //start of browserify bundle
  //Copy everything in the bundle.js file here.
  //end of browserify bundle
  
  //start of main function 
  //Write your code here.
  //end of main function
};

Below is the example of my Javascript connector code with the the mt940-nodejs library. You can see right at the bottom of the bundle.js content where the .read function of the mt940-nodejs library is include in the code.

exports.main = async ({mt_940_file}) => {
  
  //start of browserify bundle
  require=(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";

...
...

},{}],"mt940-nodejs":[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var md5_1 = require("./utils/md5");
var parser = require("./parser");
var invalidInputMessage = 'invalid input';
function read(input, options) {
    var data;
    if (typeof Buffer !== 'undefined' && input instanceof Buffer) {
        data = input;
    }
    else if (typeof ArrayBuffer !== 'undefined' && input instanceof ArrayBuffer) {
        data = new Uint8Array(input);
    }
    else {
        return Promise.reject(new Error(invalidInputMessage));
    }
    return parser.read(data, Object.assign({
        getTransactionId: function (transaction) {
            return md5_1.default(JSON.stringify(transaction));
        }
    }, options)).catch(function () {
        return Promise.reject(new Error(invalidInputMessage));
    });
}
exports.read = read;

},{"./parser":1,"./utils/md5":14}]},{},[]);
  //end of browserify bundle
  
  //start of main function
  const mt940 = require('mt940-nodejs');
  const file = Buffer.from(mt_940_file, 'utf8');    
  let parsed;
  parsed = await mt940.read(file);
  return {"output":JSON.stringify(parsed)};
  //end of main function
};

5. Code working as required

With that, I am now able to execute a custom SWIFT MT940 parser on my Workato recipe. SWEET!

Custom MT940 parser on Workato Javascript connector


If this post has been useful, support me by buying me a latte or two 🙂
Buy Me A Coffee
Share this:

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.